尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

k8s中的pod管理

k8s中的pod管理 目录1.集群基本操作2.kubectl命令使用3.利用控制器实现版本更替3.1建立控制器和svc3.2更新业务版本3.3版本回退4.利用yaml文件声明资源4.1在pod中运行多容器4.2在pod运行主机中暴漏端口4.3在pod中指定变量4.4选择运行节点4.5共享宿主机网络4.6三种资源限制BestEffortBurstableGuaranteed​编辑4.7容器重启规则Always OnFailure Never 5.pod的生命周期5.1init容器5.2Livness存活探针5.3存活探针livness5.4readness 就绪探针1.集群基本操作查看,创建删除命名空间创建与查看pod当pod出现问题可以使用kubectl describe查看上传镜像到harbor仓库使用yaml生成pod2.kubectl命令使用创建查看删除deploymentscreate修改replica数量使用edit方式edit精准非交互修改参数(patch)端口暴露expose查看访问记录logs默认进入第一个容器用-c指定容器名可以精确控制。attach进入pods可以-c指定容器exec复制pod的文件到宿主机上多容器情况也可以用-c指定cpdeploment重启与版本更新rollout修改replica数量可以有很多方法例如scaleattachedit修改yamlscale打标签--show-labelslabel3.利用控制器实现版本更替3.1建立控制器和svc3.2更新业务版本3.3版本回退声明式添加change-cause4.利用yaml文件声明资源4.1在pod中运行多容器4.2在pod运行主机中暴漏端口4.3在pod中指定变量访问node2的ip4.4选择运行节点4.5共享宿主机网络4.6三种资源限制BestEffortBurstableGuaranteed4.7容器重启规则Always OnFailure Never 5.pod的生命周期5.1init容器[rootk8s-master pod]# kubectl run webserver --image myapp:v1 --dry-runclient -o yaml init-example.yml [rootk8s-master pod]# vim init-example.yml apiVersion: v1 kind: Pod metadata: labels: run: webserver name: webserver spec: initContainers: - name: busybox image: busybox:latest command: - /bin/sh - -c - until test -e /testfile;do echo wating for myservice; sleep 2;done containers: - image: myapp:v1 name: webserver restartPolicy: Always [rootk8s-master pod]# watch -n 1 kubectl get pods -o wide [rootk8s-master pod]# kubectl apply -f init-example.yml pod/webserver created [rootk8s-master pod]# kubectl exec -it pods/webserver -c busybox -- /bin/sh / # / # touch /testfile5.2Livness存活探针没有存活探针时[rootk8s-master pod]# kubectl run testpod --image myapp:v1 --dry-runclient -o yaml livness-example.yaml [rootk8s-master pod]# vim livness-example.yaml apiVersion: v1 kind: Pod metadata: labels: run: webserver name: webserver spec: containers: - image: myapp:v1 name: webserver command: [/bin/sh, -c] args: - | nginx -g daemon off; sleep 10000 restartPolicy: Always [rootk8s-master pod]# kubectl apply -f livness-example.yaml #监控程序 [rootk8s-master pod]# watch -n 1 kubectl get pods -o wide #测试操作 [rootk8s-master pod]# kubectl exec -it pods/webserver -c webserver -- /bin/sh # nginx -s stop #查看pod的状态仍然是runing但是访问此pod时访问失败 [rootk8s-node2 ~]# curl 10.244.5.47 curl: (7) Failed to connect to 10.244.5.47 port 80: 拒绝连接5.3存活探针livness[rootk8s-master pod]# kubectl delete -f livness-example.yaml --force kind: Pod metadata: labels: run: webserver name: webserver spec: containers: - image: myapp:v1 name: testpod command: [/bin/sh, -c] args: - | nginx -g daemon off; sleep 10000 livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 3 periodSeconds: 1 timeoutSeconds: 1 restartPolicy: Always [rootk8s-master pod]# kubectl get pods -o wide -w [rootk8s-master pod]# kubectl exec -it pods/webserver -c webserver -- /bin/sh / # nginx -s stop 2026/08/23 03:59:45 [notice] 15#15: signal process started / # exit [rootk8s-master pod]# curl 10.244.5.66 curl: (7) Failed to connect to 10.244.5.66 port 80: 拒绝连接 [rootk8s-master pod]# curl 10.244.5.66 curl: (7) Failed to connect to 10.244.5.66 port 80: 拒绝连接 [rootk8s-master pod]# curl 10.244.5.66 curl: (7) Failed to connect to 10.244.5.66 port 80: 拒绝连接 [rootk8s-master pod]# curl 10.244.5.66 curl: (7) Failed to connect to 10.244.5.66 port 80: 拒绝连接 [rootk8s-master pod]# curl 10.244.5.66 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/a5.4readness 就绪探针没有就绪探针情况[rootk8s-master pod]# vim readness-example.yml apiVersion: v1 kind: Pod metadata: labels: run: webserver name: webserver spec: containers: - image: myapp:v1 name: webserver restartPolicy: Always --- apiVersion: v1 kind: Service metadata: labels: run: webserver name: webserver spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: webserver [rootk8s-master pod]# kubectl apply -f readness-example.yml pod/webserver unchanged service/webserver created [rootk8s-master pod]# kubectl describe svc webserver Name: webserver Namespace: default Labels: runwebserver Annotations: none Selector: runwebserver Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.102.162.217 IPs: 10.102.162.217 Port: unset 80/TCP TargetPort: 80/TCP Endpoints: 10.244.5.67:80 Session Affinity: None Internal Traffic Policy: Cluster Events: none #删除默认发布文件 [rootk8s-master ~]# kubectl exec -it pods/webserver -c webserver -- /bin/sh / # cd /usr/share/nginx/ /usr/share/nginx # ls html /usr/share/nginx # cd html/ /usr/share/nginx/html # ls 50x.html index.html /usr/share/nginx/html # rm -fr index.html /usr/share/nginx/html # ls 50x.html /usr/share/nginx/html # #验证是否在访问service时endpoints中被下架 [rootk8s-master pod]# kubectl describe svc webserver Name: webserver Namespace: default Labels: runwebserver Annotations: none Selector: runwebserver Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.102.162.217 IPs: 10.102.162.217 Port: unset 80/TCP TargetPort: 80/TCP Endpoints: 10.244.5.67:80 #还在 Session Affinity: None Internal Traffic Policy: Cluster Events: none #业务问题 [rootk8s-master pod]# curl 10.102.162.217 html headtitle403 Forbidden/title/head body bgcolorwhite centerh1403 Forbidden/h1/center hrcenternginx/1.12.2/center /body /html有就绪探针情况[rootk8s-master pod]# vim readness-example.yml apiVersion: v1 kind: Pod metadata: labels: run: webserver name: webserver spec: containers: - image: myapp:v1 name: webserver readinessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 3 periodSeconds: 2 timeoutSeconds: 1 restartPolicy: Always --- apiVersion: v1 kind: Service metadata: labels: run: webserver name: webserver spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: webserver [rootk8s-master pod]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES webserver 1/1 Running 0 7s 10.244.5.69 k8s-node2 none none [rootk8s-master pod]# kubectl describe svc webserver Name: webserver Namespace: default Labels: runwebserver Annotations: none Selector: runwebserver Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.110.248.237 IPs: 10.110.248.237 Port: unset 80/TCP TargetPort: 80/TCP Endpoints: 10.244.5.69:80 Session Affinity: None Internal Traffic Policy: Cluster Events: none #复现问题 /usr/share/nginx/html # command terminated with exit code 137 [rootk8s-master ~]# / # rm -fr /usr/share/nginx/html/index.html / # echo timinglee /usr/share/nginx/html/index.html / #
返回列表