background

Too soon, containerized orchestration technology has become a mandatory skill for every operations worker, even for developers. In addition to knowing how to use it, we can also learn about the implementation of kubernetes, an open source project. This not only improves our understanding of Kubernetes, but also allows us to learn about kubernetes code design patterns and coding techniques.

motivation

As an operation and maintenance staff of “vigorous, excellent hand work, strong screen staring ability”, I hope not to be eliminated by The Times, quickly took out a small book, learning, to ensure the smooth landing of “pot”.

Train of thought

Throughout the series I will analyze the core mechanics of Kubernetes from the perspective of how kubernetes creates a Deployment process. I’ll look at the source code implementation of each of the components that this command goes through.

General situation of

Before starting the whole series, please make sure you have a basic understanding of Kubernetes. If you do not know, please refer to the website: Kubernetes.io

The entire kubernetes cluster information is as follows:

  • Version: 1.12.4
  • Network plug-in: Calico-3.10.3
  • docker:

Deployment way: kubernetes. IO/useful/docs/set…

Cluster address:

  • 10.110.100.168
  • 10.110.100.125

Let’s create the first application and execute the command

 kubectl apply -f deploy.yaml
Copy the code

In addition, let’s look at the deploy.yaml file, and I will comment on all the information in the file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  The number of copies of ## pod
  replicas: 1
  Here the tag selects the POD template
  selector:
    matchLabels:
      app: nginx
  ## pod template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        # # container name
      - name: nginx
        ## Mirror address
        image: nginx
        ports:
        - containerPort: 80
Copy the code

After executing the above file, we will see what the YAML file looks like after k8S processing. What are the new fields?

apiVersion: apps/v1
kind: Deployment
metadata:
  # # comments
  annotations:
    ## Deployment version
    deployment.kubernetes.io/revision: "1"
    The configuration of the last application
    kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployme nt","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labe ls":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx","name":"nginx","ports":[{"containerPort":80}]}]}}}}  ## Create time
  creationTimestamp: "2021-03-14T14:01:04Z"
  # #
  generation: 1
  # # label
  labels:
    app: nginx
  ## "server-side Apply" function to track changes in object fields
  ## Manages fields
  managedFields:
  - apiVersion: apps/v1
    ## Specifies the type of field managed
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          . : {}
          f:kubectl.kubernetes.io/last-applied-configuration: {}
        f:labels:
          . : {}
          f:app: {}
      f:spec:
        f:progressDeadlineSeconds: {}
        f:replicas: {}
        f:revisionHistoryLimit: {}
        f:selector: {}
        f:strategy:
          f:rollingUpdate:
            . : {}
            f:maxSurge: {}
            f:maxUnavailable: {}
          f:type: {}
        f:template:
          f:metadata:
            f:labels:
              . : {}
              f:app: {}
          f:spec:
            f:containers:
              k:{"name":"nginx"}:
                . : {}
                f:image: {}
                f:imagePullPolicy: {}
                f:name: {}
                f:ports:
                  . : {}
                  k:{"containerPort":80,"protocol":"TCP"}:
                    . : {}
                    f:containerPort: {}
                    f:protocol: {}
                f:resources: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
            f:terminationGracePeriodSeconds: {}
    ## Field manager
    manager: kubectl
    ## Manager operation
    operation: Update
    ## Operation time
    time: "2021-03-14T14:01:04Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          f:deployment.kubernetes.io/revision: {}
      f:status:
        f:availableReplicas: {}
        f:conditions:
          . : {}
          k:{"type":"Available"}:
            . : {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
          k:{"type":"Progressing"}:
            . : {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
        f:observedGeneration: {}
        f:readyReplicas: {}
        f:replicas: {}
        f:updatedReplicas: {}
    ## Field manager
    manager: kube-controller-manager
    ## Operation mode
    operation: Update
    ## Operation time
    time: "2021-03-14T14:01:31Z"
  Name of ## Deployment
  name: nginx-deployment
  Namespace for ## Deployment
  namespace: default
  The current resource version of ## Deployment, which is used for inform version comparison
  resourceVersion: "197755"
  uid: e2947357-c425-4f5c-a624-3327cc62ad30
## Resource expectations
spec:
  ## Process termination time
  progressDeadlineSeconds: 600
  # # replications
  replicas: 1
  ## Retain the number of historical versions
  revisionHistoryLimit: 10
  ## Select the pod tag
  selector:
    matchLabels:
      app: nginx
  ## Publish policy
  strategy:
    ## Scroll publish
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  ## pod template
  template:
    metadata:
      creationTimestamp: null
      ## Labels here should be the same as the one selected above
      labels:
        app: nginx
    The expected state of ## pod
    spec:
      ## Container configuration
      containers:
      - image: nginx
        ## Mirror pull policy
        imagePullPolicy: Always
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        ## Interrupt log
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      # # DNS strategy
      dnsPolicy: ClusterFirst
      ## Restart policy, usually used with liVENESS
      restartPolicy: Always
      ## The name of the scheduler for this pod
      schedulerName: default-scheduler
      ## Security context
      securityContext: {}
      ## Maximum wait time to terminate when deleting a pod.
      terminationGracePeriodSeconds: 30
The final status of ## Deployment is what we see with kubectl get deploy
status:
  ## Number of available copies
  availableReplicas: 1
  # # conditions
  conditions:
  - lastTransitionTime: "2021-03-14T14:01:31Z"
    lastUpdateTime: "2021-03-14T14:01:31Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2021-03-14T14:01:04Z"
    lastUpdateTime: "2021-03-14T14:01:31Z"
    message: ReplicaSet "nginx-deployment-7848d4b86f" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
Copy the code

For example, if you want to change the value of a managerfield, you can change the value of a managerfield to the value of a managerfield. For example, if you want to change the value of a managerfield to the value of a managerfield, you can change the value of a managerfield to the value of a managerfield. This can cause serious consequences, so Apply is actually a (Merge) operation, that is, it takes into account the current condition of the object and the previous Settings of the object and merges it with your current Settings. Otherwise, conflict is likely to occur.

Specific can consult: kubernetes. IO/useful/docs/ref…

I want to give you an overview of how kubernetes can create a Deployment.

You can also pass this article: cloud.tencent.com/developer/a… Understand first.

Let’s start with a picture.

The whole process starts from Kubectl and ends with Kubelet generating the corresponding POD.

  • kubectl

    1. Validation and generator
    2. API version negotiation and API groups
    3. Send HTTP requests to apiserver
  • apiserver

    1. Client identity authentication
    2. authorization
    3. Access control
    4. Deserialize the data and store it in etCD
  • etcd

    1. Get the data
    2. Synchronous data
    3. Save the data
  • controller-manager

    1. deploy-controller
    2. ReplicaSet-controller
    3. pod-controller
  • scheduler

    1. Primary strategy
    2. Optimization strategy
    3. Binding nodes
  • kubelet

    1. Pod data synchronization
    2. Initialization, resources, volumes
    3. CRI and pause containers
    4. CNI and POD networks
    5. Container startup process

conclusion

I will publish a series of articles in the future. If you are interested, please follow me. In this process, there must be a lot of places not rigorous, but also hope that we forgive, we absorb the essence (if any), to its dregs. My wechat id is lCOMedy2021