k8s- Resource controller type

1 RS(ReplicaSet) and deployment

1.1 RC (ReplicationController )

The main function is to ensure that the number of copies of the container application is always kept at the user-defined number of copies . Just as

If the container exits abnormally , Will automatically create a new Pod To replace ; And if the extra containers are recovered automatically

Kubernetes Official recommended use RS(ReplicaSet ) replace RC (ReplicationController ) Deployment ,RS Follow RC There is no essential difference , It's just that the names are different , also RS Support for aggregate selector

 

example :

 

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: myapp
        image: wangyanglinux/myapp:v2
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - containerPort: 80

 

characteristic : rc If the pattern is updated , You need to change the mirror address of the configuration file , then apply once , But found pod There's still no update , You need to manually delete the original pod, You can update yourself , Compared with deployment It's not automatic .

1.2 Delete rs

 

[[email protected] yaml]# kubectl delete rs/frontend
replicaset.extensions "frontend" deleted

 

2 deployment

Definition Deployment To create Pod and ReplicaSet

Scroll upgrade and rollback applications

Expand and shrink

Pause and resume Deployment

example :

 

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: wangyanglinux/myapp:v2
        ports:
        - containerPort: 80
  • Capacity expansion

 

kubectl scale deployment nginx-deployment --replicas 10
  • Update the image address

 

kubectl set image  deployment/nginx-deployment nginx=wangyanglinux/myapp:v3
# You can also edit it directly
kubectl edit deployment/nginx-deployment

 

  • Roll back

 

kubectl rollout undo deployment/nginx-deployment

2.1 Delete deployment

 

[email protected] yaml]# kubectl delete deploy --all
deployment.extensions "nginx-deployment" deleted

 

3 Daemonset

DaemonSet Make sure that all ( Or some )Node Run a Pod Copy of . When there is Node When joining a cluster , It will also add a new one to them

individual Pod . When there is Node When removing from a cluster , these Pod It will also be recycled . Delete DaemonSet It will delete all the Pod

Use DaemonSet Some typical uses of :

Running cluster storage daemon, For example, in each Node Up operation glusterd 、 ceph

At every Node Run log collection on daemon, for example fluentd 、 logstash

At every Node On run monitoring daemon, for example Prometheus Node Exporter、 collectd 、Datadog agent

example :

 

It doesn't specify the number of copies , because daemonset The default is every node Create one on each node pod

 

 

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: deamonset-example
  labels:
    app: daemonset
spec:
  selector:
    matchLabels:
      name: deamonset-example
  template:
    metadata:
      labels:
        name: deamonset-example
    spec:
      containers:
      - name: daemonset-example
        image: wangyanglinux/myapp:v1

3.1 see daemonset

[[email protected] yaml]# kubectl get daemonset
NAME                DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
deamonset-example   2         2         2       2            2           <none>          31s

 

Every node Each node will run a pod

[[email protected] yaml]# kubectl get pod -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
deamonset-example-s9zsw   1/1     Running   0          40m   10.244.1.109   k8s-node1   <none>           <none>
deamonset-example-wlxm2   1/1     Running   0          40m   10.244.2.7     k8s-node2   <none>           <none>

4 job

Job Responsible for batch processing tasks , That is to say, a task that is performed only once , It guarantees one or more of the batch tasks Pod A successful ending

spec.template Same format Pod

RestartPolicy Support only Never or OnFailure

Single Pod when , Default Pod After successful operation Job That's the end

.spec.completions sign Job End what needs to run successfully Pod Number , The default is 1

.spec.parallelism Flag running in parallel Pod The number of , The default is 1

spec.activeDeadlineSeconds Flag failed Pod The maximum retry time of , No more retries beyond this time

 

example

 

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

 

4.1 see job Mission

4.2 Delete job Of pod

 

kubectl delete pod  pi-w7sr8

4.3 Delete job

 

[[email protected] yaml]# kubectl delete job pi
job.batch "pi" deleted

 

5 cronjob

Run only once at a given point in time

Run periodically at a given point in time

 

CronJob Spec
.spec.schedule : Dispatch , Required fields , Specify the task run cycle , Same format  Cron
.spec.jobTemplate :Job  Templates , Required fields , Specify the task to run , Same format  Job
.spec.startingDeadlineSeconds : start-up  Job  Time limit of ( Second level ), This field is optional . If it's wrong for any reason
After the scheduled time , So what's missing execution time  Job  Will be considered a failure . If not specified , There is no deadline
.spec.concurrencyPolicy : Concurrency strategy , This field is also optional . It specifies how to deal with being  Cron Job  Created  Job  Of
Concurrent execution . Only one of the following policies is allowed to be specified :
Allow ( Default ): Allow concurrent runs  Job
Forbid : Prohibit concurrent running , If the previous one hasn't been completed yet , Then skip the next one
Replace : Cancel the currently running  Job, Replace... With a new one
Be careful , The current policy can only be applied to the same  Cron Job  Created  Job. If there are multiple  Cron Job, They were created  Job  Between the total
Is to allow concurrent running .
.spec.suspend : Hang up , This field is also optional . If set to  true , All subsequent execution will be suspended . It has begun to
Executive  Job  It doesn't work . The default value is  false .
.spec.successfulJobsHistoryLimit  and  .spec.failedJobsHistoryLimit : Historical limitations , Is an optional field . it
They specify how many completions and failures to keep  Job. By default , They are set to  3  and  1 . Set the limit to  0 , phase
It's about the type of  Job  It will not be retained after completion .

 

5.1 see cronjob Information about

 

 

see pod journal

 

[[email protected] yaml]# kubectl log hello-1584433800-4bfc4
log is DEPRECATED and will be removed in a future version. Use logs instead.
Tue Mar 17 08:30:25 UTC 2020
Hello from the Kubernetes cluster

 

5.2 Delete cronjob

 

[[email protected] yaml]# kubectl delete cronjob --all
cronjob.batch "hello" deleted

6 StatefulSet

statefulset see :https://www.cnblogs.com/huningfei/p/12704794.html