Pod高級控制器linux
ReplicaSet基本不會直接使用,如今被更高級的Deployment替代api
$ 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
用於部署一些無狀態的服務app
$ 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
用在每一個節點部署一個副本的場景,好比監控,日誌的客戶端ui
$ 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用來建立一次性任務,相似linux的atspa
$ 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
在kubernetes下作定時任務,相似crontab3d
$ cat cronjob-example.yaml apiVersion: batch/v1beta1 kind: CronJob metadata: name: cronjob-example labels: app: mycronjob spec: schedule: "*/2 * * * *" jobTemplate: metadata: labels: app: mycronjob-jobs spec: parallelism: 2 template: spec: containers: - name: myjob image: alpine command: - /bin/sh - c - date; echo Hello from the Kubernetes cluster; sleep 10 restartPolicy: OnFailure $ kubectl apply -f cronjob-example.yaml $ kubectl get cronjobs NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE cronjob-example */2 * * * * False 1 67s 2m21s