Deployment:介绍
- 管理Pod和ReplicaSet
- 具有上线部署、副本设定、滚动升级、回滚等功能
- 提供声明式更新,例如只更新一个新的Image
Deployment:使用流程
项目生命周期
Deployment:部署
[root@k8s-master ~]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: web
image: nginx:1.16
ports:
- containerPort: 80
env:
- name: "VERSION"
value: "1.19"
livenessProbe:
httpGet:
path: /index.html
port: 80
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 10
periodSeconds: 10
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
[root@k8s-master ~]# kubectl apply -f deployment.yaml
deployment.apps/web created
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-7cc58d88d9-7rvwq 1/1 Running 0 22m 10.244.169.149 k8s-node2 <none> <none>
web-7cc58d88d9-b786z 1/1 Running 0 22m 10.244.169.148 k8s-node2 <none> <none>
web-7cc58d88d9-xqdqx 1/1 Running 0 22m 10.244.36.82 k8s-node1 <none> <none>
第二步:暴露服务
[root@k8s-master ~]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
name: web
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: NodePort
[root@k8s-master ~]# kubectl apply -f service.yaml
service/web created
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 32d
web NodePort 10.96.100.183 <none> 80:30866/TCP 88s
[root@k8s-master ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.179.102:6443 32d
web 10.244.169.148:80,10.244.169.149:80,10.244.36.82:80 2m42s
现在可以根据任意node节点的IP+30866就可以访问到pod了
第三步 Deployment:滚动升级
image: nginx:1.16
滚动更新查看replicaset状态
[root@k8s-master ~]# kubectl get replicaset -w
NAME DESIRED CURRENT READY AGE
web-7cc58d88d9 3 3 3 32m
web-6c8f6b7f84 1 0 0 0s
web-7cc58d88d9 1 3 3 34m
web-6c8f6b7f84 1 0 0 0s
web-6c8f6b7f84 3 0 0 0s
web-7cc58d88d9 1 3 3 34m
web-6c8f6b7f84 3 1 0 1s
web-7cc58d88d9 1 1 1 34m
web-6c8f6b7f84 3 1 0 1s
web-6c8f6b7f84 3 3 0 1s
web-6c8f6b7f84 3 3 1 62s
web-7cc58d88d9 0 1 1 35m
web-7cc58d88d9 0 1 1 35m
web-7cc58d88d9 0 0 0 35m
web-6c8f6b7f84 3 3 2 62s
web-6c8f6b7f84 3 3 3 64s
^C[root@k8s-master ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
web-6c8f6b7f84 3 3 3 4m55s
web-7cc58d88d9 0 0 0 39m
滚动更新查看ep状态
[root@k8s-master ~]# kubectl get ep -w
NAME ENDPOINTS AGE
kubernetes 192.168.179.102:6443 32d
web 10.244.169.148:80,10.244.169.149:80,10.244.36.82:80 7m33s
web 10.244.169.149:80,10.244.36.82:80 9m2s
web 10.244.36.82:80 9m2s
web 10.244.36.82:80 9m47s
web 10.244.36.82:80 9m49s
web 10.244.36.82:80 9m52s
web 10.244.36.82:80,10.244.36.83:80 10m
web 10.244.36.83:80 10m
web 10.244.169.150:80,10.244.36.83:80 10m
web 10.244.169.150:80,10.244.169.151:80,10.244.36.83:80 10m
[root@k8s-master ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.179.102:6443 32d
web 10.244.169.150:80,10.244.169.151:80,10.244.36.83:80 15m
滚动更新查看pod状态(可以看到是有个逐步更新替代的过程,健康检查会影响pod更新速率,因为只有健康检查通过才认为pod是runing状态)
[root@k8s-master ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
web-7cc58d88d9-7rvwq 1/1 Running 0 33m
web-7cc58d88d9-b786z 1/1 Running 0 33m
web-7cc58d88d9-xqdqx 1/1 Running 0 33m
web-6c8f6b7f84-tgqcj 0/1 Pending 0 0s
web-6c8f6b7f84-tgqcj 0/1 Pending 0 0s
web-7cc58d88d9-b786z 1/1 Terminating 0 34m
web-7cc58d88d9-7rvwq 1/1 Terminating 0 34m
web-6c8f6b7f84-tgqcj 0/1 ContainerCreating 0 1s
web-6c8f6b7f84-2kpvr 0/1 Pending 0 0s
web-6c8f6b7f84-2kpvr 0/1 Pending 0 0s
web-6c8f6b7f84-l55tq 0/1 Pending 0 0s
web-6c8f6b7f84-l55tq 0/1 Pending 0 0s
web-6c8f6b7f84-2kpvr 0/1 ContainerCreating 0 0s
web-7cc58d88d9-b786z 1/1 Terminating 0 34m
web-7cc58d88d9-7rvwq 1/1 Terminating 0 34m
web-6c8f6b7f84-2kpvr 0/1 ContainerCreating 0 2s
web-6c8f6b7f84-tgqcj 0/1 ContainerCreating 0 4s
web-7cc58d88d9-b786z 0/1 Terminating 0 34m
web-7cc58d88d9-7rvwq 0/1 Terminating 0 34m
web-7cc58d88d9-b786z 0/1 Terminating 0 34m
web-7cc58d88d9-b786z 0/1 Terminating 0 34m
web-7cc58d88d9-7rvwq 0/1 Terminating 0 34m
web-7cc58d88d9-7rvwq 0/1 Terminating 0 34m
web-6c8f6b7f84-l55tq 0/1 Pending 0 8s
web-6c8f6b7f84-l55tq 0/1 ContainerCreating 0 8s
web-6c8f6b7f84-l55tq 0/1 ContainerCreating 0 9s
web-6c8f6b7f84-2kpvr 0/1 Running 0 44s
web-6c8f6b7f84-tgqcj 0/1 Running 0 48s
web-6c8f6b7f84-l55tq 0/1 Running 0 50s
web-6c8f6b7f84-2kpvr 1/1 Running 0 61s
web-7cc58d88d9-xqdqx 1/1 Terminating 0 35m
web-6c8f6b7f84-tgqcj 1/1 Running 0 62s
web-7cc58d88d9-xqdqx 1/1 Terminating 0 35m
web-7cc58d88d9-xqdqx 0/1 Terminating 0 35m
web-6c8f6b7f84-l55tq 1/1 Running 0 63s
web-7cc58d88d9-xqdqx 0/1 Terminating 0 35m
web-7cc58d88d9-xqdqx 0/1 Terminating 0 35m
滚动升级大致意思是先启动新pod,然后替换旧的pod
这里执行了Up down这个过程,这里用到replicaset,这个控制器我们平时一般不会去使用,这个控制器由deployment创建出来的,deployment就利用了replicaset来实现滚动更新机制
[root@k8s-master ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
web-6c8f6b7f84 3 3 3 14m
web-7cc58d88d9 0 0 0 48m
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
web 3/3 3 3 47m
[root@k8s-master ~]# kubectl describe deployment web
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 48m deployment-controller Scaled up replica set web-7cc58d88d9 to 3
Normal ScalingReplicaSet 13m deployment-controller Scaled up replica set web-6c8f6b7f84 to 1
Normal ScalingReplicaSet 13m deployment-controller Scaled down replica set web-7cc58d88d9 to 1
Normal ScalingReplicaSet 13m deployment-controller Scaled up replica set web-6c8f6b7f84 to 3
Normal ScalingReplicaSet 12m deployment-controller Scaled down replica set web-7cc58d88d9 to 0
第四步:水平扩缩容(启动多实例,提高并发)
第四步:回滚(发布失败恢复正常版本)
[root@k8s-master ~]# kubectl rollout history deployment/web
deployment.apps/web
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@k8s-master ~]# kubectl rollout undo deployment/web
deployment.apps/web rolled back
[root@k8s-master ~]# kubectl get replicaset -w
NAME DESIRED CURRENT READY AGE
web-6c8f6b7f84 3 3 3 29m
web-7cc58d88d9 0 0 0 63m
web-7cc58d88d9 0 0 0 64m
web-7cc58d88d9 1 0 0 64m
web-6c8f6b7f84 1 3 3 30m
web-7cc58d88d9 1 0 0 64m
web-7cc58d88d9 3 0 0 64m
web-6c8f6b7f84 1 3 3 30m
web-6c8f6b7f84 1 1 1 30m
web-7cc58d88d9 3 1 0 64m
web-7cc58d88d9 3 1 0 64m
web-7cc58d88d9 3 3 0 64m
web-7cc58d88d9 3 3 1 64m
web-6c8f6b7f84 0 1 1 30m
web-6c8f6b7f84 0 1 1 30m
web-7cc58d88d9 3 3 2 64m
web-6c8f6b7f84 0 0 0 30m
web-7cc58d88d9 3 3 3 64m
^C[root@k8s-master ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
web-6c8f6b7f84 0 0 0 30m
web-7cc58d88d9 3 3 3 64m
Deployment:ReplicaSet
ReplicaSet控制器用途:
• Pod副本数量管理,不断对比当前Pod数量与期望Pod数量
• Deployment每次发布都会创建一个RS作为记录,用于实现回滚
kubectl get rs #查看RS记录
kubectl rollout history deployment web #版本对应RS记录
Replicaset后面有个死循环,一直判断当前的容器数量是不是预定义的,也就是管理pod副本的数量,记录每次deployment的每次发布版本,用于实现回滚,作为deployment回滚的一个记录。
[root@k8s-master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
web-6c8f6b7f84 0 0 0 40m
web-7cc58d88d9 3 3 3 74m