k8s——控制器的种类分析介绍
这里写目录标题一、控制器1.1 Pod与控制器之间的关系1.2 Deployment 无状态1.3 SatefulSet 有状态有状态和无状态区别常规service和无头服务区别总结1.4 DaemonSet1.5 Job1.6 CronJob一、控制器控制器:又称之为工作负载,分别包含以下类型控制器1:Deployment2:StatefulSet3:DaemonSet4:Job5:CronJo
这里写目录标题
一、控制器
控制器:又称之为工作负载,分别包含以下类型控制器
1:Deployment
2:StatefulSet
3:DaemonSet
4:Job
5:CronJob
1.1 Pod与控制器之间的关系
controllers:在集群上管理和运行容器的对象通过label-selector相关联
Pod通过控制器实现应用的运维,如伸缩,升级等
1.2 Deployment 无状态
部署无状态应用
管理Pod和ReplicaSet
具有上线部署、副本设定、滚动升级、回滚等功能
提供声明式更新,例如只更新一个新的Image
应用场景:web服务
#查看控制器
[root@localhost demo]# kubectl edit deployment/nginx-deployment
spec:
progressDeadlineSeconds: 600
replicas: 3 #副本集
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate: #滚动更新策略
maxSurge: 25% #最大创建pod数
maxUnavailable: 25% #最大删除pod数
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers: # 容器
- image: nginx:1.15.4 #镜像
imagePullPolicy: IfNotPresent
name: nginx-deployment
namespace: default
resourceVersion: "880370"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-deployment
uid: d5f1ec3d-50cd-11ea-895a-000c297a15fb
[root@localhost demo]# kubectl create -f nginx-deployment.yaml
[root@master01 demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-d55b94fd-5tz7l 1/1 Running 0 3s
nginx-deployment-d55b94fd-w6zcf 1/1 Running 0 3s
nginx-deployment-d55b94fd-wcbjc 1/1 Running 0 3s
[root@master01 demo]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-d55b94fd-5tz7l 1/1 Running 0 18s
pod/nginx-deployment-d55b94fd-w6zcf 1/1 Running 0 18s
pod/nginx-deployment-d55b94fd-wcbjc 1/1 Running 0 18s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d8h
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3 3 3 3 18s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-d55b94fd 3 3 3 18s #Replicaset 是控制版本,副本数,回滚就是通过此来实现
[root@master01 demo]# kubectl rollout history deployment.apps/nginx-deployment #查看历史版本
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
1.3 SatefulSet 有状态
部署有状态应用
解决Pod独立生命周期,保持Pod启动顺序和唯一性
稳定,唯一的网络标识符,持久存储(例如:etcd配置文件,节点地址发生变化,将无法使用)
有序,优雅的部署和扩展、删除和终止(例如:mysql主从关系,先启动主,再启动从)
有序,滚动更新
应用场景:数据库
https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/ #官网文档
有状态和无状态区别
无状态:
1)deployment 认为所有的pod都是一样的
2)不用考虑顺序的要求
3)不用考虑在哪个node节点上运行
4)可以随意扩容和缩容
有状态
1)实例之间有差别,每个实例都有自己的独特性,元数据不同,例如etcd,zookeeper
2)实例之间不对等的关系,以及依靠外部存储的应用。
常规service和无头服务区别
service:一组Pod访问策略,提供cluster-IP群集之间通讯,还提供负载均衡和服务发现。
Headless service 无头服务,不需要cluster-IP,直接绑定具体的Pod的IP
[root@master01 demo]# kubectl create -f nginx-service.yaml #无状态服务,service有集群ip
service/nginx-service created
[root@master01 demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d8h
nginx-service NodePort 10.0.0.196 <none> 80:41690/TCP 11s #有集群ip
#在node节点上操作
[root@node01 ~]# systemctl restart flanneld.service
[root@node01 ~]# systemctl restart docker
[root@node01 ~]# curl 10.0.0.196
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@node02 ~]# systemctl restart flanneld.service
[root@node02 ~]# systemctl restart docker
[root@node02 ~]# curl 10.0.0.196
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#无头模式部署——headless方式(因为Pod动态IP地址,所以常用于绑定DNS访问)
[root@localhost demo]# vim headless.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None #设置为none
selector:
app: nginx
[root@master01 demo]# kubectl get service # 获取无头模式服务
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d8h
nginx ClusterIP None <none> 80/TCP 55s #集群ip为none
nginx-service NodePort 10.0.0.196 <none> 80:41690/TCP 12m
复制coredns.yaml到master01的root家目录
https://www.kubernetes.org.cn/4694.html
[root@master01 ~]# vim coredns.yaml
# Warning: This is a file generated from the base underscore template file: coredns.yaml.base
apiVersion: v1
kind: ServiceAccount
metadata:
name: coredns
namespace: kube-system
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
kubernetes.io/bootstrapping: rbac-defaults
addonmanager.kubernetes.io/mode: Reconcile
name: system:coredns
rules:
- apiGroups:
- ""
resources:
- endpoints
- services
- pods
- namespaces
verbs:
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubernetes.io/bootstrapping: rbac-defaults
addonmanager.kubernetes.io/mode: EnsureExists
name: system:coredns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:coredns
subjects:
- kind: ServiceAccount
name: coredns
namespace: kube-system
---
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
labels:
addonmanager.kubernetes.io/mode: EnsureExists
data:
Corefile: |
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
proxy . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: coredns
namespace: kube-system
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "CoreDNS"
spec:
# replicas: not specified here:
# 1. In order to make Addon Manager do not reconcile this replicas parameter.
# 2. Default is 1.
# 3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
selector:
matchLabels:
k8s-app: kube-dns
template:
metadata:
labels:
k8s-app: kube-dns
annotations:
seccomp.security.alpha.kubernetes.io/pod: 'docker/default'
spec:
serviceAccountName: coredns
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
- key: "CriticalAddonsOnly"
operator: "Exists"
containers:
- name: coredns
image: coredns/coredns:1.2.2
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 170Mi
requests:
cpu: 100m
memory: 70Mi
args: [ "-conf", "/etc/coredns/Corefile" ]
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
readOnly: true
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9153
name: metrics
protocol: TCP
livenessProbe:
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- NET_BIND_SERVICE
drop:
- all
readOnlyRootFilesystem: true
dnsPolicy: Default
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
---
apiVersion: v1
kind: Service
metadata:
name: kube-dns
namespace: kube-system
annotations:
prometheus.io/port: "9153"
prometheus.io/scrape: "true"
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "CoreDNS"
spec:
selector:
k8s-app: kube-dns
clusterIP: 10.0.0.2
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
protocol: TCP
[root@master01 ~]# kubectl create -f coredns.yaml
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
configmap/coredns created
deployment.extensions/coredns created
service/kube-dns created
[root@master01 ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-56684f94d6-wjnft 1/1 Running 0 72s
kubernetes-dashboard-7dffbccd68-n4hgt 1/1 Running 6 6d16h
[root@master01 demo]# vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
name: dns-test
spec:
containers:
- name: busybox
image: busybox:1.28.4
args:
- /bin/sh
- -c
- sleep 36000
restartPolicy: Never
[root@master01 demo]# kubectl create -f pod3.yaml
pod/dns-test created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 0 28s
[root@master01 demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d9h
nginx ClusterIP None <none> 80/TCP 30s
nginx-service NodePort 10.0.0.196 <none> 80:43943/TCP 4s
[root@master01 demo]# kubectl exec -it dns-test sh
/ # nslookup kubernetes
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: kubernetes
Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local
/ # nslookup nginx-service
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: nginx-service
Address 1: 10.0.0.196 nginx-service.default.svc.cluster.local
[root@master01 demo]# vim 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
//清理所有pod
[root@localhost demo]# kubectl delete -f .
[root@master01 demo]# kubectl create -f sts.yaml
service/nginx created
statefulset.apps/nginx-statefulset created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-statefulset-0 1/1 Running 0 4m3s
nginx-statefulset-1 1/1 Running 0 3m46s
nginx-statefulset-2 1/1 Running 0 3m22s
[root@master01 demo]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-statefulset-0 1/1 Running 0 4m30s
pod/nginx-statefulset-1 1/1 Running 0 4m13s
pod/nginx-statefulset-2 1/1 Running 0 3m49s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d9h
service/nginx ClusterIP None <none> 80/TCP 4m29s
[root@master01 demo]# kubectl apply -f pod3.yaml
pod/dns-test created
[root@master01 demo]# kubectl create -f pod3.yaml
pod/dns-test created
[root@master01 demo]# kubectl exec -it dns-test sh
/ # 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.36.2 nginx-statefulset-0.nginx.default.svc.cluster.local
/ # nslookup nginx-statefulset-1.nginx
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: nginx-statefulset-1.nginx
Address 1: 172.17.94.2 nginx-statefulset-1.nginx.default.svc.cluster.local
/ # nslookup nginx-statefulset-2.nginx
Server: 10.0.0.2
Address 1: 10.0.0.2 kube-dns.kube-system.svc.cluster.local
Name: nginx-statefulset-2.nginx
Address 1: 172.17.36.5 nginx-statefulset-2.nginx.default.svc.cluster.local
总结
StatefulSet与Deployment区别:有身份的!
身份三要素:
域名 nginx-statefulset-0.nginx
主机名 nginx-statefulset-0
存储(PVC)
1.4 DaemonSet
在每一个Node上运行一个Pod
新加入的Node也同样会自动运行一个Pod
应用场景:Agent
https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
//官方案例(监控)
[root@master01 demo]# vim ds.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
[root@master01 demo]# kubectl apply -f ds.yaml
daemonset.apps/nginx-deployment created
#DaemonSet会在每个node节点都创建一个Pod
[root@master01 demo]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-deployment-b67bq 1/1 Running 0 34s 172.17.94.4 192.168.200.110 <none>
nginx-deployment-pzmbp 1/1 Running 0 34s 172.17.36.6 192.168.200.120 <none>
1.5 Job
Job分为普通任务(Job)和定时任务(CronJob)
一次性执行
应用场景:离线数据处理,视频解码等业务
https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
[root@master01 demo]# vim 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
[root@master01 demo]# kubectl apply -f job.yaml
job.batch/pi created
[root@master01 demo]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
pi-49zcz 0/1 ContainerCreating 0 31s
pi-49zcz 1/1 Running 0 76s
pi-49zcz 0/1 Completed 0 79s
[root@master01 demo]# kubectl logs pi-49zcz
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[root@master01 demo]# kubectl get job
NAME COMPLETIONS DURATION AGE
pi 1/1 79s 2m17s
[root@master01 demo]# kubectl delete -f job.yaml
job.batch "pi" deleted
1.6 CronJob
周期性任务,像Linux的Crontab一样。
周期性任务
应用场景:通知,备份
https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/
示例:
//每分钟打印hello
[root@master01 demo]# vim 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
[root@master01 demo]# kubectl create -f cronjob.yaml
cronjob.batch/hello created
[root@master01 demo]# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 8s
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 0 16m
hello-1602735600-x7gvh 0/1 Completed 0 36s
[root@master01 demo]# kubectl logs hello-1602735600-x7gvh
Thu Oct 15 04:20:06 UTC 2020
Hello from the Kubernetes cluster
[root@master01 demo]# kubectl get pods -w 等待一分钟后又会再执行一次
NAME READY STATUS RESTARTS AGE
hello-1602735600-x7gvh 0/1 Completed 0 93s
hello-1602735660-5pz9f 0/1 Completed 0 33s
更多推荐
所有评论(0)