kubernetes中Deployment和replicaset关系剖析
前言:
本片文章主要介绍了Deployment和Replicaset之间的关系以及如何产生Pod
#vim nginx-deploy.yaml #先给出Deployment的YAML文件实例;
apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "1" creationTimestamp: "2021-01-11T02:54:57Z" generation: 1 labels: app: nginx name: nginx namespace: default spec: progressDeadlineSeconds: 600 replicas: 3 revisionHistoryLimit: 10 selector: matchLabels: app: nginx strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.15.2 imagePullPolicy: IfNotPresent name: nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30
kubectl create -f nginx-deploy.yaml #生成Deployment
#确定deployment
[root@k8s-master01 ~]# kubectl get deployment nginx -owide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 8m38s nginx nginx:1.15.2 app=nginx
[root@k8s-master01 ~]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-66bbc9fdc5-llhnn 1/1 Running 0 8m55s 172.18.195.60 k8s-master03 <none> <none>
nginx-66bbc9fdc5-sdhnh 1/1 Running 0 8m55s 172.25.244.203 k8s-master01 <none> <none>
nginx-66bbc9fdc5-x4x67 1/1 Running 0 8m55s 172.27.14.230 k8s-node02 <none> <none>
对生成的deployment的nginx进行日志分析,确定在deployment产生replica set的信息
#kubectl describe deploy nginx
.......... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set nginx-66bbc9fdc5 to 3
可以看出在message对应的信息中出现了一个Replica set,通过deployment-controller控制器创建一个replica set名为nginx-66bbc9fdc5
#确认Replica Set
[root@k8s-master01 ~]# kubectl get rs nginx-66bbc9fdc5 NAME DESIRED CURRENT READY AGE nginx-66bbc9fdc5 3 3 3 29m [root@k8s-master01 ~]#kubectl describe rs nginx-66bbc9fdc5 Name: nginx-66bbc9fdc5 Namespace: default Selector: app=nginx,pod-template-hash=66bbc9fdc5 Labels: app=nginx pod-template-hash=66bbc9fdc5 Annotations: deployment.kubernetes.io/desired-replicas: 3 deployment.kubernetes.io/max-replicas: 4 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/nginx Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=nginx pod-template-hash=66bbc9fdc5 Containers: nginx: Image: nginx:1.15.2 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 30m replicaset-controller Created pod: nginx-66bbc9fdc5-x4x67 Normal SuccessfulCreate 30m replicaset-controller Created pod: nginx-66bbc9fdc5-sdhnh Normal SuccessfulCreate 30m replicaset-controller Created pod: nginx-66bbc9fdc5-llhnn
可以清晰的看到3个pod实例是通过Replica set生成的;
#确认Pod
语法格式:kubectl describe pod nginx-66bbc9fdc5-x4x67
[root@k8s-master01 ~]# for i in `kubectl get pod | awk 'NR>1{print $1}'`;do echo $i;kubectl describe pod $i | tail -n 6;done nginx-66bbc9fdc5-llhnn Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 42m default-scheduler Successfully assigned default/nginx-66bbc9fdc5-llhnn to k8s-master03 Normal Pulled 42m kubelet Container image "nginx:1.15.2" already present on machine Normal Created 42m kubelet Created container nginx Normal Started 42m kubelet Started container nginx nginx-66bbc9fdc5-sdhnh Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 42m default-scheduler Successfully assigned default/nginx-66bbc9fdc5-sdhnh to k8s-master01 Normal Pulled 42m kubelet Container image "nginx:1.15.2" already present on machine Normal Created 42m kubelet Created container nginx Normal Started 42m kubelet Started container nginx nginx-66bbc9fdc5-x4x67 Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 42m default-scheduler Successfully assigned default/nginx-66bbc9fdc5-x4x67 to k8s-node02 Normal Pulled 42m kubelet Container image "nginx:1.15.2" already present on machine Normal Created 42m kubelet Created container nginx Normal Started 42m kubelet Started container nginx
总结:
从上面一步一步的剖析,我们可以清晰的看到Pod的产生过程:配置完deployment的yaml文件之后:
1、通过kubectl create 创建一个deployment,那么此时就会调用deployment-controller(deployment控制器)创建一个replica set
2、replica set调用replicaset-controller创建pod
3、Pod创建完之后就会由启用资源调度程序default-scheduler,将pod分配对应的node节点,由kubelet管理pod