The previous section said that the pod created directly has no self-healing function.
Only pods created by controllers can be self-healing, so what is a controller.
It can be understood that the container is a bare box. Pod has packed the box to facilitate k8s mounting.
How to mount several pods in k8s at a time? These information are specified and managed by the controller.
There are many kinds of controllers. This section practices deployment controllers.
- 20200601 – first draft – Zuo Chengli
- Original address-https://blog.zuolinux.com/2020/06/01/about-controller-deployment.html
We release pods through the deployment controller
# cat nginx.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx replicas: 2 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
In this case:
- Create a controller called nginx deployment, metadata.name appoint
- Create 2 replicated pods, spec.replicas appoint
- spec.selector Specifies how deployment selects managed pods
- spec.template.metadata . labels specifies the label of the pod
- spec.template.spec . containers specifies information about the container
[[email protected] ~]# kubectl apply -f nginx.yaml
[[email protected] ~]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 2/2 2 2 36m
View replica status
[[email protected] ~]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-5bf87f5f59 2 2 2 39m
Desired expected state
Current actual status
View pod information
[[email protected] ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-5bf87f5f59-4phlc 1/1 Running 0 40m app=nginx,pod-template-hash=5bf87f5f59 nginx-deployment-5bf87f5f59-jrzxj 1/1 Running 0 40m app=nginx,pod-template-hash=5bf87f5f59
The meaning of the parameter pod template hash = 5bf87f5f59:
Hash the podtemplate of the replicaset to ensure that the child replicasets of the deployment do not overlap and conflict.
Working principle of controller
The controller is always in an infinite loop to obtain the expected state and the actual state of the managed object.
It’s on the top nginx.yaml For example:
- Replicas: 2 is the expected state of pod
- The deployment controller continuously obtains the actual status of the pod from the etcd
- If the actual state is less than 2, a new pod is created. If the actual state is greater than 2, the redundant pod is deleted
In GitHub’s kubernetes / kubernetes / tree / Master / PKG / controller directory, there are various controllers of k8s, and deployment is one of them.
Change nginx.yaml Of spec.template The content in the report is enough
If you update image: nginx:1.7.9 For image: nginx:1.9.1
Then kubectl apply – F nginx.yaml
View historical versions
[[email protected] ~]# kubectl rollout history deployment.v1.apps/nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 1 <none> 2 <none> 3 <none>
View detailed modification records
[[email protected] ~]# kubectl rollout history deployment.v1.apps/nginx-deployment --revision=1 deployment.apps/nginx-deployment with revision #1 Pod Template: Labels: app=nginx pod-template-hash=5b6f47948 Containers: nginx: Image: nginx:1.7.8 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> [[email protected] ~]# kubectl rollout history deployment.v1.apps/nginx-deployment --revision=2 deployment.apps/nginx-deployment with revision #2 Pod Template: Labels: app=nginx pod-template-hash=678645bf77 Containers: nginx: Image: nginx:1.9.1 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> [[email protected] ~]# kubectl rollout history deployment.v1.apps/nginx-deployment --revision=3 deployment.apps/nginx-deployment with revision #3 Pod Template: Labels: app=nginx pod-template-hash=64d99cdfb6 Containers: nginx: Image: nginx:1.9.8 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none>
Rollback to the specified version — to revision = XXX
[[email protected] ~]# kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=1
Roll back to last modification
kubectl rollout undo deployment.v1.apps/nginx-deployment
Record the command executed each time in the history
[[email protected] ~]# kubectl apply -f nginx.yaml --record
The deployment controller can manage pods, which indirectly manage pods by operating a controller called replicaset.
The deployment controller has a disadvantage. It can only manage stateless pods such as nginx, but cannot manage stateful applications such as mysql. Stateful applications need stateful set controller to manage.
WeChat official account: zuolinux_ COM