简介:
资源控制器
ReplicationController 和 ReplicaSet(RS)
Deployment
Deployment回退
DaemonSet
Job & CronJob
StatefulSet
资源控制器
RS 与 RC 与 Deployment 关联
$ vim rs.yaml
apiVersion: extensions/v1beta1 kind: ReplicaSet #kind类型 metadata: #元数据信息 name: frontend spec: #详细描述信息 replicas: 3 #副本数 selector: matchLabels: #标签 tier: frontend #匹配的key:values template: #模板(通过怎样的方式创建RS,以下可以理解为嵌套了一个pod模板信息) metadata: labels: tier: frontend spec: containers: - name: php-redis image: gcr.io/google_samples/gb-frontend:v3 env: - name: GET_HOSTS_FROM value: dns ports: - containerPort: 80
kubectl create -f rs.yaml
测试自主式pod和资源控制器控制的pod的区别
查看pod状态
kubectl get pod
可以看到所有pod信息,包括在运行的之前创建的自主式pod和上面创建的资源控制器pod
删除所有pod
kubectl delete pod --all
再次查看pod
可以看到删除的自主式Pod没有重新创建,而资源控制器pod会根据期望副本值,重新生成pod。
查看选择标签(标签是控制器识别副本数的标记)
kubectl get pod --show-labels
标签也可以修改
kubectl label pod frontend-m8hpc tier=frontend1 --overwrite=True
删除rs
kubectl delete rs --all
RS 与 Deployment 的关联图例
Deployment
$ vim deployment.yaml
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
kubectl create -f deployment.yaml --record # --record参数可以记录命令,我们可以很方便的查看每次 revision 的变化
查看deployment (deployment创建后随即会创建相应的RS)
kubectl get deployment
查看RS
查看pod
查看网络
kubectl get pod -o wide
从上面结果即可看出deployment、RS、pod的关系
kubectl scale deployment nginx-deployment --replicas 10
查看扩容后结果
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
kubectl set image deployment/nginx-deployment nginx=wangyanglinux/myapp:v2
kubectl rollout undo deployment/nginx-deployment
$ kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment "nginx-deployment" image updated
$ kubectl edit deployment/nginx-deployment
deployment "nginx-deployment" edited
$ kubectl rollout status deployment/nginx-deployment Waiting for rollout to finish: 2 out of 3 new replicas have been updated... deployment "nginx-deployment" successfully rolled out
$ kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-1564180365 3 3 0 6s nginx-deployment-2035384211 0 0 0 36s
$ kubectl describe deployments
Rollover(多个rollout并行)
kubectl set image deployment/nginx-deployment nginx=nginx:1.91
kubectl rollout status deployments nginx-deployment kubectl get pods kubectl rollout history deployment/nginx-deployment #查看回滚历史 kubectl rollout undo deployment/nginx-deployment #回滚上一个操作版本 kubectl rollout undo deployment/nginx-deployment --to-revision=2 # 可以使用 --revision参数指定 某个历史版本 kubectl rollout pause deployment/nginx-deployment # 暂停deployment 的更新
$ kubectl rollout status deployment/nginx-deployment Waiting for rollout to finish: 2 of 3 updated replicas are available... deployment "nginx-deployment" successfully rolled out $ echo $? 0
DaemonSet
$ vim deamonset.yaml
apiVersion: apps/v1 kind: DaemonSet metadata: name: deamonset-example labels: app: deamonset spec: selector: matchLabels: name: deamonset-example #和上面对应,必须一样,否创建不成功(创建出来一个新的pod,标签是deamonset-example,和上面的元数据信息匹配才能成功创建) template: metadata: labels: name: deamonset-example spec: containers: - name: daemonset-example image: wangyanglinux/myapp:v1
kubectl create -f deamonset.yaml
apiVersion: batch/v1 kind: Job metadata: name: pi spec: parallelism: 2 #最大并行数 completions: 4 #最小完成数 template: metadata: name: pi spec: containers: - name: pi image: perl command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"] restartPolicy: Never #执行成功后不重启,执行失败后Job Controller就会不断地尝试创建一个新Pod,默认重试次数在spec.backoffLimit定义,默认为6
apiVersion: batch/v1beta1 kind: CronJob metadata: name: hello spec: schedule: "*/1 * * * *" #每分钟执行一次 jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo Hello from the Kubernetes cluster restartPolicy: OnFailure #任务执行失败后,Job Controller 不会去尝试创建新的 Pod,但是,它会不断地尝试重启Pod里的容器
一段时间后,可看到pod状态变为了Completed,说明任务执行完成,此时查看任务日志,可看到执行结果。
如前所述,当一个 Job 的 Pod 运行结束后,它会进入 Completed 状态。但是,如果这个 Pod 因为某种原因一直不肯结束呢?
在 Job 的 API 对象里,有一个 spec.activeDeadlineSeconds 字段可以设置最长运行时间,比如:
spec: backoffLimit: 5 activeDeadlineSeconds: 100
一旦运行超过了 100 s,这个 Job 的所有 Pod 都会被终止。并且,你可以在 Pod 的状态里看到终 止的原因是 reason: DeadlineExceeded
删除任务
$ kubectl get cronjob NAME SCHEDULE SUSPEND ACTIVE LAST-SCHEDULE hello */1 * * * * False 0 <none> $ kubectl get jobs NAME DESIRED SUCCESSFUL AGE hello-1202039034 1 1 49s $ pods=$(kubectl get pods --selector=job-name=hello-1202039034 --output=jsonpath= {.items..metadata.name}) $ kubectl logs $pods Mon Aug 29 21:34:09 UTC 2016 Hello from the Kubernetes cluster # 注意,删除 cronjob 的时候不会自动删除 job,这些 job 可以用 kubectl delete job 来删除 $ kubectl delete cronjob hello cronjob "hello" deleted
Cron Job 在每次调度运行时间内 大概 会创建一个 Job 对象。我们之所以说 大概 ,是因为在特定的环境下可能会创建两个 Job,或者一个 Job 都没创建。我们尝试少发生这种情况,但却不能完全避免。因此,创建 Job 操作应该是 幂等的。
Job 根据它所创建的 Pod 的并行度,负责重试创建 Pod,并就决定这一组 Pod 的成功或失败。Cron Job 根本就不会去检查 Pod