乐趣区

关于kubernetes:Pod高级控制器

Pod 高级控制器

Pod 高级控制器

  • ReplicaSet
  • Deployment
  • DaemonSet
  • Job
  • CronJob
ReplicaSet
$ cat rs-pod.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-example
spec:
  replicas: 2
  selector:
    matchLabels:
      app: rs-demo
  template:
    metadata:
      labels:
        app: rs-demo
    spec:
      containers:
      - name: myapp
        image: myapp:v1
        ports:
        - name: http
          containerPort: 80
$ kubectl apply -f rs-pod.yaml 
replicaset.apps/rs-example created
$ kubectl get pod
NAME               READY   STATUS    RESTARTS   AGE
rs-example-4r7rl   1/1     Running   0          4s
rs-example-hngbv   1/1     Running   0          4s

# 批改其中一个 pod 的标签,replicaset 控制器会主动保护正本数量,如果标签不存在的会变成独立 pod
$ kubectl label pods rs-example-4r7rl app= --overwrite
pod/rs-example-4r7rl labeled


$ kubectl get pod --show-labels                       
NAME               READY   STATUS    RESTARTS   AGE   LABELS
rs-example-4r7rl   1/1     Running   0          37s   app=
rs-example-54589   1/1     Running   0          9s    app=rs-demo
rs-example-hngbv   1/1     Running   0          37s   app=rs-demo

Deployment
$ cat myapp-deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:v1
        ports:
        - containerPort: 80
          name: http
# 通过 Deployments 创立 pod
$ kubectl apply -f myapp-deploy.yaml --record
deployment.apps/myapp-deploy created

# 查看 deployment 状态
$ kubectl get deployments
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   3/3     3            3           11s


$ kubectl describe deployments myapp-deploy
Name:                   myapp-deploy
Namespace:              default
CreationTimestamp:      Tue, 21 Jul 2020 22:19:16 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=myapp-deploy...
                        kubernetes.io/change-cause: kubectl apply --filename=myapp-deploy.yaml --record=true
Selector:               app=myapp
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=myapp
  Containers:
   myapp:
    Image:        myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   myapp-deploy-76c6f7d88 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  24s   deployment-controller  Scaled up replica set myapp-deploy-76c6f7d88 to 3
  
# 查看变更历史
$ kubectl rollout history deployment/myapp-deploy
deployment.apps/myapp-deploy
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=myapp-deploy.yaml --record=true

DaemonSet
$ cat filebeat-ds.yaml 
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat-ds
  labels:
    app: filebeat
spec:
  selector:
    matchLabels:
      app: filebeat
  template:
    metadata:
      labels:
        app: filebeat
      name: filebeat
    spec:
      containers:
      - name: filebeat
        image: store/elastic/filebeat:7.6.2
        env:
        - name: REDIS_HOST
          value: db.ilinux.io:6379
        - name: LOG_LEVEL
          value: info
          
$ kubectl apply -f filebeat-ds.yaml
$ kubectl get daemonset
NAME          DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
filebeat-ds   1         1         1       1            1           <none>          4m13s
$ kubectl get pods     
NAME                READY   STATUS    RESTARTS   AGE
filebeat-ds-nrzbn   1/1     Running   0          4m15s

# 更新 DaemonSet 对象
$ kubectl set image daemonsets filebeat-ds filebeat=store/elastic/filebeat:7.6.0

Job
$ cat job-example.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: job-example
spec:
  template:
    spec:
      containers:
      - name: myjob
        image: busybox
        command: ["/bin/sh", "-c", "sleep 120"]
      restartPolicy: Never
      
$ kubectl apply -f job-example.yaml
$ kubectl get jobs
NAME          COMPLETIONS   DURATION   AGE
job-example   0/1           66s        66s

$ kubectl describe jobs job-example 
Name:           job-example
Namespace:      default
Selector:       controller-uid=9aad5c45-d165-4824-9982-16a0e746babd
Labels:         controller-uid=9aad5c45-d165-4824-9982-16a0e746babd
                job-name=job-example
Annotations:    kubectl.kubernetes.io/last-applied-configuration:
                  {"apiVersion":"batch/v1","kind":"Job","metadata":{"annotations":{},"name":"job-example","namespace":"default"},"spec":{"template":{"spec":...
Parallelism:    1
Completions:    1
Start Time:     Tue, 07 Apr 2020 22:51:27 +0800
Pods Statuses:  1 Running / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=9aad5c45-d165-4824-9982-16a0e746babd
           job-name=job-example
  Containers:
   myjob:
    Image:      busybox
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
      -c
      sleep 120
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From            Message
  ----    ------            ----  ----            -------
  Normal  SuccessfulCreate  94s   job-controller  Created pod: job-example-ftrb9
  
$ kubectl get pod -w
NAME                READY   STATUS    RESTARTS   AGE
job-example-ftrb9   1/1     Running   0          104s
job-example-ftrb9   0/1     Completed   0          2m7s

并行性 Job

$ cat job-multi.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: job-multi
spec:
  completions: 5
  template:
    spec:
      containers:
      - name: myjob
        image: alpine
        command: ["/bin/sh", "-c", "sleep 20"]
      restartPolicy: OnFailure
      
$ kubectl apply -f job-multi.yaml
$ kubectl get pod -w
NAME              READY   STATUS      RESTARTS   AGE
job-multi-5tcbs   0/1     Completed   0          2m17s
job-multi-bv6cg   0/1     Completed   0          3m43s
job-multi-nnskm   0/1     Completed   0          2m45s
job-multi-qp7nq   0/1     Completed   0          3m11s
job-multi-xz6dn   0/1     Completed   0          110s

退出移动版