- 创建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
[[email protected] ~]# kubectl apply -f test-deploy-svc.yaml
deployment.apps/web created
service/web created
[[email protected] ~]#
查看svc的NodePort,为31147
[[email protected] ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1
web NodePort 10.99.203.162
[[email protected] ~]#
查询svc关联的pod
[[email protected] ~]# 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
[[email protected] ~]#
浏览器打开主页,http://
同时打开控制台,查看nginx版本,这里是1.19.10
@滚动更新
方法1: kubectl set image deployment web nginx=nginx:1.20
[[email protected] ~]# kubectl set image deployment web nginx=nginx:1.20
deployment.apps/web image updated
[[email protected] ~]#
刷新浏览器页面浏览器 版本更新为nginx/1.20.1
pod在这个过程会滚动更新(下一个方法中详述);查看endpoints,svc关联的pod地址变了
[[email protected] ~]# 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
[[email protected] ~]#
方法2:修改yaml文件,再次apply,将版本改为1.21
[[email protected] ~]# grep image test-deploy-svc.yaml
- image: nginx:1.19
[[email protected] ~]# vim test-deploy-svc.yaml
[[email protected] ~]# grep image test-deploy-svc.yaml
- image: nginx:1.21
[[email protected] ~]#
先记录此刻1.20版本pod的NAME和IP
[[email protected] ~]# 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
web-7f5844f89c-cxqjf 1/1 Running 0 5m29s 10.244.36.82 k8s-node1
web-7f5844f89c-m6655 1/1 Running 0 5m26s 10.244.36.81 k8s-node1
[[email protected] ~]#
执行kubectl apply -f test-deploy-svc.yaml更新
[[email protected] ~]# kubectl apply -f test-deploy-svc.yaml
deployment.apps/web configured
service/web unchanged
[[email protected] ~]#
不断使用命令kubectl get pod -o wide查看pod创建情况
--> 正在创建一个新pod,其状态为ContainerCreating
[[email protected] ~]# 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
web-7f5844f89c-cxqjf 1/1 Running 0 6m8s 10.244.36.82 k8s-node1
web-7f5844f89c-m6655 1/1 Running 0 6m5s 10.244.36.81 k8s-node1
web-d779974b6-j72dv 0/1 ContainerCreating 0 4s
[[email protected] ~]#
--> 第一个新pod状态变为Running,一个旧pod进入Terminating,同时创建第二个新pod
[[email protected] ~]# 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
web-7f5844f89c-cxqjf 1/1 Running 0 6m23s 10.244.36.82 k8s-node1
web-7f5844f89c-m6655 1/1 Terminating 0 6m20s 10.244.36.81 k8s-node1
web-d779974b6-j72dv 1/1 Running 0 19s 10.244.36.90 k8s-node1
web-d779974b6-j9fkf 0/1 ContainerCreating 0 0s
[[email protected] ~]#
……
--> 创建了三个新pod,旧的三个pod处于Terminating状态
[[email protected] ~]# 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
web-7f5844f89c-cxqjf 1/1 Terminating 0 6m26s 10.244.36.82 k8s-node1
web-7f5844f89c-m6655 0/1 Terminating 0 6m23s 10.244.36.81 k8s-node1
web-d779974b6-9nmb6 1/1 Running 0 2s 10.244.36.91 k8s-node1
web-d779974b6-j72dv 1/1 Running 0 22s 10.244.36.90 k8s-node1
web-d779974b6-j9fkf 1/1 Running 0 3s 10.244.169.164 k8s-node2
[[email protected] ~]#
……
--> 三个pod全部"更新"为新版本
[[email protected] ~]# 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
web-d779974b6-j72dv 1/1 Running 0 31s 10.244.36.90 k8s-node1
web-d779974b6-j9fkf 1/1 Running 0 12s 10.244.169.164 k8s-node2
[[email protected] ~]#
以上步骤可以看到pod分配了新的IP,查看endpoints也可以观察到service绑定的pod地址更新了
[[email protected] ~]# 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
[[email protected] ~]#
刷新浏览器页面,查看nginx版本更新为 nginx/1.21.1
@ReplicaSet
常用命令:
kubectl get replicaset
kubectl describe deployment
kubectl describe
滚动升级的执行者是 控制器 replicaset(rs) ,管理pod的副本数量,也会记录发布的版本
用命令kubectl get replicaset查看rs,最后一行期望副本数为3的web-d779974b6是最近一次的部署,对应上述最新的pod,可以发现 pod NAME = deployment NAME + rs NAME + 随机字符串
[[email protected] ~]# 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
[[email protected] ~]#
查看deployment详细信息可以看到滚动升级的过程,关注下述最后的Events部分
(但是过一段时间再执行就会变成 Events:
[[email protected] ~]# 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:
Host Port:
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
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
[[email protected] ~]#
查看rs信息也可以看到标记的版本
[[email protected] ~]# 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:
Host Port:
Environment:
Mounts:
Volumes:
Events:
[[email protected] ~]#
@回滚
常用命令:
kubectl rollout history deployment web
kubectl rollout undo deployment web
kubectl rollout undo deployment web --to-revision=
查看历史发布版本
[[email protected] ~]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1
2
3
[[email protected] ~]#
说明:部署的时候加上--record CHANGE-CAUSE会有记录
[[email protected] ~]# kubectl set image deployment web nginx=nginx:1.21 --record
deployment.apps/web image updated
[[email protected] ~]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1
4
5 kubectl set image deployment web nginx=nginx:1.21 --record=true
[[email protected] ~]#
回滚到上一个版本(常用)
[[email protected] ~]# kubectl rollout undo deployment web
deployment.apps/web rolled back
[[email protected] ~]#
可以查看到rs的变化
[[email protected] ~]# 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
[[email protected] ~]#
@水平伸缩
方法1:修改yaml文件中replicas的值
方法2:使用命令 kubectl scale deployment web --replicas=
例如,使用方法2缩容到2个pod
[[email protected] ~]# kubectl scale deployment web --replicas=2
deployment.apps/web scaled
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web-d779974b6-6rctw 1/1 Running 0 5m50s
web-d779974b6-dv7fh 1/1 Running 0 5m49s
[[email protected] ~]#
[[email protected] ~]# 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
[[email protected] ~]#
再使用方法1扩容到3个pod
[[email protected] ~]# kubectl apply -f test-deploy-svc.yaml
deployment.apps/web configured
service/web unchanged
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# 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
[[email protected] ~]#