- 创建Deployment
- 滚动更新
- ReplicaSet
- 回滚
- 水平伸缩
@创建Deployment
首先创建一个deployment,pod副本数为3,使用镜像nginx:1.19,并创建服务
test-deploy-svc.yaml 的内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: web
name: web
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
labels:
app: web
spec:
containers:
- image: nginx:1.19
name: nginx
---
apiVersion: v1
kind: Service
metadata:
labels:
app: web
name: web
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web
type: NodePort
创建deployment
[root@k8s-master ~]# kubectl apply -f test-deploy-svc.yaml
deployment.apps/web created
service/web created
[root@k8s-master ~]#
查看svc的NodePort,为31147
[root@k8s-master ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19d <none>
web NodePort 10.99.203.162 <none> 80:31147/TCP 49s app=web
[root@k8s-master ~]#
查询svc关联的pod
[root@k8s-master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.231.121:6443 19d
web 10.244.169.161:80,10.244.36.84:80,10.244.36.88:80 2m28s
[root@k8s-master ~]#
浏览器打开主页,http://<cluster中任意node的地址>:31147
同时打开控制台,查看nginx版本,这里是1.19.10
@滚动更新
方法1: kubectl set image deployment web nginx=nginx:1.20
[root@k8s-master ~]# kubectl set image deployment web nginx=nginx:1.20
deployment.apps/web image updated
[root@k8s-master ~]#
刷新浏览器页面浏览器 版本更新为nginx/1.20.1
pod在这个过程会滚动更新(下一个方法中详述);查看endpoints,svc关联的pod地址变了
[root@k8s-master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.231.121:6443 19d
web 10.244.169.160:80,10.244.36.81:80,10.244.36.82:80 13m
[root@k8s-master ~]#
方法2:修改yaml文件,再次apply,将版本改为1.21
[root@k8s-master ~]# grep image test-deploy-svc.yaml
- image: nginx:1.19
[root@k8s-master ~]# vim test-deploy-svc.yaml
[root@k8s-master ~]# grep image test-deploy-svc.yaml
- image: nginx:1.21
[root@k8s-master ~]#
先记录此刻1.20版本pod的NAME和IP
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-7f5844f89c-6sdxt 1/1 Running 0 5m28s 10.244.169.160 k8s-node2 <none> <none>
web-7f5844f89c-cxqjf 1/1 Running 0 5m29s 10.244.36.82 k8s-node1 <none> <none>
web-7f5844f89c-m6655 1/1 Running 0 5m26s 10.244.36.81 k8s-node1 <none> <none>
[root@k8s-master ~]#
执行kubectl apply -f test-deploy-svc.yaml更新
[root@k8s-master ~]# kubectl apply -f test-deploy-svc.yaml
deployment.apps/web configured
service/web unchanged
[root@k8s-master ~]#
不断使用命令kubectl get pod -o wide查看pod创建情况
--> 正在创建一个新pod,其状态为ContainerCreating
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-7f5844f89c-6sdxt 1/1 Running 0 6m7s 10.244.169.160 k8s-node2 <none> <none>
web-7f5844f89c-cxqjf 1/1 Running 0 6m8s 10.244.36.82 k8s-node1 <none> <none>
web-7f5844f89c-m6655 1/1 Running 0 6m5s 10.244.36.81 k8s-node1 <none> <none>
web-d779974b6-j72dv 0/1 ContainerCreating 0 4s <none> k8s-node1 <none> <none>
[root@k8s-master ~]#
--> 第一个新pod状态变为Running,一个旧pod进入Terminating,同时创建第二个新pod
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-7f5844f89c-6sdxt 1/1 Running 0 6m22s 10.244.169.160 k8s-node2 <none> <none>
web-7f5844f89c-cxqjf 1/1 Running 0 6m23s 10.244.36.82 k8s-node1 <none> <none>
web-7f5844f89c-m6655 1/1 Terminating 0 6m20s 10.244.36.81 k8s-node1 <none> <none>
web-d779974b6-j72dv 1/1 Running 0 19s 10.244.36.90 k8s-node1 <none> <none>
web-d779974b6-j9fkf 0/1 ContainerCreating 0 0s <none> k8s-node2 <none> <none>
[root@k8s-master ~]#
……
--> 创建了三个新pod,旧的三个pod处于Terminating状态
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-7f5844f89c-6sdxt 0/1 Terminating 0 6m25s 10.244.169.160 k8s-node2 <none> <none>
web-7f5844f89c-cxqjf 1/1 Terminating 0 6m26s 10.244.36.82 k8s-node1 <none> <none>
web-7f5844f89c-m6655 0/1 Terminating 0 6m23s 10.244.36.81 k8s-node1 <none> <none>
web-d779974b6-9nmb6 1/1 Running 0 2s 10.244.36.91 k8s-node1 <none> <none>
web-d779974b6-j72dv 1/1 Running 0 22s 10.244.36.90 k8s-node1 <none> <none>
web-d779974b6-j9fkf 1/1 Running 0 3s 10.244.169.164 k8s-node2 <none> <none>
[root@k8s-master ~]#
……
--> 三个pod全部"更新"为新版本
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-d779974b6-9nmb6 1/1 Running 0 11s 10.244.36.91 k8s-node1 <none> <none>
web-d779974b6-j72dv 1/1 Running 0 31s 10.244.36.90 k8s-node1 <none> <none>
web-d779974b6-j9fkf 1/1 Running 0 12s 10.244.169.164 k8s-node2 <none> <none>
[root@k8s-master ~]#
以上步骤可以看到pod分配了新的IP,查看endpoints也可以观察到service绑定的pod地址更新了
[root@k8s-master ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.231.121:6443 19d
web 10.244.169.164:80,10.244.36.90:80,10.244.36.91:80 30m
[root@k8s-master ~]#
刷新浏览器页面,查看nginx版本更新为 nginx/1.21.1
@ReplicaSet
常用命令:
kubectl get replicaset
kubectl describe deployment <deployment name>
kubectl describe <replicaset name>
滚动升级的执行者是 控制器 replicaset(rs) ,管理pod的副本数量,也会记录发布的版本
用命令kubectl get replicaset查看rs,最后一行期望副本数为3的web-d779974b6是最近一次的部署,对应上述最新的pod,可以发现 pod NAME = deployment NAME + rs NAME + 随机字符串
[root@k8s-master ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
web-7f5844f89c 0 0 0 30m
web-bc7cc9f65 0 0 0 40m
web-d779974b6 3 3 3 24m
[root@k8s-master ~]#
查看deployment详细信息可以看到滚动升级的过程,关注下述最后的Events部分
(但是过一段时间再执行就会变成 Events: <none>)
[root@k8s-master ~]# kubectl describe deployment web
Name: web
Namespace: default
CreationTimestamp: Sat, 14 Aug 2021 14:32:35 +0800
Labels: app=web
Annotations: deployment.kubernetes.io/revision: 3
Selector: app=web
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=web
Containers:
nginx:
Image: nginx:1.21
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: web-d779974b6 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 59m deployment-controller Scaled up replica set web-d779974b6 to 1
Normal ScalingReplicaSet 58m deployment-controller Scaled down replica set web-7f5844f89c to 2
Normal ScalingReplicaSet 58m deployment-controller Scaled up replica set web-d779974b6 to 2
Normal ScalingReplicaSet 58m (x3 over 58m) deployment-controller (combined from similar events): Scaled down replica set web-7f5844f89c to 0
[root@k8s-master ~]#
查看rs信息也可以看到标记的版本
[root@k8s-master ~]# kubectl describe rs web-d779974b6
Name: web-d779974b6
Namespace: default
Selector: app=web,pod-template-hash=d779974b6
Labels: app=web
pod-template-hash=d779974b6
Annotations: deployment.kubernetes.io/desired-replicas: 3
deployment.kubernetes.io/max-replicas: 4
deployment.kubernetes.io/revision: 3
Controlled By: Deployment/web
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=web
pod-template-hash=d779974b6
Containers:
nginx:
Image: nginx:1.21
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events: <none>
[root@k8s-master ~]#
@回滚
常用命令:
kubectl rollout history deployment web
kubectl rollout undo deployment web
kubectl rollout undo deployment web --to-revision=<num>
查看历史发布版本
[root@k8s-master ~]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
[root@k8s-master ~]#
说明:部署的时候加上--record CHANGE-CAUSE会有记录
[root@k8s-master ~]# kubectl set image deployment web nginx=nginx:1.21 --record
deployment.apps/web image updated
[root@k8s-master ~]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1 <none>
4 <none>
5 kubectl set image deployment web nginx=nginx:1.21 --record=true
[root@k8s-master ~]#
回滚到上一个版本(常用)
[root@k8s-master ~]# kubectl rollout undo deployment web
deployment.apps/web rolled back
[root@k8s-master ~]#
可以查看到rs的变化
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
web-7f5844f89c 3 3 3 88m
web-bc7cc9f65 0 0 0 98m
web-d779974b6 0 0 0 82m
[root@k8s-master ~]#
@水平伸缩
方法1:修改yaml文件中replicas的值
方法2:使用命令 kubectl scale deployment web --replicas=<pod num>
例如,使用方法2缩容到2个pod
[root@k8s-master ~]# kubectl scale deployment web --replicas=2
deployment.apps/web scaled
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-d779974b6-6rctw 1/1 Running 0 5m41s
web-d779974b6-dv7fh 1/1 Running 0 5m40s
web-d779974b6-j6n9r 0/1 Terminating 0 5m38s
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-d779974b6-6rctw 1/1 Running 0 5m50s
web-d779974b6-dv7fh 1/1 Running 0 5m49s
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
web-7f5844f89c 0 0 0 128m
web-bc7cc9f65 0 0 0 138m
web-d779974b6 2 2 2 122m
[root@k8s-master ~]#
再使用方法1扩容到3个pod
[root@k8s-master ~]# kubectl apply -f test-deploy-svc.yaml
deployment.apps/web configured
service/web unchanged
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-d779974b6-6rctw 1/1 Running 0 7m34s
web-d779974b6-dv7fh 1/1 Running 0 7m33s
web-d779974b6-n8p6d 1/1 Running 0 5s
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
web-7f5844f89c 0 0 0 131m
web-bc7cc9f65 0 0 0 141m
web-d779974b6 3 3 3 125m
[root@k8s-master ~]#