一. 控制器介绍
1.控制器(工作负载)类型
(1). Deployment
(2). StatefulSet
(3). DaemonSet
(4). Job
(5). CronJob
2. Pod与controllers(控制器)的关系
• controllers 在集群上管理和运行Pod对象
• 通过label-selector相关联
• Pod通过控制器实现应用运维,如伸缩,升级等
二. Deployment
1. Deployment介绍
• 部署无状态应用
• 管理Pod和ReplicaSet(用于管理pod副本数,版本的控制 一个隐藏控制器)
• 具有上线部署、副本设定、滚动升级、回滚等功能
• 提供声明式更新(指定更新某个字段,两个用户同时更新会合并更新),例如只更新一个新的Image
2. Deployment应用
主要应用无状态应用,如Web服务
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
3. Deployment基本操作
# kubectl get pods,deploy,rs
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-6b8969f448-4p7zr 1/1 Running 0 99s
pod/nginx-deployment-6b8969f448-gj9c8 1/1 Running 0 99s
pod/nginx-deployment-6b8969f448-xmfb9 1/1 Running 0 99s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx-deployment 3 3 3 3 99s
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-deployment-6b8969f448 3 3 3 99s
查看编辑deployment的所有字段:
# kubectl edit deployment.extensions/nginx-deployment
查看历史版本
# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
二. SatefulSet
1. SatefulSet介绍
部署有状态应用, 解决Pod独立生命周期,保持Pod启动顺序和唯一性
(1). 稳定,唯一的网络标识符,持久存储
(2). 有序,优雅的部署和扩展、删除和终止
(3). 有序,滚动更新
应用场景:数据库
headless service:无头服务, 配置clusterIP: None,不给service分配IP。
service:一组pod访问策略,提供负载均衡和服务发现,每个service都会分配一个cluster IP。
headless service 没有分配clusterIP,要访问 headless service 下都pod,得借助集群中的DNS(如 coredns)。DNS解析到固定域名(唯一的网络标识符),如果用 headless service 做请求转发到pod, 这些pod到必须是使用 statefulset 启动的,否则pod就没有固定的网络标识符(域名)。
headless service定义:
# cat headless.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP None <none> 80/TCP 25s
service创建时会分配 CLUSTER-IP:
# kubectl get svc,ep
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-service NodePort 10.0.0.138 <none> 80:43431/TCP 20d
NAME ENDPOINTS AGE
endpoints/nginx-service 172.17.31.3:80,172.17.59.7:80,172.17.59.8:80 20d
2. SatefulSet应用
创建一个headless servive 和 statefulset及pod, 在创建statefulset时指定 serviceName,serviceName是用于保持Pod身份, Pod 删除重建pod NAME 不会变化。
# cat sts.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: nginx-statefulset
namespace: default
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
# kubectl create -f sts.yaml
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-statefulset-0 1/1 Running 0 98s
nginx-statefulset-1 1/1 Running 0 96s
nginx-statefulset-2 1/1 Running 0 92s
pod NAME是不变的,这样可以保证pod有一个唯一固定的域名:
# kubectl run busybox --image=busybox:1.28.4 -it --rm --restart=Never
/ # nslookup nginx-statefulset-0.nginx
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: nginx-statefulset-0.nginx
Address 1: 172.17.59.7 nginx-statefulset-0.nginx.default.svc.cluster.local
# kubectl delete pod nginx-statefulset-0
# kubectl get pod
3. 无状态与有状态服务的区别
无状态:
(1) deployment任务所有pod都是一样的
(2) 不用考虑pod启动的顺序要求
(3) 不用考虑在哪个node上运行
(4) 随意扩容/缩容
有状态:
(1) pod 之间有关系,数据完全不一致,如etcd节点之间的关系,配置不一致
(2) 实例之间不对等的关系,以及依靠外部存储的应用
(3) StatefulSet是有身份的(唯一网络标识符)
(4) 有序,优雅的部署和扩展、删除和终止
(5) 有序,滚动更新
StatefulSet身份(唯一网络标识符)三要素:
• 域名
• 主机名
• 存储(PVC)
ClusterIP A记录格式:<service-name>.<namespace-name>.svc.cluster.local
ClusterIP=None A记录格式:<statefulsetName-index>.<service-name>.<namespace-name>.svc.cluster.local
示例:nginx-statefulset-0.nginx.default.svc.cluster.local
三. DaemonSet
1. DaemonSet介绍及应用场景
• 在每一个Node上运行一个Pod
• 新加入的Node也同样会自动运行一个Pod
• 应用场景:Agent
https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
2. DaemonSet应用
# cat filebeat-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-daemonset
labels:
app: filebeat
spec:
selector:
matchLabels:
app: filebeat
template:
metadata:
labels:
app: filebeat
spec:
containers:
- name: logs
image: nginx:1.15.4
ports:
- containerPort: 80
volumeMounts:
- name: varlog
mountPath: /tmp/log
volumes:
- name: varlog
hostPath:
path: /var/log
# kubectl create -f filebeat-daemonset.yaml
daemonset.apps/filebeat-daemonset created
# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
filebeat-daemonset-987j6 1/1 Running 0 3m9s 172.17.31.12 10.40.6.210 <none>
filebeat-daemonset-pjlh4 1/1 Running 0 3m9s 172.17.59.11 10.40.6.213 <none>
# kubectl exec -it filebeat-daemonset-987j6 bash
root@filebeat-daemonset-987j6:/# ls /tmp/log/
四. Job批处理
Job分为普通任务(Job)和定时任务(CronJob)
1. 普通任务(Job)
• 一次性执行
应用场景:离线数据处理,视频解码等业务
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
# kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-6b94d96994-bhc5r 1/1 Running 0 8d
pi-vg7t9 0/1 Completed 0 4m34s
# kubectl log pi-vg7t9
https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
2. 定时任务(CronJob)
定时任务,像Linux的Crontab一样。
• 循环定时任务
应用场景:通知,备份
# cat cronjob.yaml
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
# kubectl create -f ronjob.yaml
# kubectl get cronjob
\NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 1 15s 5m53s
# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-1561304400-hbh9j 0/1 Completed 0 99s
hello-1561304460-l9tnh 0/1 Completed 0 39s
# kubectl log hello-1561304460-l9tnh
log is DEPRECATED and will be removed in a future version. Use logs instead.
Sun Jun 23 15:41:10 UTC 2019
Hello from the Kubernetes cluster
https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/
五. 小结
Deployment 无状态部署
StatefulSet 有状态部署
DaemonSet 守护进程部署
Job & CronJob 批处理