K8S基础:通过多种方式管理Pod

网友投稿 262 2022-10-21

K8S基础:通过多种方式管理Pod

1、kubectl修改pod nginx默认页面

#命令行进入pod [root@master server]# kubectl run mynginx --image=nginx pod/mynginx created [root@master server]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx 1/1 Running 0 3m3s 10.233.82.20 node01 [root@master server]# kubectl exec -it mynginx -- /bin/bash root@mynginx:/# ls bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var boot docker-entrypoint.d etc root@mynginx:/#echo "leoheng-test-page"> /usr/share/nginx/html/index.html root@mynginx:/#exit [root@master server]# curl 10.233.82.20 leoheng-test-page

2、多容器运行在同一个Pod

#创建yaml文件 [root@master server]# vim myapp.yaml apiVersion: v1 kind: Pod metadata: labels: run: myapp name: myapp #定义pod运行的容器 spec: containers: - image: nginx name: nginx01 - image: tomcat:8.5.68 name: tomcat #通过yaml文件创建多容器pod [root@master server]# kubectl apply -f myapp.yaml pod/myapp created [root@master server]# kubectl get pod NAME READY STATUS RESTARTS AGE myapp 2/2 Running 0 93s #查看pod创建详细信息、以及容器分配的信息 [root@master server]# kubectl describe pod myapp Name: myapp Namespace: default Priority: 0 Node: master/172.18.0.2 Start Time: Wed, 13 Apr 2022 12:01:48 +0800 Labels: run=myapp Annotations: cni.projectcalico.org/containerID: ac4ad7372e6bbeca7fa56f24fdd912d9fb3050a62a12658a916e7adaefb194f6 cni.projectcalico.org/podIP: 10.233.70.12/32 cni.projectcalico.org/podIPs: 10.233.70.12/32 Status: Running IP: 10.233.70.12 IPs: IP: 10.233.70.12 Containers: nginx01: Container ID: docker://47f75dbe49635a90af2915eb7bfedcd7dcc7c7b0e892a08e73d367814f618bf9 Image: nginx Image ID: docker-pullable://nginx@sha256:2275af0f20d71b293916f1958f8497f987b8d8fd8113df54635f2a5915002bf1 Port: Host Port: State: Running Started: Wed, 13 Apr 2022 12:02:15 +0800 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-h96jl (ro) tomcat: Container ID: docker://4625bba94fab89d020bbd75311b45119fb2c472d937ad6f24c04745f997dafec Image: tomcat:8.5.68 Image ID: docker-pullable://tomcat@sha256:a266dd222864de2fe72e0464e6d91c406a687c861bb72a07218e6d7c89fe1d3e Port: Host Port: State: Running Started: Wed, 13 Apr 2022 12:03:20 +0800 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-h96jl (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: kube-api-access-h96jl: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: DownwardAPI: true QoS Class: BestEffort Node-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 105s default-scheduler Successfully assigned default/myapp to master Normal Pulling 104s kubelet Pulling image "nginx" Normal Pulled 81s kubelet Successfully pulled image "nginx" in 23.109090468s Normal Created 78s kubelet Created container nginx01 Normal Started 78s kubelet Started container nginx01 Normal Pulling 78s kubelet Pulling image "tomcat:8.5.68" Normal Pulled 21s kubelet Successfully pulled image "tomcat:8.5.68" in 57.020508928s Normal Created 13s kubelet Created container tomcat Normal Started 13s kubelet Started container tomcat [root@master server]#

3、deployment管理pod

#创建pod放在新的deployment里 [root@master server]# kubectl create deployment mytomcat --image=tomcat:8.5.68 deployment.apps/mytomcat created [root@master server]# kubectl get pod NAME READY STATUS RESTARTS AGE mytomcat-6f5f895f4f-t9fh2 1/1 Running 0 4m15s #删除pod演示宕机操作 [root@master server]# kubectl delete pod mytomcat-6f5f895f4f-t9fh2 pod "mytomcat-6f5f895f4f-t9fh2" deleted [root@master server]# kubectl get pod NAME READY STATUS RESTARTS AGE mytomcat-6f5f895f4f-95hjs 1/1 Running 0 3s [root@master server]# #删除deployment,彻底删除pod [root@master server]# kubectl get pod NAME READY STATUS RESTARTS AGE mytomcat-6f5f895f4f-95hjs 1/1 Running 0 3s [root@master server]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE mytomcat 1/1 1 1 7m31s [root@master server]# kubectl delete deploy mytomcat deployment.apps "mytomcat" deleted [root@master server]# kubectl get pod No resources found in default namespace. [root@master server]#

4、通过副本数实现pod高可用

#创建3副本的pod [root@master server]# kubectl create deploy mynginx --image=nginx --replicas=3 deployment.apps/mynginx created [root@master server]# kubectl get pod NAME READY STATUS RESTARTS AGE mynginx-5b686ccd46-2mpnf 1/1 Running 0 8s mynginx-5b686ccd46-d97p5 1/1 Running 0 8s mynginx-5b686ccd46-t6dgt 1/1 Running 0 8s [root@master server]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE mynginx 3/3 3 3 12s #查看容器分布的情况 [root@master server]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-2mpnf 1/1 Running 0 2m43s 10.233.82.21 node01 mynginx-5b686ccd46-d97p5 1/1 Running 0 2m43s 10.233.82.22 node01 mynginx-5b686ccd46-t6dgt 1/1 Running 0 2m43s 10.233.70.15 master [root@master server]# #模拟某个容器不可用 [root@master server]# kubectl delete pod mynginx-5b686ccd46-t6dgt pod "mynginx-5b686ccd46-t6dgt" deleted [root@master server]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-2mpnf 1/1 Running 0 3m55s 10.233.82.21 node01 mynginx-5b686ccd46-d97p5 1/1 Running 0 3m55s 10.233.82.22 node01 mynginx-5b686ccd46-tds4m 1/1 Running 0 6s 10.233.70.16 master [root@master server]#

5、通过deployment实现扩缩容

#扩容 [root@master server]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE mynginx 3/3 3 3 6m37s [root@master server]# kubectl get pod NAME READY STATUS RESTARTS AGE mynginx-5b686ccd46-2mpnf 1/1 Running 0 6m47s mynginx-5b686ccd46-d97p5 1/1 Running 0 6m47s mynginx-5b686ccd46-tds4m 1/1 Running 0 2m58s [root@master server]# kubectl scale deploy/mynginx --replicas=5 deployment.apps/mynginx scaled [root@master server]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-2mpnf 1/1 Running 0 8m25s 10.233.82.21 node01 mynginx-5b686ccd46-6tzj9 1/1 Running 0 28s 10.233.70.17 master mynginx-5b686ccd46-d97p5 1/1 Running 0 8m25s 10.233.82.22 node01 mynginx-5b686ccd46-f8kh6 1/1 Running 0 28s 10.233.82.23 node01 mynginx-5b686ccd46-tds4m 1/1 Running 0 4m36s 10.233.70.16 master [root@master server]# #缩容 [root@master server]# kubectl scale deploy/mynginx --replicas=2 deployment.apps/mynginx scaled [root@master server]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-6tzj9 1/1 Running 0 77s 10.233.70.17 master mynginx-5b686ccd46-tds4m 1/1 Running 0 5m25s 10.233.70.16 master [root@master server]# #通过编辑deployment实现缩容 [root@master server]# kubectl edit deploy mynginx spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: app: mynginx [root@master server]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-tds4m 1/1 Running 0 7m48s 10.233.70.16 master [root@master server]# #模拟主机宕机故障转移,停机node01 [root@master ~]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-6qgtc 1/1 Running 0 24m 10.233.82.28 node01 mynginx-5b686ccd46-ctjhl 1/1 Running 0 9m45s 10.233.82.34 node01 mynginx-5b686ccd46-gzkfs 1/1 Running 0 25m 10.233.70.18 master #5分钟后查看pod情况 [root@master ~]# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-k4sg4 1/1 Running 1 24m 10.233.82.29 node02 mynginx-5b686ccd46-w8jl2 1/1 Running 1 9m45s 10.233.82.30 node02 mynginx-5b686ccd46-gzkfs 1/1 Running 0 25m 10.233.70.18 master

6、pod的版本控制

#滚动更新,指定镜像更新deployment里的容器版本 [root@master ~]# kubectl set image deploy/mynginx nginx=nginx:1.16.1 --record deployment.apps/mynginx image updated #每秒刷新pod状态,查看版本更新过程 [root@master server]# watch -n 1 kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mynginx-5b686ccd46-4cwr8 1/1 Running 0 3m7s 10.233.82.45 node01 mynginx-5b686ccd46-r82j6 1/1 Running 0 3m38s 10.233.82.43 node01 mynginx-5b686ccd46-zdfrb 1/1 Running 0 3m7s 10.233.82.44 node01 mynginx-f9cbbdc9f-vjszv 0/1 ContainerCreating 0 20s node01 #版本回滚,查看历史版本记录 [root@master server]# kubectl rollout history deploy/mynginx deployment.apps/mynginx REVISION CHANGE-CAUSE 1 2 kubectl set image deploy/mynginx nginx=nginx:1.16.1 --record=true [root@master server]# #查看某个版本详情 [root@master server]# kubectl rollout history deploy/mynginx --revision=2 deployment.apps/mynginx with revision #2 Pod Template: Labels: app=mynginx pod-template-hash=f9cbbdc9f Annotations: kubernetes.io/change-cause: kubectl set image deploy/mynginx nginx=nginx:1.16.1 --record=true Containers: nginx: Image: nginx:1.16.1 Port: Host Port: Environment: Mounts: Volumes: [root@master server]# #指定某个版本进行回滚 [root@master server]# kubectl rollout undo deploy/mynginx --to-revision=1 deployment.apps/mynginx rolled back

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:K8S基础:服务网络service/ingress
下一篇:Java9新特性Module模块化编程示例演绎
相关文章

 发表评论

暂时没有评论,来抢沙发吧~