什么是控制器?
k8s内拥有许多的控制器类型,用来控制pod的状态、行为、副本数量等等,控制器通过pod的标签来控制pod。
控制器类型
控制器类型有多种,如:RC、RS、Deployment、HPA、StatefullSet、DaemonSet、Job、CronJob
- RC为副本控制器,为保证pod数量维持在期望的数量之内,如果容器异常退出,会自动创建新的pod来代替;当然,多了的pod也会被回收。
- RS与RC没有本质的不同,RS支持集合式的选择器,官方推荐使用RS。
apiVersion: apps/v1
# 资源类型是RS
kind: ReplicaSet
metadata:
name: rs-test
namespace: test
spec:
selector:
matchLabels:
# RS通过标签控制pod,控制标签app=nginx的pod
app: nginx
# 副本数为3个
replicas: 3
template:
# 从这开始,跟pod的编排文件一模一样
metadata:
labels:
# pod的标签是app=nginx PS: 标签是自定义的任意key与任意value的键值对
app: nginx
spec:
containers:
- image: nginx:latest
name: nginxapp
ports:
- name: nginx-port
containerPort: 80
- Deployment为Pod与RS提供声明式定义的方式,Deployment会自动创建RS来维持Pod数量,同时Deployment在RS的基础上还支持滚动更新、回滚。典型用法:web服务
# !!!Deployment的编排文件跟RS只差在了kind: Deployment!!!
apiVersion: apps/v1
# 资源类型是Deployment
kind: Deployment
metadata:
name: deploy-test
namespace: test
spec:
selector:
matchLabels:
# 通过标签控制pod,控制标签app=nginx的pod
app: nginx
# 副本数为3个
replicas: 3
template:
# 从这开始,跟pod的编排文件一模一样
metadata:
labels:
# pod的标签是app=nginx PS: 标签是自定义的任意key与任意value的键值对
app: nginx
spec:
containers:
- image: nginx:latest
name: nginxapp
ports:
- name: nginx-port
containerPort: 80
创建一个test.yaml
[root@ master ~]# kubectl apply -f test.yaml
deployment.apps/deploy-test created
[root@ master ~]# kubectl get deployments -n test
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy-test 3 3 3 3 7s
[root@ master ~]# kubectl get rs -n test
NAME DESIRED CURRENT READY AGE
deploy-test-866c58dbc9 3 3 3 14s
[root@ master ~]# kubectl get pods -n test
NAME READY STATUS RESTARTS AGE
deploy-test-866c58dbc9-2tdpc 1/1 Running 0 20s
deploy-test-866c58dbc9-c8gj2 1/1 Running 0 20s
deploy-test-866c58dbc9-l8w6k 1/1 Running 0 20s
更新nginx版本kubectl set image deployment deploy-test nginxapp=nginx:1.9.1 -n test
,可以看到deployment通过创建rs来滚动更新
[root@ master ~]# kubectl get rs -n test
NAME DESIRED CURRENT READY AGE
deploy-test-5cbd5ffdff 3 3 2 6s
deploy-test-866c58dbc9 1 1 1 6m24s
[root@ master ~]# kubectl get rs -n test
NAME DESIRED CURRENT READY AGE
deploy-test-5cbd5ffdff 3 3 3 10s
deploy-test-866c58dbc9 0 0 0 6m28s
- DaemonSet确保集群内全部(或部分)node节点上都分配一个pod,如果新加node节点,也会自动再分配对应的pod。典型用法:filebeat日志收集、prometheus linux资源监控
apiVersion: apps/v1
# 资源类型是DaemonSet
kind: DaemonSet
metadata:
name: daemonset-test
namespace: test
spec:
selector:
matchLabels:
# 控制标签app=daemon-label的pod
app: daemon-label
template:
metadata:
labels:
# pod的标签是app = daemon-label
app: daemon-label
spec:
containers:
- image: nginx:latest
name: nginxapp
ports:
- name: nginx-port
containerPort: 80
- Job是只运行一次的服务,CronJob是周期性运行的服务。典型用法:数据库定时备份
apiVersion: batch/v1
# 类型是Job
kind: Job
metadata:
name: job-test
namespace: test
spec:
template:
metadata:
name: job-test
spec:
# 需要3个pod成功运行,jod才会结束,默认是1个
completions: 3
# 并行运行的pod个数,默认是1个
parallelism: 1
containers:
- image: busybox
name: mybox
command: ["/bin/sh", "-c", "echo i am job"]
# 重试策略 仅支持Never(不重试)与OnFailure(失败时重试)
restartPolicy: Never
apiVersion: batch/v1beta1
# 类型是CronJob
kind: CronJob
metadata:
name: cronjob-test
namespace: test
spec:
schedule: "*/1 * * * *"
# 任务的并行策略,Forbid表示如果前一个任务没有运行完毕,则直接跳过本次任务
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo hello world
restartPolicy: Never
- HPA支持按照期望的pod的cpu或内存来自动伸缩pod数量。
apiVersion: autoscaling/v1
# 资源类型是HPA
kind: HorizontalPodAutoscaler
metadata:
name: hpa-test
namespace: test
spec:
# 最大副本数
maxReplicas: 10
# 最小副本数
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1beta1
kind: Deployment
# 监控名为deploy-test的Deployment
name: deploy-test
# cpu 阈值 CPU大于80会自动创建pod来分担服务压力,小于80则减少pod数量
targetCPUUtilizationPercentage: 80
- StatefullSet是有状态服务的控制器,如各种DBMS数据存储系统。StatefullSet内的服务有着稳定的持久化存储,稳定的网络标识,有序部署,有序伸缩。