k8s study notes (3): pod startup process and practical cases

k8s creates pod

pod startup process

flow chart

Insert image description here

  1. The operation and maintenance personnel issue instructions to kube-apiserver (what do I want to do, what state do I expect things to be in)

  2. The api responds to the command, passes a series of authentication and authorization, stores the pod data in etcd , creates and initializes the deployment resource. (desired state)

  3. Through the list-watch mechanism, the controller monitors the API server to read etcd, discovers new deployment, adds the resource to the internal work queue, and finds that the resource has no associated pod and replicaset. It enables the deployment controller to create the replicaset resource, and then enables the replicaset controller to create it . pod . After all controllers are created, the deployment, replicaset, and pod resource updates are stored in etcd .

  4. The scheduler uses the list-watch mechanism to monitor the api server to read etcd, discover new pods, bind the pod to the appropriate host through host filtering and host scoring rules, and store the binding results in etcd .

  5. Every 20 seconds (can be customized), kubelet obtains a list of pods to be run on its own Node from the apiserver . By comparing it with its own internal cache, it uses the container runtime software (docker or containerd) to pull the image to create a pod.

  6. kube-proxy registers dynamic DNS for newly created pods with CoreOS . Add iptables/ipvs rules to the pod's service for service discovery and load balancing .

  7. The controller compares the current pod state with the user's expected state through the control loop . If the current state is different from the user's expected state, the controller will modify the pod to the user's expected state. If this fails, the pod will be deleted. Then recreate the pod.

Practical case: Creating an nginx pod using a controller

flow chart

Insert image description here

1. Use kubectl to create nginx pod

[root@k8s-master ~]# kubectl create deployment k8s-nginx --image=nginx -r 3

It is equivalent to docker run to start the container, and it also needs to pull the image behind the scenes.

  • create deployment, create a deployment controller
  • k8s-nginx, named deployment controller
  • –image, specifies the image to use
  • -r 3, create three copies, which will be automatically added after deletion

2. View the deployment controller

[root@k8s-master ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
k8s-nginx   3/3     3            3           14m

3. View replica controller

[root@k8s-master ~]# kubectl get replicaset
NAME                   DESIRED   CURRENT   READY   AGE
k8s-nginx-75f95db655   3         3         3       15m

4. View the detailed information of the pod

[root@k8s-master ~]# kubectl get pod -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP              NODE         NOMINATED NODE   READINESS GATES
k8s-nginx-75f95db655-24mfb   1/1     Running   0          17m   10.244.140.73   k8s-node-2   <none>           <none>
k8s-nginx-75f95db655-fl5cc   1/1     Running   0          17m   10.244.109.81   k8s-node-1   <none>           <none>
k8s-nginx-75f95db655-txf25   1/1     Running   0          17m   10.244.109.80   k8s-node-1   <none>           <none>

Parameter meaning

status

  • Running, normal operation
  • Terminating, terminating
  • Container Created, the container is being created

pod naming rules

k8s-nginx-75f95db655-24mfb
  • Deployment controller name + replica controller name + pod name

5. Enter the pod container

[root@k8s-master ~]# kubectl exec -it k8s-nginx-75f95db655-24mfb -- bash
root@k8s-nginx-75f95db655-24mfb:/#
  • kubectl exec enters the pod container
  • -it interactively enters the container
  • – bash specifies the interpreter

6. Delete the pod container

[root@k8s-master ~]# kubectl delete pod k9s-nginx-68df494c64-529qk
  • delete, delete the specified pod

After deleting a pod, because the replica controller is specified as 3, although one is deleted, a pod will be automatically replenished.

7. Delete the deployment controller

[root@k8s-master ~]# kubectl delete ployment k8s-nginx-75f95db655

After deleting the deployment controller, all replica controllers and pods will be completely deleted.

Guess you like

Origin blog.csdn.net/qq_57629230/article/details/131354210