Controller Definition

The built-in controller in K8S acts as a state machine to control Pod’s specific state and behavior, such as scaling and upgrading. So it’s called a workload

Controller Type

Controllers are classified into the following types:

  • ReplicationController and ReplicaSet
  • Deployment (most commonly used)
  • DaemonSet
  • StateFulSet
  • Job/CronJob
  • Horizontal Pod Autoscaling

RC and RS

RC is not used at present, RS is adopted.

RC is used to control pod to maintain a normal and stable number

RS supports an ensemble of selectors that can be matched by tag

Sample:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myRS
spec:
  selector:
    matchLabels:
      auth: myAuth
  replicas: 3
  template:
    metadata:
      labels:
        auth: myAuth
    spec:
      containers:
        - name: myAuth
          image: icepear/dendalion-auth:2.0.0
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
Copy the code

Deployment

Provides a declarative definition method to replace RC

  • Define Deployment to create POD and replicaSet
  • Provides rolling upgrade and rollback applications
  • Capacity expansion and reduction
  • Suspend and continue deployment

Relationship between Deployment, replicaSet and POD



Use kubectl apply instead of kubectl create for declarative creation — the record parameter records commands and makes it easy to see every reversion change

1. Deploy simple applications

For example, I want to open three Nginx pod applications:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myNginx
  labels:
    app: myNginx
spec:
  replicas: 3
  template:
    metadata:
      name: myNginx
      labels:
        app: myNginx
    spec:
      containers:
        - name: myNginx
          image: nginx:1.7.9
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
  selector:
    matchLabels:
      app: myNginx
Copy the code

Expansion and 2.

Sample:

kubectl scale deployment myNginx --replicas=10
Copy the code

3. Ha capacity expansion

If the cluster supports HPA, you can also set conditions, such as the number of cpus to be enabled and the minimum number of cpus to be enabled when the number reaches a certain level

kubecl autoscale deployment myNginx --min=10 --max=15 --cpu-percent=80
Copy the code

4. Update the image

Kubectl set Image Deployment /myNginx nginx:1.8.0Copy the code
  • In the 25%-25% strategy, 25% of replicas will be created in the new replicas and 25% will be deleted in the old Replicas. Update according to this pattern
  • With the Rollover policy of updating the new version while it is still being created, Deployment will simply kill the previously created RS and build the new version directly

5. Roll back the version

Kubectl rollout undo deployment/myNginx --to-version=2 Kubectl rollout status deployment myNginx kubectl rollout history Deployment /myNginx Rollout Pause Deployment /myNginx # Pause rollback updateCopy the code

DaemonSet

DaemonSet ensures that all nodes run a copy of pod. When nodes are added or deleted, the corresponding pod is also added or deleted. Deleting DaemonSet deletes the pod created by it

  • Run cluster storage daemons such as Glusterd and Ceph on each node
  • Run log collection daemons such as Logstash and Fluentd on each node
  • Run monitoring daemons on each node, such as Promethenus node

Create using kubectl create

Job

Job Is a controller that runs scripts for batch tasks

  • Spec. Template is in pod format
  • RestartPolicy Supports only Never or Onfailure
  • If there is only one Pod, the Job ends when the Pod runs successfully by default
  • Spec.com pletions Indicates the number of PODS that need to run successfully after the job is finished. The default value is 1
  • Spec.parallelism marks the number of pods running in parallel. The default is 1
  • Spec. ActiveDeadlineSeconds sign Pod retry time biggest failure, more than not try again

For example: create one using Perl that computes PI to print 2000 bits

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
        - name: pi
          image: perl
          command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
Copy the code

CronJob

On the basis of jobs, the system provides a periodic execution scheme

  • Spec. schedule Run period. The format is Cron
  • JobTemplate Job template. The format is Job
  • Spec. StartingDeadlineSeconds start Job period, seconds
  • Spec. concurrencyPolicy concurrencyPolicy. A second job may be created when the first job is not completed
  • Allow to Allow
  • Firbid ban
  • Replace Cancels the current one and replaces it with a new one
  • Sper. Suspend hung
  • Spec. SuccessfulJobHistoryLimit and failedJobsHistoryLimit setting job success or failure of pod number, retained the default success is three, failure is 1

Example:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: myCronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: myCronjob
              image: busybox
              args:
                - /bin/sh
                - -c
                - date; echo Hello k8s
          restartPolicy: OnFailure
Copy the code

The result of a CronJob run should be idempotent, that is, the result of each run should be the same

StateFulSet

StateFulSet solves the problem of stateful service running

  • Stable persistent storage, that is, pod can still access the same persistent data after rescheduling, based on PVC implementation
  • Stable network flag, i.e. pod rescheduling, podName and HostName unchanged, based on Headless service implementation
  • Orderly deployment, orderly expansion, POD startup is orderly, according to the order, only after the previous POD startup success, the following can be continued
  • Shrink in order, delete in order, delete from back to front

HPA

Automatic expansion of POD, expand capacity at peak, delete some resources at trough, improve system stability