一.控制器分类
Pod 的分类:
- 自主式 Pod:Pod 退出后不会被创建
- 控制器管理的 Pod:在控制器的生命周期里,始终要维持 Pod 的副本数目
控制器类型:
- Replication Controller和ReplicaSet
- Deployment
- DaemonSet
- StatefulSet
- Job
- CronJob
- HPA全称Horizontal Pod Autoscaler
二.Replication Controller和ReplicaSet
- ReplicaSet 是下一代的 Replication Controller,官方推荐使用ReplicaSet。
- ReplicaSet 和 Replication Controller 的唯一区别是选择器的支持,ReplicaSet支持新的基于集合的选择器需求。
- ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行。
- 虽然 ReplicaSets 可以独立使用,但今天它主要被Deployments 用作协调 Pod 创建、删除和更新的机制
使用ReplicaSet的脚本
[root@server1 ~]# cat rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 6
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: myapp:v1
执行rs.yml,查看pod信息
[root@server1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-7gfv6 1/1 Running 0 3m6s app=nginx
replicaset-example-ds8nn 1/1 Running 0 3m6s app=nginx
replicaset-example-hzc5k 1/1 Running 0 3m6s app=nginx
replicaset-example-qcf2n 1/1 Running 0 3m6s app=nginx
replicaset-example-swjvw 1/1 Running 0 3m6s app=nginx
replicaset-example-xj7ck 1/1 Running 0 3m6s app=nginx
详细信息
[root@server1 ~]# kubectl describe rs replicaset-example
Name: replicaset-example
Namespace: default
Selector: app=nginx
Labels: <none>
Annotations: <none>
Replicas: 6 current / 6 desired
Pods Status: 6 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: myapp:v1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 4m46s replicaset-controller Created pod: replicaset-example-7gfv6
Normal SuccessfulCreate 4m46s replicaset-controller Created pod: replicaset-example-xj7ck
Normal SuccessfulCreate 4m46s replicaset-controller Created pod: replicaset-example-swjvw
Normal SuccessfulCreate 4m46s replicaset-controller Created pod: replicaset-example-hzc5k
Normal SuccessfulCreate 4m46s replicaset-controller Created pod: replicaset-example-qcf2n
Normal SuccessfulCreate 4m46s replicaset-controller Created pod: replicaset-example-ds8nn
更改标签没不会影响数量,会自动创建一个新容器,自动补齐
[root@server1 ~]# kubectl label pod replicaset-example-7gfv6 app=myapp --overwrite
pod/replicaset-example-7gfv6 labeled
[root@server1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
replicaset-example-7gfv6 1/1 Running 0 6m30s app=myapp
replicaset-example-ds8nn 1/1 Running 0 6m30s app=nginx
replicaset-example-f6rzs 1/1 Running 0 9s app=nginx
replicaset-example-hzc5k 1/1 Running 0 6m30s app=nginx
replicaset-example-qcf2n 1/1 Running 0 6m30s app=nginx
replicaset-example-swjvw 1/1 Running 0 6m30s app=nginx
replicaset-example-xj7ck 1/1 Running 0 6m30s app=nginx
[root@server1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
replicaset-example 6 6 6 7m27s
更改版本为v2再执行脚本,发现版本不变一直是v1,因为rs只控制副本,当前运行容器不受控制
[root@server1 ~]# vim rs.yml
[root@server1 ~]# kubectl apply -f rs.yml
replicaset.apps/replicaset-example configured
[root@server1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
replicaset-example-7gfv6 1/1 Running 0 8m29s 10.244.2.29 server4 <none> <none>
replicaset-example-ds8nn 1/1 Running 0 8m29s 10.244.1.22 server2 <none> <none>
replicaset-example-f6rzs 1/1 Running 0 2m8s 10.244.1.24 server2 <none> <none>
replicaset-example-hzc5k 1/1 Running 0 8m29s 10.244.1.23 server2 <none> <none>
replicaset-example-qcf2n 1/1 Running 0 8m29s 10.244.2.30 server4 <none> <none>
replicaset-example-swjvw 1/1 Running 0 8m29s 10.244.1.21 server2 <none> <none>
replicaset-example-xj7ck 1/1 Running 0 8m29s 10.244.2.31 server4 <none> <none>
[root@server1 ~]# curl 10.244.2.29
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# curl 10.244.2.29
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
三.Deployment
Deployment
- Deployment 为 Pod 和 ReplicaSet 提供了一个申明式的定义方法
典型的应用场景:
- 用来创建Pod和ReplicaSet
- 滚动更新和回滚
- 扩容和缩容
- 暂停与恢复
Deployment中可以实现ReplicaSet 的全部功能,所以一般是一起使用
更改版本为v2,容器版本会发生变化
deployment.yml
[root@server1 ~]# cat deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
更改版本
[root@server1 ~]# vim deployment.yml
[root@server1 ~]# kubectl apply -f deployment.yml
deployment.apps/nginx-deployment created
[root@server1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-6456d7c676-7q8tt 1/1 Running 0 13s 10.244.1.25 server2 <none> <none>
nginx-deployment-6456d7c676-g574d 1/1 Running 0 13s 10.244.1.26 server2 <none> <none>
nginx-deployment-6456d7c676-gxv4h 1/1 Running 0 13s 10.244.2.32 server4 <none> <none>
[root@server1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-6456d7c676-7q8tt 1/1 Running 0 19s app=nginx,pod-template-hash=6456d7c676
nginx-deployment-6456d7c676-g574d 1/1 Running 0 19s app=nginx,pod-template-hash=6456d7c676
nginx-deployment-6456d7c676-gxv4h 1/1 Running 0 19s app=nginx,pod-template-hash=6456d7c676
[root@server1 ~]# curl 10.244.1.25
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# vim deployment.yml
[root@server1 ~]# kubectl apply -f deployment.yml
deployment.apps/nginx-deployment configured
[root@server1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-6d4f5bf58f-5frmm 1/1 Running 0 15s 10.244.2.33 server4 <none> <none>
nginx-deployment-6d4f5bf58f-dllzm 1/1 Running 0 12s 10.244.1.27 server2 <none> <none>
nginx-deployment-6d4f5bf58f-vjxgr 1/1 Running 0 13s 10.244.2.34 server4 <none> <none>
访问查看,得到v2版本
[root@server1 ~]# curl 10.244.2.33
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# curl 10.244.2.33
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@server1 ~]# curl 10.244.2.33
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
rs保存原副本,用于回滚
[root@server1 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-6d4f5bf58f-5frmm 1/1 Running 0 116s
nginx-deployment-6d4f5bf58f-dllzm 1/1 Running 0 113s
nginx-deployment-6d4f5bf58f-vjxgr 1/1 Running 0 114s
[root@server1 ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-6456d7c676 0 0 0 4m14s
nginx-deployment-6d4f5bf58f 3 3 3 3m18s
[root@server1 ~]#
四.DaemonSet
DaemonSet 确保全部(或者某些)节点上运行一个 Pod 的副本。当有节点加入集群时, 也会为他们新增一个 Pod 。当有节点从集群移除时,这些Pod 也会被回收。删除 DaemonSet 将会删除它创建的所有 Pod。
DaemonSet 的典型用法:
- 在每个节点上运行集群存储 DaemonSet,例如 glusterd、ceph。
- 在每个节点上运行日志收集 DaemonSet,例如 fluentd、logstash。
- 在每个节点上运行监控 DaemonSet,例如 Prometheus NodeExporter、zabbix agent等
- 一个简单的用法是在所有的节点上都启动一个 DaemonSet,将被作为每种类型的 daemon 使用。
- 一个稍微复杂的用法是单独对每种 daemon 类型使用多个 DaemonSet,但具有不同的标志, 并且对不同硬件类型具有不同的内存、CPU 要求。
[root@server2 pod]# cat daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
k8s-app: zabbix-agent
spec:
selector:
matchLabels:
name: zabbix-agent
template:
metadata:
labels:
name: zabbix-agent
spec:
containers:
- name: zabbix-agent
image: zabbix/zabbix-agent
五.StatefulSet
StatefulSet 是用来管理有状态应用的工作负载 API 对象。实例之间有不对等关系,以及实例对外部数据有依赖关系的应用,称为“有状态应用”
StatefulSet 用来管理 Deployment 和扩展一组 Pod,并且能为这些 Pod提供序号和唯一性保证。
StatefulSets 对于需要满足以下一个或多个需求的应用程序很有价值:
- 稳定的、唯一的网络标识符。
- 稳定的、持久的存储。
- 有序的、优雅的部署和缩放。
六.Job
执行批处理任务,仅执行一次任务,保证任务的一个或多个Pod成功结束。
CronJob
- Cron Job 创建基于时间调度的 Jobs。
- 一个 CronJob 对象就像 crontab (cron table) 文件中的一行,它用 Cron格式进行编写,并周期性地在给定的调度时间执行 Job。
job.yaml
[root@server2 pod]# cat job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
cronjob.yaml
[root@server2 pod]# cat cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-example
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: busyboxplus
args:
- /bin/sh
- -c
- date; echo Hello from k8s cluster
restartPolicy: OnFailure
七.HPA
- 根据资源利用率自动调整service中Pod数量,实现Pod水平自动缩放。