labels与selector 在配置文件中可以使用labels
来指定资源的labels
1 2 3 4 5 6 [root@k8s-master pods] NAME READY STATUS RESTARTS AGE LABELS nginx-85b98978db-4xdrk 1/1 Running 2 (13h ago) 19h app=nginx,pod-template-hash=85b98978db nginx-85b98978db-8h2hh 1/1 Running 3 (13h ago) 23h app=nginx,pod-template-hash=85b98978db nginx-85b98978db-h452n 1/1 Running 2 (13h ago) 19h app=nginx,pod-template-hash=85b98978db nginx-po 1/1 Running 0 95s test =1.0.0,type =app
-l
可以使用selector来筛选资源,多个条件并列写时是and的关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@k8s-master pods] NAME READY STATUS RESTARTS AGE LABELS nginx-85b98978db-4xdrk 1/1 Running 2 (13h ago) 19h app=nginx,pod-template-hash=85b98978db nginx-85b98978db-8h2hh 1/1 Running 3 (13h ago) 23h app=nginx,pod-template-hash=85b98978db nginx-85b98978db-h452n 1/1 Running 2 (13h ago) 19h app=nginx,pod-template-hash=85b98978db nginx-po 1/1 Running 0 95s test =1.0.0,type =app [root@k8s-master pods] [root@k8s-master pods] NAME READY STATUS RESTARTS AGE nginx-85b98978db-4xdrk 1/1 Running 2 (13h ago) 19h nginx-85b98978db-8h2hh 1/1 Running 3 (13h ago) 24h nginx-85b98978db-h452n 1/1 Running 2 (13h ago) 19h [root@k8s-master pods] NAME READY STATUS RESTARTS AGE nginx-po 1/1 Running 0 5m11s [root@k8s-master pods] NAME READY STATUS RESTARTS AGE nginx-po 1/1 Running 0 5m46s [root@k8s-master pods] No resources found in default namespace.
deployment 使用kubectl create deploy nginx-deploy --image=nginx:1.7.9
创建一个deployment
查看rs和pod,发现deploy会自动创建对应的rs和pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@k8s-master pods] NAME READY UP-TO-DATE AVAILABLE AGE nginx 3/3 3 3 24h nginx-deploy 1/1 1 1 64s [root@k8s-master pods] NAME DESIRED CURRENT READY AGE nginx-85b98978db 3 3 3 24h nginx-deploy-78d8bf4fd7 1 1 1 3m2s [root@k8s-master pods] NAME READY STATUS RESTARTS AGE nginx-85b98978db-4xdrk 1/1 Running 2 (14h ago) 20h nginx-85b98978db-8h2hh 1/1 Running 3 (14h ago) 24h nginx-85b98978db-h452n 1/1 Running 2 (14h ago) 20h nginx-deploy-78d8bf4fd7-p4rkq 1/1 Running 0 4m38s nginx-po 1/1 Running 0 40m
使用kubectl get deploy nginx-deploy -o yaml
命令可以查看他的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: "2023-11-28T03:10:15Z" generation: 1 labels: app: nginx-deploy name: nginx-deploy namespace: default resourceVersion: "30430" uid: 404bfa08-851b-4e1b-b35b-9c44131203fd spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: app: nginx-deploy strategy: rollingUpdate: maxSurge: 25 % maxUnavailable: 25 % type: RollingUpdate template: metadata: creationTimestamp: null labels: app: nginx-deploy spec: containers: - image: nginx:1.7.9 imagePullPolicy: IfNotPresent name: nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 status: availableReplicas: 1 conditions: - lastTransitionTime: "2023-11-28T03:10:18Z" lastUpdateTime: "2023-11-28T03:10:18Z" message: Deployment has minimum availability. reason: MinimumReplicasAvailable status: "True" type: Available - lastTransitionTime: "2023-11-28T03:10:15Z" lastUpdateTime: "2023-11-28T03:10:18Z" message: ReplicaSet "nginx-deploy-78d8bf4fd7" has successfully progressed. reason: NewReplicaSetAvailable status: "True" type: Progressing observedGeneration: 1 readyReplicas: 1 replicas: 1 updatedReplicas: 1
删除一些不必要的配置项之后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-deploy name: nginx-deploy namespace: default spec: replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: app: nginx-deploy strategy: rollingUpdate: maxSurge: 25 % maxUnavailable: 25 % type: RollingUpdate template: metadata: labels: app: nginx-deploy spec: containers: - image: nginx:1.7.9 imagePullPolicy: IfNotPresent name: nginx restartPolicy: Always terminationGracePeriodSeconds: 30
滚动更新 只有修改了 deployment 配置文件中的 template 中的属性后,才会触发更新操作
使用kubectl edit deploy/nginx-deploy
命令修改label属性,发现deploy并不会更新(通过kubectl get deploy
查看deploy状态时,发现READY一直是1/1)
下面修改image版本号
使用describe
发现deploy完成了更新操作
把副本数修改为3
版本回滚 有时候你可能想回退一个Deployment,例如,当Deployment不稳定时,比如一直crash looping。
ps:可以通过设置 .spec.revisonHistoryLimit 来指定 deployment 保留多少 revison,如果设置为 0,则不允许 deployment 回退了。
更新 deployment 时参数不小心写错,如 nginx:1.9.1 写成了 nginx:1.91
kubectl set image deployment/nginx-deploy nginx=nginx:1.91
此时deploy会执行更新操作,使用kubectl get po
,发现刚刚更新的pod处于ErrImagePull
状态(显然这是由于刚才版本号写错了导致的)
下面进行版本回滚
通过kubectl rollout history deploy/nginx-deploy
查看可以回滚的版本
通过kubectl rollout history deploy/nginx-deploy
可以查看具体的版本信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 [root@k8s-master ~] deployment.apps/nginx-deploy with revision Pod Template: Labels: app=nginx-deploy pod-template-hash=754898b577 Containers: nginx: Image: nginx:1.9.1 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> [root@k8s-master ~] deployment.apps/nginx-deploy with revision Pod Template: Labels: app=nginx-deploy pod-template-hash=78d8bf4fd7 Containers: nginx: Image: nginx:1.7.9 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> [root@k8s-master ~] deployment.apps/nginx-deploy with revision Pod Template: Labels: app=nginx-deploy pod-template-hash=f7f5656c7 Containers: nginx: Image: nginx:1.91 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> [root@k8s-master ~]
确认要回退的版本后,可以通过kubectl rollout undo deploy/nginx-deploy --to-revision=1
回退到指定版本
再次通过 kubectl get deployment 和 kubectl describe deployment 可以看到,我们的版本已经回退到对应的 revison 上了
暂停与恢复 由于每次对 pod template 中的信息发生修改后,都会触发更新 deployment 操作,那么此时如果频繁修改信息,就会产生多次更新,而实际上只需要执行最后一次更新即可,当出现此类情况时我们就可以暂停 deployment 的 rollout
通过 kubectl rollout pause deployment <name> 就可以实现暂停,直到你下次恢复后才会继续进行滚动更新
使用edit更新配置文件后 pod 和 rs均没有改变(也就是说并没有更新)
使用kubectl rollout resume deploy nginx-deploy
命令恢复