Kubernetes之Controller组件深入

网友投稿 584 2022-09-08

Kubernetes之Controller组件深入

Kubernetes之Controller组件深入

文章目录

​​Controller​​

​​ReplicationController​​

​​概念​​​​创建 controllers-replication.yaml​​​​kubectl apply -f controllers-replication.yaml [启动]​​​​kubectl get pods -o wide [查看pod状态变化]​​​​kubectl delete pod {pod_name} [删除pod]​​

​​客户端:执行删除某一个pod的命令​​​​客户端:实时查看当前所有pod的变换​​

​​kubectl scale rc nginx --replicas=5 [修改replicas副本数量]​​

​​ReplicaSet​​

​​概述​​

​​Deployments​​

​​创建 controllers-nginx-deployment.yaml 文件​​​​kubectl apply -f controllers-nginx-deployment.yaml [启动]​​​​kubectl get deployments [获取所有的deployments]​​​​kubectl get deployments -o wide [展示deployment的详情]​​​​kubectl rollout status deployment.v1.apps/{deployment_name} [查看deployment的推送状态]​​​​kubectl get rs [查看deployment的副本数量]​​​​kubectl get rs -o wide [查看deployment的副本数量详情]​​​​kubectl get pods --show-labels [查看pod 同时显示pod的label标签]​​​​kubectl describe deployments [查看deployments详情]​​​​kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 [镜像版本更新]​​

​​kubectl delete deployment {deployment-name} [删除deployment-name下的pod]​​

Controller

ReplicationController

概念

​​controllers-replication.yaml

apiVersion: v1kind: ReplicationControllermetadata: name: nginxspec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80

备注:配置文件说明

1.kind: ReplicationController :组件类型为 ReplicationController2.replicas: 3 副本数量,保持几个pod运行并可用3.template: 模板,下面所有的属性都是基于创建的pod以及内部容器的模板4.template通过labels 去管理 pod5.selector这里要注意下:app的值:nginx,必须在下面的labels之中app有定义的值nginx

kubectl apply -f controllers-replication.yaml [启动]

[root@manager-node demo]# kubectl apply -f controllers-replication.yaml replicationcontroller/nginx created

kubectl get pods -o wide [查看pod状态变化]

[root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 0/1 ContainerCreating 0 12s worker01-node nginx-764f2 0/1 ContainerCreating 0 12s worker01-node nginx-l25wp 0/1 ContainerCreating 0 12s worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 0/1 ContainerCreating 0 15s worker01-node nginx-764f2 0/1 ContainerCreating 0 15s worker01-node nginx-l25wp 1/1 Running 0 15s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 0/1 ContainerCreating 0 17s worker01-node nginx-764f2 1/1 Running 0 17s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 17s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 0/1 ContainerCreating 0 18s worker01-node nginx-764f2 1/1 Running 0 18s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 18s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 0/1 ContainerCreating 0 19s worker01-node nginx-764f2 1/1 Running 0 19s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 19s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 0/1 ContainerCreating 0 20s worker01-node nginx-764f2 1/1 Running 0 20s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 20s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 1/1 Running 0 22s 192.168.101.14 worker01-node nginx-764f2 1/1 Running 0 22s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 22s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 1/1 Running 0 23s 192.168.101.14 worker01-node nginx-764f2 1/1 Running 0 23s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 23s 192.168.38.75 worker02-node [root@manager-node demo]#

备注说明

1.我们可以看到 三个pod已经被创建,2.下面做一个试验,加入我删除一个pod,看一下会有什么变化

kubectl delete pod {pod_name} [删除pod]

试验说明

1.因为我们在上面ReplicationController的介绍中已经提到过,它可以维护固定的几个运行并可用的pod 那是不是意味着:一旦我删除一个pod,也会自动的创建一个新的pod呢?2.我们在主节点上打开两个客户端,一个客户端用于实时查看当前所有pod的变换 另外一个客户端,我们去执行删除某一个pod的命令;

客户端:执行删除某一个pod的命令

[root@manager-node demo]# kubectl get pod -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-2pfcs 1/1 Running 0 5m45s 192.168.101.14 worker01-node nginx-764f2 1/1 Running 0 5m45s 192.168.101.13 worker01-node nginx-l25wp 1/1 Running 0 5m45s 192.168.38.75 worker02-node [root@manager-node demo]# kubectl delete pod nginx-2pfcspod "nginx-2pfcs" deleted[root@manager-node demo]#

客户端:实时查看当前所有pod的变换

[root@manager-node ~]# kubectl get pod -wNAME READY STATUS RESTARTS AGEnginx-2pfcs 1/1 Running 0 5m26snginx-764f2 1/1 Running 0 5m26snginx-l25wp 1/1 Running 0 5m26snginx-2pfcs 1/1 Terminating 0 6m6snginx-r8t77 0/1 Pending 0 0snginx-r8t77 0/1 Pending 0 0snginx-r8t77 0/1 ContainerCreating 0 0snginx-2pfcs 0/1 Terminating 0 6m7snginx-r8t77 0/1 ContainerCreating 0 1snginx-2pfcs 0/1 Terminating 0 6m13snginx-2pfcs 0/1 Terminating 0 6m13snginx-r8t77 1/1 Running 0 7s

说明

1.在我们执行上面的删除pod:nginx-2pfcs的命令之后,同时Pod:nginx-r8t77 也同时被创建出来了;2.也就证明了ReplicationController 通过Replica属性 可以维护固定的运行并可用的Pod的数量;

kubectl scale rc nginx --replicas=5 [修改replicas副本数量]

[root@manager-node demo]# kubectl scale rc nginx --replicas=5replicationcontroller/nginx scaled[root@manager-node demo]# kubectl get pods -wNAME READY STATUS RESTARTS AGEnginx-764f2 1/1 Running 0 18mnginx-l25wp 1/1 Running 0 18mnginx-pcb7t 0/1 ContainerCreating 0 5snginx-r8t77 1/1 Running 0 12mnginx-xfwqv 0/1 ContainerCreating 0 5snginx-pcb7t 1/1 Running 0 6snginx-xfwqv 1/1 Running 0 6s

备注说明

1.很明显,新增了两个pod

ReplicaSet

概述

1.ReplicaSet 实际上包含ReplicationController的所有功能; ReplicationController 这个是逐渐的被摒弃的一个组件;2.ReplicaSet 在selector选择器的使用方面更加灵活; ReplicationController 只支持一个选择器的选择(等于) ReplicaSet 可以支持多个选择器,类似in某个选择,或者not in 某个选择器3.因为我们知道Deployment可以进行管理ReplicaSet 所以ReplicaSet一般与Deployment一起使用; 具体可以参考Deployment的讲解;

Deployments

创建 controllers-nginx-deployment.yaml 文件

apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-deployment labels: app: nginxspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80

kubectl apply -f controllers-nginx-deployment.yaml [启动]

[root@manager-node demo]# kubectl apply -f controllers-nginx-deployment.yaml deployment.apps/nginx-deployment created[root@manager-node demo]#

查看

[root@manager-node demo]# kubectl get podsNAME READY STATUS RESTARTS AGEnginx-deployment-6dd86d77d-962t6 0/1 Pending 0 2m18snginx-deployment-6dd86d77d-jmj9v 0/1 Pending 0 2m18snginx-deployment-6dd86d77d-vh8pl 0/1 Pending 0 2m18s[root@manager-node demo]#

1.这里大家注意下,如果是Pending(待定)状态,有可能是你集群中的子节点服务器没有正常运行,导致待分配

[root@manager-node demo]# kubectl get podsNAME READY STATUS RESTARTS AGEnginx-deployment-6dd86d77d-962t6 1/1 Running 0 4m11snginx-deployment-6dd86d77d-jmj9v 1/1 Running 0 4m11snginx-deployment-6dd86d77d-vh8pl 1/1 Running 0 4m11s[root@manager-node demo]#

kubectl get deployments [获取所有的deployments]

[root@manager-node demo]# kubectl get deploymentsNAME READY UP-TO-DATE AVAILABLE AGEnginx-deployment 3/3 3 3 5m21s[root@manager-node demo]#

字段描述

kubectl get deployments -o wide [展示deployment的详情]

[root@manager-node demo]# kubectl get deployments -o wideNAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTORnginx-deployment 3/3 3 3 7m38s nginx nginx:1.7.9 app=nginx[root@manager-node demo]#

kubectl rollout status deployment.v1.apps/{deployment_name} [查看deployment的推送状态]

[root@manager-node demo]# kubectl rollout status deployment.v1.apps/nginx-deploymentdeployment "nginx-deployment" successfully rolled out[root@manager-node demo]#

kubectl get rs [查看deployment的副本数量]

[root@manager-node demo]# kubectl get rsNAME DESIRED CURRENT READY AGEnginx-deployment-6dd86d77d 3 3 3 14m[root@manager-node demo]#

kubectl get rs -o wide [查看deployment的副本数量详情]

[root@manager-node demo]# kubectl get rs -o wideNAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTORnginx-deployment-6dd86d77d 3 3 3 56m nginx nginx:1.7.9 app=nginx,pod-template-hash=6dd86d77d[root@manager-node demo]#

kubectl get pods --show-labels [查看pod 同时显示pod的label标签]

[root@manager-node demo]# kubectl get pods --show-labelsNAME READY STATUS RESTARTS AGE LABELSnginx-deployment-6dd86d77d-962t6 1/1 Running 0 46m app=nginx,pod-template-hash=6dd86d77dnginx-deployment-6dd86d77d-jmj9v 1/1 Running 0 46m app=nginx,pod-template-hash=6dd86d77dnginx-deployment-6dd86d77d-vh8pl 1/1 Running 0 46m app=nginx,pod-template-hash=6dd86d77d[root@manager-node demo]#

kubectl describe deployments [查看deployments详情]

[root@manager-node demo]# kubectl describe deploymentsName: nginx-deploymentNamespace: defaultCreationTimestamp: Mon, 06 Jan 2020 03:34:51 +0000Labels: app=nginxAnnotations: deployment.kubernetes.io/revision: 1 kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"d...Selector: app=nginxReplicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailableStrategyType: RollingUpdateMinReadySeconds: 0RollingUpdateStrategy: 25% max unavailable, 25% max surgePod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.7.9 Port: 80/TCP Host Port: 0/TCP Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailableOldReplicaSets: NewReplicaSet: nginx-deployment-6dd86d77d (3/3 replicas created)Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 59m deployment-controller Scaled up replica set nginx-deployment-6dd86d77d to 3[root@manager-node demo]#

kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 [镜像版本更新]

[root@manager-node demo]# kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 deployment.extensions/nginx-deployment image updated[root@manager-node demo]#

备注

1.nginx-deployment这里是deployments的名字,nginx=nginx:1.9.1 镜像版本设置为1.9.1

[root@manager-node demo]# kubectl get rs -o wideNAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTORnginx-deployment-6dd86d77d 0 0 0 123m nginx nginx:1.7.9 app=nginx,pod-template-hash=6dd86d77dnginx-deployment-784b7cc96d 3 3 3 53m nginx nginx:1.9.1 app=nginx,pod-template-hash=784b7cc96d[root@manager-node demo]#

kubectl delete deployment {deployment-name} [删除deployment-name下的pod]

[root@manager-node ~]# kubectl get podsNAME READY STATUS RESTARTS AGEwhoami-deployment-678b64444d-2htx6 1/1 Running 0 143mwhoami-deployment-678b64444d-6bgrm 1/1 Running 3 13hwhoami-deployment-678b64444d-7h2lm 1/1 Running 0 143mwhoami-deployment-678b64444d-nsh2r 1/1 Running 0 143mwhoami-deployment-678b64444d-sxwfl 1/1 Running 3 13h[root@manager-node ~]#

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

上一篇:excel数据处理一:巧妙使用openpyxl提取、筛选数据(python openpyxl读取excel)
下一篇:2021Docker容器技术全解-nginx和apache共享首页目录实验(8)
相关文章

 发表评论

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