Skip to content

1. 创建yaml文件

vim deployment-demo.yaml

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-demo
spec:
  replicas: 4
  selector:
    matchLabels:
      app: demoapp
      release: stable
  template:
    metadata:
      labels:
        app: demoapp
        release: stable
    spec:
      containers:
      - name: demoapp
        image:registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:${VERSION}
        ports:
        - containerPort: 80
          name: http
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-demo
spec:
  replicas: 4
  selector:
    matchLabels:
      app: demoapp
      release: stable
  template:
    metadata:
      labels:
        app: demoapp
        release: stable
    spec:
      containers:
      - name: demoapp
        image:registry.cn-zhangjiakou.aliyuncs.com/hsuing/demoapp:${VERSION}
        ports:
        - containerPort: 80
          name: http

2. 应用清单文件

bash
VERSION=v1.0 envsubst < deployment-demo.yaml | kubectl apply -f -
VERSION=v1.0 envsubst < deployment-demo.yaml | kubectl apply -f -
bash
# 查看deployment信息
root@k8s-master01:~/yaml/chapter08# kubectl get deployments.apps
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
deployment-demo   4/4     4            4           12s

# 查看pod信息
root@k8s-master01:~/yaml/chapter08# kubectl get pods -l 'app=demoapp,release=stable'
NAME                              READY   STATUS    RESTARTS   AGE
deployment-demo-fb544c5d8-2687q   1/1     Running   0          2m16s
deployment-demo-fb544c5d8-2t6q4   1/1     Running   0          2m16s
deployment-demo-fb544c5d8-pkgzn   1/1     Running   0          2m16s
deployment-demo-fb544c5d8-w52qp   1/1     Running   0          2m16s
# 可以看到第一段为deployment名字,最后一段为随机值,中间的fb544c5d8为replicaset中Pod模板的哈希值,也就是template字段的哈希值

# 查看replicaset信息
root@k8s-master01:~/yaml/chapter08# kubectl get replicasets.apps
NAME                        DESIRED   CURRENT   READY   AGE
deployment-demo-fb544c5d8   4         4         4       4m5s

# 一旦pod模板发生变更,会导致ReplicaSet的哈希值发生变化,然后出发deployment更新的
# 查看deployment信息
root@k8s-master01:~/yaml/chapter08# kubectl get deployments.apps
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
deployment-demo   4/4     4            4           12s

# 查看pod信息
root@k8s-master01:~/yaml/chapter08# kubectl get pods -l 'app=demoapp,release=stable'
NAME                              READY   STATUS    RESTARTS   AGE
deployment-demo-fb544c5d8-2687q   1/1     Running   0          2m16s
deployment-demo-fb544c5d8-2t6q4   1/1     Running   0          2m16s
deployment-demo-fb544c5d8-pkgzn   1/1     Running   0          2m16s
deployment-demo-fb544c5d8-w52qp   1/1     Running   0          2m16s
# 可以看到第一段为deployment名字,最后一段为随机值,中间的fb544c5d8为replicaset中Pod模板的哈希值,也就是template字段的哈希值

# 查看replicaset信息
root@k8s-master01:~/yaml/chapter08# kubectl get replicasets.apps
NAME                        DESIRED   CURRENT   READY   AGE
deployment-demo-fb544c5d8   4         4         4       4m5s

# 一旦pod模板发生变更,会导致ReplicaSet的哈希值发生变化,然后出发deployment更新的

3. 查看deployment的描述信息

bash
kubectl describe deployments.apps deployment-demo

Name:                   deployment-demo
Namespace:              default
CreationTimestamp:      Wed, 21 Apr 2024 13:23:13 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=demoapp,release=stable
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate      # 一旦模板发生变化将触发滚动跟新
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge     # 滚动更新逻辑
kubectl describe deployments.apps deployment-demo

Name:                   deployment-demo
Namespace:              default
CreationTimestamp:      Wed, 21 Apr 2024 13:23:13 +0000
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=demoapp,release=stable
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate      # 一旦模板发生变化将触发滚动跟新
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge     # 滚动更新逻辑

4. 更新

VERSION=v2.0 envsubst < deployment-demo.yaml | kubectl apply -f -
VERSION=v2.0 envsubst < deployment-demo.yaml | kubectl apply -f -
  • 使用rollout查看滚动更新的状态
kubectl rollout status deployment deployment-demo
kubectl rollout status deployment deployment-demo
  • 查看pod是否更新为v2.0版本
bash
kubectl get pods -o wide

# 对pod进行访问
root@k8s-master01:~# curl pod_ip
hsuing demoapp v2.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-867c7d9d55-b4trp, ServerIP: 10.244.3.33!
kubectl get pods -o wide

# 对pod进行访问
root@k8s-master01:~# curl pod_ip
hsuing demoapp v2.0 !! ClientIP: 10.244.0.0, ServerName: deployment-demo-867c7d9d55-b4trp, ServerIP: 10.244.3.33!

5. 回滚

  • 查看deployment的更新历史信息
bash
root@k8s-master01:~# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION  CHANGE-CAUSE
1         <none>
2         <none>   # 此为当前版本信息
root@k8s-master01:~# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION  CHANGE-CAUSE
1         <none>
2         <none>   # 此为当前版本信息
  • 将其回滚到上一个版本
bash
kubectl rollout undo deployment deployment-demo
kubectl rollout undo deployment deployment-demo
  • 查看replicaset信息
bash
root@k8s-master01:~# kubectl get rs
NAME                         DESIRED   CURRENT   READY   AGE
deployment-demo-867c7d9d55   0         0         0       24m
deployment-demo-fb544c5d8    4         4         4       48m
# 原本的deployment-demo-867c7d9d55已经变为0
root@k8s-master01:~# kubectl get rs
NAME                         DESIRED   CURRENT   READY   AGE
deployment-demo-867c7d9d55   0         0         0       24m
deployment-demo-fb544c5d8    4         4         4       48m
# 原本的deployment-demo-867c7d9d55已经变为0
  • 再次查看更新历史
bash
root@k8s-master01:~# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
# 可以看到原本的1没有了新增了3,3表示当前的
root@k8s-master01:~# kubectl rollout history deployment deployment-demo
deployment.apps/deployment-demo
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
# 可以看到原本的1没有了新增了3,3表示当前的

VERSION=v1.2 envsubst < deployment-demo.yaml | kubectl apply -f - && kubectl rollout pause deployment deployment-demo

kubectl rollout status deployment deployment-demo

kubectl rollout resume deployment deployment-demo