小程序容器助力企业在金融与物联网领域实现高效合规运营,带来的新机遇与挑战如何管理?
857
2022-11-29
Kubernetes 工作负载控制器Deployment和Replicaset
Deployment:介绍
应用场景:网站、API、微服务
Deployment:使用流程
项目生命周期
Deployment:部署
第一步:部署镜像
• kubectl apply -f xxx.yaml
• kubectl create deployment web --image=nginx:1.15
[root@k8s-master ~]# cat deployment.yaml apiVersion: apps/v1kind: Deploymentmetadata: name: web namespace: defaultspec: 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: path: /index.html port: 80 readinessProbe: 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 wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESweb-7cc58d88d9-7rvwq 1/1 Running 0 22m 10.244.169.149 k8s-node2
第二步:暴露服务
[root@k8s-master ~]# cat service.yaml apiVersion: v1kind: Servicemetadata: name: web namespace: defaultspec: 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 svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1
现在可以根据任意node节点的IP+30866就可以访问到pod了
第三步 Deployment:滚动升级
(更新镜像三种方式)
• kubectl apply -f xxx.yaml
• kubectl set image deployment/web nginx=nginx:1.16
• kubectl edit deployment/webkubec
image: nginx:1.16
滚动更新查看replicaset状态
[root@k8s-master ~]# kubectl get replicaset -wNAME DESIRED CURRENT READY AGEweb-7cc58d88d9 3 3 3 32mweb-6c8f6b7f84 1 0 0 0sweb-7cc58d88d9 1 3 3 34mweb-6c8f6b7f84 1 0 0 0sweb-6c8f6b7f84 3 0 0 0sweb-7cc58d88d9 1 3 3 34mweb-6c8f6b7f84 3 1 0 1sweb-7cc58d88d9 1 1 1 34mweb-6c8f6b7f84 3 1 0 1sweb-6c8f6b7f84 3 3 0 1sweb-6c8f6b7f84 3 3 1 62sweb-7cc58d88d9 0 1 1 35mweb-7cc58d88d9 0 1 1 35mweb-7cc58d88d9 0 0 0 35mweb-6c8f6b7f84 3 3 2 62sweb-6c8f6b7f84 3 3 3 64s^C[root@k8s-master ~]# kubectl get replicasetNAME DESIRED CURRENT READY AGEweb-6c8f6b7f84 3 3 3 4m55sweb-7cc58d88d9 0 0 0 39m
滚动更新查看ep状态
[root@k8s-master ~]# kubectl get ep -wNAME ENDPOINTS AGEkubernetes 192.168.179.102:6443 32dweb 10.244.169.148:80,10.244.169.149:80,10.244.36.82:80 7m33sweb 10.244.169.149:80,10.244.36.82:80 9m2sweb 10.244.36.82:80 9m2sweb 10.244.36.82:80 9m47sweb 10.244.36.82:80 9m49sweb 10.244.36.82:80 9m52sweb 10.244.36.82:80,10.244.36.83:80 10mweb 10.244.36.83:80 10mweb 10.244.169.150:80,10.244.36.83:80 10mweb 10.244.169.150:80,10.244.169.151:80,10.244.36.83:80 10m[root@k8s-master ~]# kubectl get epNAME ENDPOINTS AGEkubernetes 192.168.179.102:6443 32dweb 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 -wNAME READY STATUS RESTARTS AGEweb-7cc58d88d9-7rvwq 1/1 Running 0 33mweb-7cc58d88d9-b786z 1/1 Running 0 33mweb-7cc58d88d9-xqdqx 1/1 Running 0 33mweb-6c8f6b7f84-tgqcj 0/1 Pending 0 0sweb-6c8f6b7f84-tgqcj 0/1 Pending 0 0sweb-7cc58d88d9-b786z 1/1 Terminating 0 34mweb-7cc58d88d9-7rvwq 1/1 Terminating 0 34mweb-6c8f6b7f84-tgqcj 0/1 ContainerCreating 0 1sweb-6c8f6b7f84-2kpvr 0/1 Pending 0 0sweb-6c8f6b7f84-2kpvr 0/1 Pending 0 0sweb-6c8f6b7f84-l55tq 0/1 Pending 0 0sweb-6c8f6b7f84-l55tq 0/1 Pending 0 0sweb-6c8f6b7f84-2kpvr 0/1 ContainerCreating 0 0sweb-7cc58d88d9-b786z 1/1 Terminating 0 34mweb-7cc58d88d9-7rvwq 1/1 Terminating 0 34mweb-6c8f6b7f84-2kpvr 0/1 ContainerCreating 0 2sweb-6c8f6b7f84-tgqcj 0/1 ContainerCreating 0 4sweb-7cc58d88d9-b786z 0/1 Terminating 0 34mweb-7cc58d88d9-7rvwq 0/1 Terminating 0 34mweb-7cc58d88d9-b786z 0/1 Terminating 0 34mweb-7cc58d88d9-b786z 0/1 Terminating 0 34mweb-7cc58d88d9-7rvwq 0/1 Terminating 0 34mweb-7cc58d88d9-7rvwq 0/1 Terminating 0 34mweb-6c8f6b7f84-l55tq 0/1 Pending 0 8sweb-6c8f6b7f84-l55tq 0/1 ContainerCreating 0 8sweb-6c8f6b7f84-l55tq 0/1 ContainerCreating 0 9sweb-6c8f6b7f84-2kpvr 0/1 Running 0 44sweb-6c8f6b7f84-tgqcj 0/1 Running 0 48sweb-6c8f6b7f84-l55tq 0/1 Running 0 50sweb-6c8f6b7f84-2kpvr 1/1 Running 0 61sweb-7cc58d88d9-xqdqx 1/1 Terminating 0 35mweb-6c8f6b7f84-tgqcj 1/1 Running 0 62sweb-7cc58d88d9-xqdqx 1/1 Terminating 0 35mweb-7cc58d88d9-xqdqx 0/1 Terminating 0 35mweb-6c8f6b7f84-l55tq 1/1 Running 0 63sweb-7cc58d88d9-xqdqx 0/1 Terminating 0 35mweb-7cc58d88d9-xqdqx 0/1 Terminating 0 35m
滚动升级: K8s对Pod升级的默认策略,通过使 用新版本Pod逐步更新旧版本Pod,实现零停机 发布,用户无感知。
滚动升级大致意思是先启动新pod,然后替换旧的pod
这里执行了Up down这个过程,这里用到replicaset,这个控制器我们平时一般不会去使用,这个控制器由deployment创建出来的,deployment就利用了replicaset来实现滚动更新机制
[root@k8s-master ~]# kubectl get replicasetNAME DESIRED CURRENT READY AGEweb-6c8f6b7f84 3 3 3 14mweb-7cc58d88d9 0 0 0 48m[root@k8s-master ~]# kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGEweb 3/3 3 3 47m[root@k8s-master ~]# kubectl describe deployment webEvents: 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
第四步:水平扩缩容(启动多实例,提高并发)
• 修改yaml里replicas值,再apply
• kubectl scale deployment web --replicas=10 注:replicas参数控制Pod副本数量
第四步:回滚(发布失败恢复正常版本)
kubectl rollout history deployment/web # 查看历史发布版本
kubectl rollout undo deployment/web # 回滚上一个版本
kubectl rollout undo deployment/web --to-revision=2 # 回滚历史指定版本
注:回滚是重新部署某一次部署时的状态,即当时版本所有配置
[root@k8s-master ~]# kubectl rollout history deployment/webdeployment.apps/web REVISION CHANGE-CAUSE1
第五步:Deployment:删除
最后,项目下线:
kubectl delete deploy/web
kubectl delete svc/web
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 rsNAME DESIRED CURRENT READY AGEweb-6c8f6b7f84 0 0 0 40mweb-7cc58d88d9 3 3 3 74m
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~