init contaienr

它是一种专用的容器,在Pod内的应用容器启动之前运行,并包括一些应用镜像中不存在的实用工具和安装脚本。

Init 容器支持应用容器的全部字段和特性,包括资源限制、数据卷和安全设置。 然而,Init 容器对资源请求和限制的处理稍有不同,在下面 资源 处有说明。

同时 Init 容器不支持 Readiness Probe,因为它们必须在 Pod 就绪之前运行完成。

如果为一个 Pod 指定了多个 Init 容器,这些容器会按顺序逐个运行。每个 Init 容器必须运行成功,下一个才能够运行。当所有的 Init 容器运行完成时,Kubernetes 才会为 Pod 初始化应用容器并像平常一样运行。

链接

  1. cat << EOF > initcontainers.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: init-test
  6. labels:
  7. app: myapp
  8. spec:
  9. containers:
  10. - name: container
  11. image: busybox:1.28
  12. command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  13. initContainers:
  14. - name: init-test
  15. image: busybox:1.28
  16. command: ['sh', '-c', 'echo init containers test && sleep 10']
  17. EOF

rancher上启动pod添加sidecar,添加initcontainer

replicaset

ReplicaSet通过selector标识创建的pod。yaml文件里的字段replicas是指控制器应该维护的Pod副本数量。当ReplicSet需要创建新的Pod副本会使用yaml里的pod模板,也是就template字段。

注意labels:如果创建副本指定labels,那么如果创建的labels的pod或容器超过labels数量,就会被删除

  1. cat << EOF > replicaset.yaml
  2. apiVersion: apps/v1
  3. kind: ReplicaSet
  4. metadata:
  5. name: nginx
  6. labels:
  7. app: rs-nginx
  8. spec:
  9. # modify replicas according to your case
  10. replicas: 3
  11. selector:
  12. matchLabels:
  13. app: pod-nginx
  14. template:
  15. metadata:
  16. labels:
  17. app: pod-nginx
  18. spec:
  19. containers:
  20. - name: nginx
  21. image: nginx
  22. EOF
  23. [rancher@master1 ~]$ kubectl get replicasets.apps
  24. NAME DESIRED CURRENT READY AGE
  25. nginx 3 3 3 66s

deployment

Deployment是一个更高级别的概念用于部署无状态服务,通过管理ReplicaSet为Pod提供新的功能,比如:更新、回滚等。

创建完deployment后会自动创建replicasets,官网链接

  1. # run
  2. [rancher@master1 test]$ kubectl run --image=nginx --replicas=2 --image-pull-policy=IfNotPresent --labels=app=nginx test
  3. kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
  4. deployment.apps/test created
  5. # 查
  6. [rancher@master1 test]$ kubectl get deployments.apps
  7. NAME READY UP-TO-DATE AVAILABLE AGE
  8. test 2/2 2 2 75s
  9. [rancher@master1 test]$ kubectl get replicasets.apps
  10. NAME DESIRED CURRENT READY AGE
  11. nginx 3 3 3 47m
  12. test-6f596c6495 2 2 2 105s
  13. [rancher@master1 test]$ kubectl describe deployments.apps nginx-deployment
  14. # 更新,指定deployment容器的名字
  15. [rancher@master1 test]$ kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1
  16. deployment.apps/nginx-deployment image updated
  17. deployment.apps/nginx-deployment image updated
  18. # 查看每一次更新版本的操作
  19. [rancher@master1 test]$ kubectl rollout history deployment nginx-deployment --revision=2
  20. # 回滚
  21. [rancher@master1 test]$ kubectl rollout undo deployment nginx-deployment
  22. deployment.apps/nginx-deployment rolled back
  23. # 升级
  24. [rancher@master1 test]$ kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:latest --record=true
  25. deployment.apps/nginx-deployment image updated
  26. # 检查正在进行或进行完成的状态
  27. [rancher@master1 test]$ kubectl rollout status deployment nginx-deployment
  28. deployment "nginx-deployment" successfully rolled out
  29. # 缩放
  30. kubectl scale deployment.v1.apps/nginx-deployment --replicas=10
  1. ```yml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-deployment
  6. labels:
  7. app: nginx
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx
  17. spec:
  18. containers:
  19. - name: nginx
  20. image: nginx:1.14.2
  21. ports:
  22. - containerPort: 80
  1. <a name="daemonset"></a>
  2. ## daemonset
  3. DaemonSet确保所有(或部分)节点上运行一个Pod的副本。当集群加入新的节点时,会自动在新的节点上创建Pod的副本。当节点从群集中删除时,这些Pod会被回收。删除DaemonSet将清除其创建的Pod。典型的应用包括:
  4. -
  5. 日志收集,比如 fluentd,logstash 等
  6. -
  7. 系统监控,比如 Prometheus Node Exporter,collectd,New Relic agent,Ganglia gmon等
  8. -
  9. 系统程序,比如 kube-proxy, glusterd, ceph 等
  10. ```bash
  11. [rancher@master1 test]$ kubectl delete -f daemonset.yml
  12. daemonset.apps "fluentd-elasticsearch" deleted
  1. apiVer1sion: apps/v1
  2. kind: DaemonSet
  3. metadata:
  4. name: fluentd-elasticsearch
  5. namespace: kube-system
  6. labels:
  7. k8s-app: fluentd-logging
  8. spec:
  9. selector:
  10. matchLabels:
  11. name: fluentd-elasticsearch
  12. template:
  13. metadata:
  14. labels:
  15. name: fluentd-elasticsearch
  16. spec:
  17. tolerations:
  18. # 设置了宽容,在某种情况下,可以调度或不调度master,这里是容忍master可以运行pod
  19. # this toleration is to have the daemonset runnable on master nodes
  20. # remove it if your masters can't run pods
  21. - key: node-role.kubernetes.io/master
  22. effect: NoSchedule
  23. containers:
  24. - name: fluentd-elasticsearch
  25. image: quay.azk8s.cn/fluentd_elasticsearch/fluentd:v2.5.2
  26. resources:
  27. limits:
  28. memory: 200Mi
  29. requests:
  30. cpu: 100m
  31. memory: 200Mi
  32. volumeMounts:
  33. - name: varlog
  34. mountPath: /var/log
  35. - name: varlibdockercontainers
  36. mountPath: /var/lib/docker/containers
  37. readOnly: true
  38. terminationGracePeriodSeconds: 30
  39. volumes:
  40. - name: varlog
  41. hostPath:
  42. path: /var/log
  43. - name: varlibdockercontainers
  44. hostPath:
  45. path: /var/lib/docker/containers

statefulset

StatefulSet控制器用于部署有状态应用程序,并且为每个Pod维护一个永久性标识符。

哪些需求或者应用程序会使用到StatefulSet控制器?

  • 稳定的持久化存储,即 Pod 重新调度后还是能访问到相同的持久化数据,基于 PVC 来实现

  • 稳定的网络标志,即 Pod 重新调度后其 PodName 和 HostName 不变,基于 Headless Service(即没有 Cluster IP 的 Service)来实现

  • 有序部署与扩缩容:即 Pod 是有顺序的在部署或者扩缩容的时候要依据定义的顺序依次依序进行(即从 0 到 N-1,在下一个 Pod 运行之前所有之前的 Pod 必须都是 Running 和 Ready 状态)

  • 有序的滚动更新:StatefulSet控制器将删除并重新创建StatefulSet中的每个Pod。它将以Pod终止的顺序进行(从最大顺序到最小顺序),每次更新一个Pod。它会等到一个更新完成在去更新下一个Pod ```yml apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports:

    • port: 80 name: web clusterIP: None selector: app: nginx

apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: selector: matchLabels: app: nginx # has to match .spec.template.metadata.labels serviceName: “nginx” replicas: 3 # by default is 1 template: metadata: labels: app: nginx # has to match .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers:

  1. - name: nginx
  2. image: nginx
  3. ports:
  4. - containerPort: 80
  5. name: web
  1. <a name="job"></a>
  2. ## job
  3. Jobs创建一个或多个Pod并确保指定数量的Pod成功终止。当pod成功完成后,Job会跟踪成功的完成情况。达到指定数量的成功完成时,任务(即Jobs)完成。注意:删除作业将清理它创建的Pod。
  4. ```yml
  5. apiVersion: batch/v1
  6. kind: Job
  7. metadata:
  8. name: pi
  9. spec:
  10. template:
  11. spec:
  12. containers:
  13. - name: pi
  14. image: perl
  15. command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
  16. restartPolicy: Never
  17. backoffLimit: 4

cronjob

CronJob 用于创建具有时间安排的任务。类似于linux系统的Crontab,都是通过周期表来运行Job。注意:CronJob schedule:时间均基于的kube-controller-manager的时区。

典型的用法包括:

  • 需要指定时间点调度 Job 运行

  • 创建周期性运行的 Job,例如:数据库备份、发送邮件。

    1. apiVersion: batch/v1beta1
    2. kind: CronJob
    3. metadata:
    4. name: hello
    5. spec:
    6. schedule: "*/1 * * * *"
    7. jobTemplate:
    8. spec:
    9. template:
    10. spec:
    11. containers:
    12. - name: hello
    13. image: busybox
    14. args:
    15. - /bin/sh
    16. - -c
    17. - date; echo Hello from the Kubernetes cluster
    18. restartPolicy: OnFailure