k8s练习-创建一个Deployment

创建Deployment

  1. 创建一个nginx deployment

    [root@k8s-master home]# cat nginx-deployment.yaml

powershell 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
spec:
 selector:
  matchLabels:
   app: nginx		# 配置pod的labels
 replicas: 2		# 声明2个副本
 template:
  metadata:
   labels:
    app: nginx		# 副本的labels
  spec: 
   containers:
   - name: nginx
     image: nginx
     ports:
     - containerPort: 80
[root@k8s-master home]# kubectl create -f nginx-deployment.yaml 
  1. 查看deploy

    [root@k8s-master home]# kubectl get deploy

  2. 显示nginx Deployment的相关信息

    [root@k8s-master home]# kubectl describe deploy nginx-deployment

powershell 复制代码
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Mon, 01 Apr 2024 22:15:47 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-7c5ddbdf54 (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  4m40s  deployment-controller  Scaled up replica set nginx-deployment-7c5ddbdf54 to 2
  1. 列出该Deployment创建的Pod

    查看nginx-deployment 匹配的Pod标签

     [root@k8s-master home]# kubectl get deployment nginx-deployment -o wide
    

    通过标签查看该Department创建的所有的Pod

     [root@k8s-master home]# kubectl get pods -l app=nginx
    
  2. 展示某个Pod的相关信息

     kubectl describe pod <pod-name>
    

更新Deployment

  1. 修改nginx 副本数,将2 个增加到 4 个
    直接修改yaml文件
powershell 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
spec:
 selector:
  matchLabels:
   app: nginx
 replicas: 4		# 增加副本个数
 template:
  metadata:
   labels:
    app: nginx
  spec:
   containers:
   - name: nginx
     image: nginx
     ports:
     - containerPort: 80

更新nginx-deployment

[root@k8s-master home]# kubectl apply -f nginx-deployment.yaml

查看Pod

[root@k8s-master home]# kubectl get pod

使用edit更新一个Deployment

[root@k8s-master home]# kubectl edit deployment  nginx-deployment

将副本数修改为10个并保存

查看pod

删除Deployment

格式

kubectl delete <资源类型> <资源名称>

[root@k8s-master home]# kubectl delete deploy nginx-deployment

扩展:

  1. 查看所有api资源,简称以及资源类型对应的api

     kubectl api-resources
    
powershell 复制代码
[root@k8s-master home]# kubectl api-resources | grep apps
controllerrevisions                            apps/v1                                true         ControllerRevision
daemonsets                        ds           apps/v1                                true         DaemonSet
deployments                       deploy       apps/v1                                true         Deployment
replicasets                       rs           apps/v1                                true         ReplicaSet
statefulsets                      sts          apps/v1                                true         StatefulSet
powershell 复制代码
[root@k8s-master home]# kubectl api-resources | grep pod
pods                              po           v1                                     true         Pod
podtemplates                                   v1                                     true         PodTemplate
horizontalpodautoscalers          hpa          autoscaling/v2                         true         HorizontalPodAutoscaler
poddisruptionbudgets              pdb          policy/v1                              true         PodDisruptionBudget
相关推荐
didiplus1 小时前
Kubernetes 镜像拉取策略全解析:如何根据需求选择最佳配置?
云原生·容器·kubernetes
玄德公笔记1 小时前
docker-compose搭建sfpt服务器
服务器·docker·容器·sftp·docker-compose
小钟不想敲代码3 小时前
Docker部署Sentinel
docker·容器·sentinel
上海运维Q先生3 小时前
面试题整理17----K8s中request和limit资源限制是如何实现的
服务器·云原生·kubernetes
胖毁青春,瘦解百病5 小时前
Docker镜像源设置不生效问题排查
docker·容器
CloudPilotAI5 小时前
15条 Karpenter 最佳实践,轻松掌握弹性伸缩
kubernetes·弹性伸缩·karpenter
会飞的土拨鼠呀5 小时前
Flannel是什么,如何安装Flannel
运维·云原生·kubernetes
ether-lin6 小时前
DevOps实战:用Kubernetes和Argo打造自动化CI/CD流程(1)
ci/cd·kubernetes·devops
Carry_NJ7 小时前
docker-compose样例
运维·docker·容器
勇-子7 小时前
K8s 常用资源介绍
云原生·容器·kubernetes