kubernetes集群编排——控制器

控制器

文档:https://v1-25.docs.kubernetes.io/zh-cn/docs/concepts/workloads/controllers/

replicaset

vim rs-example.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
kubectl apply -f rs-example.yml
kubectl get pod --show-labels

replicaset是通过标签匹配pod

kubectl label pod replicaset-example-w9z28 app=myapp --overwrite

kubectl get pod --show-labels

kubernetes集群编排——控制器_第1张图片

kubectl label pod replicaset-example-w9z28 app=nginx --overwrite

kubectl get pod --show-labels

kubernetes集群编排——控制器_第2张图片

replicaset自动控制副本数量,pod可以自愈

kubectl delete pod replicaset-example-q2sq9

kubectl get pod --show-labels

kubernetes集群编排——控制器_第3张图片

回收资源

kubectl delete -f rs-example.yml

deployment

vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:v1
kubectl apply -f deployment-example.yaml
kubectl get all

kubernetes集群编排——控制器_第4张图片

版本迭代

vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:v2
kubectl apply -f deployment-example.yaml
kubectl get all

kubernetes集群编排——控制器_第5张图片

replicaset就是deployment的一个版本

回滚

vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-example
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:v1
kubectl apply -f deployment-example.yaml
kubectl get all

kubernetes集群编排——控制器_第6张图片

滚动更新策略

vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-example
spec:
  minReadySeconds: 5				
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:v1
kubectl apply -f deployment-example.yaml

kubectl describe deployments.apps deployment-example

kubernetes集群编排——控制器_第7张图片

暂停与恢复

暂停,避免触发不必要的线上更新

kubectl rollout pause deployment deployment-example
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-example
spec:
  minReadySeconds: 5
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  replicas: 6				
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: nginx
        resources:
          limits:
            cpu: 0.5
            memory: 200Mi
          requests:
            cpu: 0.5
            memory: 200Mi
kubectl apply -f deployment-example.yaml
kubectl get all

调整副本数,不受影响

kubernetes集群编排——控制器_第8张图片

但是更新镜像和修改资源并没有触发更新

kubectl rollout history deployment deployment-example

恢复后开始触发更新

kubectl rollout resume deployment deployment-example

kubectl rollout history  deployment deployment-example

kubernetes集群编排——控制器_第9张图片

kubectl get pod

kubernetes集群编排——控制器_第10张图片

回收

kubectl delete -f deployment-example.yaml

daemonset

vim daemonset-example.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-example
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      tolerations:
      - effect: NoSchedule
        operator: Exists
      containers:
      - name: nginx
        image: nginx
kubectl apply -f daemonset-example.yml

kubectl get pod -o wide

kubernetes集群编排——控制器_第11张图片

kubectl delete -f daemonset-example.yml

job

vim job.yml
apiVersion: batch/v1

kind: Job

metadata:

  name: pi

spec:

  completions: 6

  parallelism: 2

  template:

    spec:

      containers:

      - name: pi

        image: perl:5.34.0

        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]

      restartPolicy: Never

  backoffLimit: 4
kubectl apply -f job.yml

kubectl get pod

kubernetes集群编排——控制器_第12张图片

kubectl delete -f job.yml

cronjob

vim cronjob.yml
apiVersion: batch/v1

kind: CronJob

metadata:

  name: hello

spec:

  schedule: "* * * * *"

  jobTemplate:

    spec:

      template:

        spec:

          containers:

          - name: hello

            image: busybox

            imagePullPolicy: IfNotPresent

            command:

            - /bin/sh

            - -c

            - date; echo Hello from the Kubernetes cluster

          restartPolicy: OnFailure
kubectl apply -f cronjob.yml

kubectl get all

kubernetes集群编排——控制器_第13张图片

kubectl delete -f cronjob.yml

你可能感兴趣的:(kubernetes集群编排,kubernetes,容器,云原生)