k8s-----------控制器
目录Deployment(部署无状态应用)Pod与控制器之间的关系SatefulSet(部署有状态应用)无状态和有状态无状态有状态常规service和无头服务区别DaemonSetjobCronJob控制器:又称之为工作负载,分别包含以下类型控制器Deployment(部署无状态应用)Pod与控制器之间的关系controllers:在集群上管理和运行容器的对象通过label-selector相关联
目录
控制器:又称之为工作负载,分别包含以下类型控制器
Deployment(部署无状态应用)
Pod与控制器之间的关系
- controllers:在集群上管理和运行容器的对象通过label-selector相关联
- Pod通过控制器实现应用的运维,如伸缩,升级等
管理Pod和ReplicaSet,具有上线部署、副本设定、滚动升级、回滚等功能,提供声明式更新,例如只更新一个新的Image
应用场景:web服务
例子:
[root@localhost demo]# kubectl create -f nginx-deployment.yaml
#Replicaset 是控制版本,副本数,回滚就是通过此来实现
[root@localhost demo]# kubectl get pods,deploy,rs
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-d55b94fd-9pwvk 1/1 Running 0 45s
pod/nginx-deployment-d55b94fd-dk4sr 1/1 Running 0 45s
pod/nginx-deployment-d55b94fd-ncrbk 1/1 Running 0 45s
pod/pod-example 1/1 Running 0 39m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx-deployment 3 3 3 3 45s
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-deployment-d55b94fd 3 3 3 45s
查看控制器
[root@localhost demo]# kubectl edit deployment/nginx-deployment
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
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 rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
SatefulSet(部署有状态应用)
解决Pod独立生命周期,保持Pod启动顺序和唯一性,稳定,唯一的网络标识符,持久存储(例如:etcd配置文件,节点地址发生变化,将无法使用),有序,优雅的部署和扩展、删除和终止(例如:mysql主从关系,先启动主,再启动从) 有序,滚动更新
应用场景:数据库
无状态和有状态
无状态
- deployment 认为所有的pod都是一样的
- 不用考虑顺序的要求
- 不用考虑在哪个node节点上运行
- 可以随意扩容和缩容
有状态
- 实例之间有差别,每个实例都有自己的独特性,元数据不同,例如etcd,zookeeper
- 实例之间不对等的关系,以及依靠外部存储的应用。
常规service和无头服务区别
service:一组Pod访问策略,提供cluster-IP群集之间通讯,还提供负载均衡和服务发现。
Headless service 无头服务,不需要cluster-IP,直接绑定具体的Pod的IP
例如:
[root@localhost demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@localhost demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 18d
nginx-service NodePort 10.0.0.202 <none> 80:32655/TCP 3m17s
//在node节点上操作(注意:coredns地址统一,否则无法解析)
[root@localhost ~]# systemctl restart flanneld.service
[root@localhost ~]# systemctl restart docker
//查看群集间通讯
[root@localhost ~]# curl 10.0.0.202
<!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
selector:
app: nginx
[root@localhost demo]# kubectl apply -f headless.yaml
service/nginx created
[root@localhost demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 19d
nginx ClusterIP None <none> 80/TCP 28s
nginx-service NodePort 10.0.0.202 <none> 80:32655/TCP 15m
复制coredns.yaml到master01的root家目录
[root@localhost ~]# ls
coredns.yaml
[root@localhost ~]# 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@localhost ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-56684f94d6-mvrw5 1/1 Running 0 2m15s
kubernetes-dashboard-7dffbccd68-txcmp 1/1 Running 2 9d
[root@localhost ~]# cd demo/
[root@localhost 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
//验证dns解析
[root@localhost demo]# kubectl create -f pod3.yaml
pod/dns-test created
[root@localhost demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 19d
nginx ClusterIP None <none> 80/TCP 24m
nginx-service NodePort 10.0.0.202 <none> 80:32655/TCP 39m
//解析kubernetes和nginx-service名称
[root@localhost 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.202 nginx-service.default.svc.cluster.local
[root@localhost 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@localhost demo]# kubectl create -f sts.yaml
statefulset.apps/nginx-statefulset created
[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-statefulset-0 1/1 Running 0 30s
nginx-statefulset-1 1/1 Running 0 17s
nginx-statefulset-2 1/1 Running 0 9s
[root@localhost demo]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-statefulset-0 1/1 Running 0 4m51s
pod/nginx-statefulset-1 1/1 Running 0 4m38s
pod/nginx-statefulset-2 1/1 Running 0 4m30s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 19d
service/nginx ClusterIP None <none> 80/TCP 64s
[root@localhost demo]# kubectl apply -f pod6.yaml
pod/dns-test created
//解析pod的唯一域名和自身的ip
[root@localhost 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.42.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.25.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.42.3 nginx-statefulset-2.nginx.default.svc.cluster.local
总结:StatefulSet与Deployment区别:有身份的! 身份三要素: 域名 nginx-statefulset-0.nginx 主机名 nginx-statefulset-0 存储(PVC)
DaemonSet
在每一个Node上运行一个Pod 新加入的Node也同样会自动运行一个Pod
应用场景:Agent
例如
[root@localhost 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@localhost demo]# kubectl apply -f ds.yaml
daemonset.apps/nginx-deployment created
//DaemonSet会在每个node节点都创建一个Pod
[root@localhost demo]# kubectl get pods
nginx-deployment-4kr6h 1/1 Running 0 35s
nginx-deployment-8jrg5 1/1 Running 0 35s
job
Job分为普通任务(Job)和定时任务(CronJob)
一次性执行
应用场景:离线数据处理,视频解码等业务
例如
示例中,重试次数默认是6次,修改为4次,当遇到异常时Never状态会重启,所以要设定次数。
[root@localhost 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
在node节点下载perl镜像,因为镜像比较大所以提前下载好
[root@localhost ~]# docker pull perl
#master
[root@localhost demo]# kubectl apply -f job.yaml
计算完成状态
[root@localhost demo]# kubectl get pods
pi-bqtf7 0/1 Completed 0 41s
结果输出到控制台
[root@localhost demo]# kubectl logs pi-bqtf7
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[root@localhost demo]# kubectl get job
NAME COMPLETIONS DURATION AGE
pi 1/1 24s 3m37s
//清除job资源
[root@localhost demo]# kubectl delete -f job.yaml
job.batch "pi" deleted
CronJob
周期性任务,像Linux的Crontab一样。 周期性任务
应用场景:通知,备份
例如:
#每分钟打印hello
[root@localhost ~]# 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@localhost demo]# kubectl get cronjob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 <none> 25s
[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-1581917340-dzxbj 0/1 Completed 0 24s
[root@localhost demo]# kubectl logs hello-1581917340-dzxbj
Mon Feb 17 05:29:09 UTC 2020
Hello from the Kubernetes cluster
//等待一分钟后又会再执行一次
[root@localhost demo]# kubectl get pods
hello-1581917340-dzxbj 0/1 Completed 0 2m45s
hello-1581917400-nkb72 0/1 Completed 0 105s
更多推荐
所有评论(0)