1.ReplicaSet

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

  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

查看replicaset资源
[liwm@rmaster01 ~]$ kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
nginx 3 3 3 92s
[liwm@rmaster01 ~]$
[liwm@rmaster01 ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-q7dsl 1/1 Running 0 97s
nginx-qwbtp 1/1 Running 0 97s
nginx-vlwbd 1/1 Running 0 97s
[liwm@rmaster01 ~]$
#########
DESIRED: 请求的副本数
CURRENT:实际运行的副本数
READY:副本数为READY的数量
AGE:生命周期
###
# 方法一:修改本地yaml文件的replicas: 3
# 方法二:编辑已经运行资源的yaml文件:kubectl edit replicaset
# 方法三:通过命令行:kubectl scale replicaset —replicas=1

replicaset和deployment的区别

2. Deployment

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

方法一:命令行创建
kubectl run nginx-app —image=nginx:1.9.0 —image-pull-policy=IfNotPresent —replicas=2
# 方法二:yaml创建

  1. cat << EOF > nginx-deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-deployment
  6. labels:
  7. app: nginx-deploy
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: nginx-pod
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx-pod
  17. spec:
  18. containers:
  19. - name: nginx
  20. image: nginx:1.7.6
  21. imagePullPolicy: IfNotPresent
  22. ports:
  23. - containerPort: 80
  24. EOF
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. progressDeadlineSeconds: 600 #执行操作的超时时间
  7. replicas: 20
  8. revisionHistoryLimit: 10 #保存的历史版本数量
  9. selector:
  10. matchLabels:
  11. app: nginx-deployment
  12. strategy:
  13. rollingUpdate:
  14. maxSurge: 25% #升级过程中最多可以比原先设置多出的 pod 数量
  15. maxUnavailable: 25% #升级过程中最多有多少个 pod 处于无法提供服务的状态
  16. type: RollingUpdate #更新策略
  17. template:
  18. metadata:
  19. labels:
  20. app: nginx-deployment
  21. spec:
  22. containers:
  23. - name: nginx-deployment
  24. image: nginx:1.9
  25. imagePullPolicy: IfNotPresent
  26. ports:
  27. - containerPort: 80

查看资源
[liwm@rmaster01 ~]$ kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[liwm@rmaster01 ~]$ kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 41s
[liwm@rmaster01 ~]$
###
READY:Pod READY的数量
UP-TO-DATE: 升级最新的Pod数量
AVAILABLE:可用的Pod数量
AGE:资源的生命周期

升级镜像版本为 nginx:1.9.0
# 方法一:更改本地yaml,并使用apply升级
# 方法二:使用edit在线更改运行的Deployment,修改container的image
# 方法三:通过命令行升级并记录升级信息:kubectl set image deployment nginx-deployment nginx=nginx:latest —record

查看升级版本
[rancher@rmaster01 ~]$ kubectl describe pod nginx-deployment-75ff656486-x7rq6
#查看升级过程
[rancher@rmaster01 ~]$ kubectl describe deployments.apps nginx-deployment
#更新保留pod的数量
RollingUpdateStrategy: 25% max unavailable, 25% max surge
#更新策略
Recreate(重建)
RollingUpdate(滚动更新)默认值

查看deployment升级的状态
kubectl rollout status deployment nginx-deployment
# 在升级过程可以暂停部署
kubectl rollout pause deployment nginx-deployment

查看更新信息
[rancher@rmaster01 ~]$ kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
4 kubectl set image deployment nginx-deployment nginx=nginx:latest —record=true
5 kubectl set image deployment nginx-deployment nginx=nginx:1.9.0 —record=true
6 kubectl set image deployment nginx-deployment nginx=nginx:latest —record=true

[rancher@rmaster01 ~]$ kubectl rollout history deployment nginx-deployment —revision=5
deployment.apps/nginx-deployment with revision #5
Pod Template:
Labels: app=nginx-pod
pod-template-hash=75ff656486
Annotations: kubernetes.io/change-cause: kubectl set image deployment nginx-deployment nginx=nginx:1.9.0 —record=true
Containers:
nginx:
Image: nginx:1.9.0
Port: 80/TCP
Host Port: 0/TCP
Environment:
Mounts:
Volumes:
[rancher@rmaster01 ~]$

更新回滚指定版本

[rancher@rmaster01 ~]$ kubectl rollout undo deployment nginx-deployment —to-revision=5
deployment.apps/nginx-deployment rolled back
[rancher@rmaster01 ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-554bb5dc67-87qw7 1/1 Running 0 8m50s
nginx-deployment-554bb5dc67-fj5zc 1/1 Running 0 8m50s
nginx-deployment-554bb5dc67-k4s9h 1/1 Running 0 8m47s
nginx-deployment-554bb5dc67-ln77v 1/1 Terminating 0 8m47s
nginx-deployment-75ff656486-42q64 0/1 ContainerCreating 0 2s
nginx-deployment-75ff656486-vmq5c 0/1 ContainerCreating 0 3s

[rancher@rmaster01 ~]$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-75ff656486-42q64 1/1 Running 0 24s
nginx-deployment-75ff656486-78vv7 1/1 Running 0 17s
nginx-deployment-75ff656486-d2g76 1/1 Running 0 20s
nginx-deployment-75ff656486-vmq5c 1/1 Running 0 25s
[rancher@rmaster01 ~]$

3. DaemonSet
DaemonSet确保所有(或部分)节点上运行一个Pod的副本。当集群加入新的节点时,会自动在新的节点上创建Pod的副本。当节点从群集中删除时,这些Pod会被回收。删除DaemonSet将清除其创建的Pod。典型的应用包括:
日志收集,比如 fluentd,logstash 等
系统监控,比如 Prometheus Node Exporter,collectd,New Relic agent,Ganglia gmond 等
系统程序,比如 kube-proxy, glusterd, ceph 等

  1. cat << EOF > daemonset.yaml
  2. apiVersion: apps/v1
  3. kind: DaemonSet
  4. metadata:
  5. name: fluentd-elasticsearch
  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. containers:
  18. - name: fluentd-elasticsearch
  19. image: ist0ne/fluentd-elasticsearch
  20. EOF

4. 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

  1. cat << EOF > statefulset.yaml
  2. apiVersion: apps/v1
  3. kind: StatefulSet
  4. metadata:
  5. name: web
  6. spec:
  7. selector:
  8. matchLabels:
  9. app: nginx # has to match .spec.template.metadata.labels
  10. serviceName: "nginx" # \$(podname).(headless server name).namespace.svc.cluster.local
  11. replicas: 3 # by default is 1
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx # has to match .spec.selector.matchLabels
  16. spec:
  17. terminationGracePeriodSeconds: 10
  18. containers:
  19. - name: nginx
  20. image: nginx
  21. imagePullPolicy: IfNotPresent
  22. ports:
  23. - containerPort: 80
  24. name: web
  25. EOF

5. Job
Jobs创建一个或多个Pod并确保指定数量的Pod成功终止。当pod成功完成后,Job会跟踪成功的完成情况。达到指定数量的成功完成时,任务(即Jobs)完成。注意:删除作业将清理它创建的Pod。

  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. name: pi
  5. spec:
  6. backoffLimit: 6 #标记为 failed 前的重试次数,默认为 6
  7. completions: 4 #要完成job 的 pod 数,若没有设定该值则默认等于 parallelism 的值
  8. parallelism: 2 #任意时间最多可以启动多少个 pod 同时运行,默认为 1
  9. activeDeadlineSeconds: 120 #job 运行时间
  10. ttlSecondsAfterFinished: 60 #job 在运行完成后 60 秒就会自动删除掉
  11. template:
  12. spec:
  13. containers:
  14. - command:
  15. - sh
  16. - -c
  17. - 'echo ''scale=5000; 4*a(1)'' | bc -l '
  18. image: busybox
  19. name: pi
  20. restartPolicy: Never

  1. cat << EOF > job.yaml
  2. apiVersion: batch/v1
  3. kind: Job
  4. metadata:
  5. name: pi
  6. spec:
  7. completions: 10
  8. parallelism: 2
  9. template:
  10. spec:
  11. containers:
  12. - name: pi
  13. image: perl
  14. command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
  15. restartPolicy: Never
  16. backoffLimit: 4
  17. EOF

6. **CronJobCronJob 用于创建具有时间安排的任务。类似于linux系统的Crontab,都是通过周期表来运行Job。注意:CronJob schedule:时间均基于的kube-controller-manager的时区。
典型的用法包括:
需要指定时间点调度 Job 运行
创建周期性运行的 Job,例如:数据库备份、发送邮件。

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

SCHEDULE:时间表
SUSPEND:暂停
ACTIVE:激活的任务
LAST SCHEDULE:最后执行的时间
yaml字段解析:

  • .spec.schedule:时间表,必需字段,指定任务运行周期,格式同 Cron
  • .spec.jobTemplate:Job 模板,必需字段,指定需要运行的任务,格式同 Job
  • .spec.startingDeadlineSeconds :启动 Job 的期限(秒级别),该字段是可选的。如果因为任何原因而错过了被调度的时间,那么错过执行时间的 Job 将被认为是失败的。如果没有指定,则没有期限
  • .spec.concurrencyPolicy:并发策略
    • Allow(默认):允许并发运行 Job
    • Forbid:禁止并发运行,如果前一个还没有完成,则直接跳过下一个
    • Replace:取消当前正在运行的 Job,用一个新的来替换