前言:控制器:又称之为工作负载,分别包含以下类型控制器
1:Deployment
2:StatefulSet
3:DaemonSet
4:Job
5:CronJob
无状态:任意一个Web请求端提出请求时,请求本身包含了响应端为响应这一请求所需的全部信息(认证信息等)
有状态:Web请求端的请求必须被提交到保存有其相关状态信息(比如session)的服务器上,否则这些请求可能无法被理解,这也就意味着在此模式下服务器端无法对用户请求进行自由调度
再说直白一点就是状态(公共交互)信息是由请求方还是响应方负责保存,请求方保存就是无状态,响应方保存就是有状态
无状态应用不关心响应方是谁,需不需要同步各个响应方之间的信息,响应服务可随时被删除也不会影响别人,容错性高,分布式服务的负载均衡失效不会丢数据,无内存消耗,直接部署上线即可使用
有状态应用需要及时同步数据,可能存在数据同步不玩丢失数据,消耗内存资源保存数据等
简单的说:
Deployment为Pod和Replica Set(下一代Replication Controller)提供声明式更新
你只需要在Deployment中描述你想要的目标状态是什么,Deployment controller就会帮你将Pod和Replica Set的实际状态改变到你的目标状态。你可以定义一个全新的Deployment,也可以创建一个新的替换旧的Deployment
一个典型的用例如下:
应用场景:web服务
[root@master01 demo]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment 类型为deployment控制器
metadata:
name: nginx-deployment
labels:
app: nginx //标签要一致
spec:
replicas: 3 //Replicaset 是控制版本,副本数,回滚就是通过此来实现
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 create -f nginx-deployment.yaml //创建
deployment.apps/nginx-deployment created
[root@master01 demo]# kubectl get pods,deploy,rs //查看pod资源,控制器资源,副本集资源
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-d55b94fd-2lmwz 1/1 Running 0 29s
pod/nginx-deployment-d55b94fd-9fn5w 1/1 Running 0 29s
pod/nginx-deployment-d55b94fd-wmr5n 1/1 Running 0 29s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx-deployment 3 3 3 3 29s
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-deployment-d55b94fd 3 3 3 29s
[root@master01 demo]# kubectl edit deployment/nginx-deployment
。。。省略部分内容
spec:
progressDeadlineSeconds: 600
replicas: 3 //副本集为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
ports:
- containerPort: 80
protocol: TCP
resources: {}
。。。省略部分内容
//查看历史版本
[root@master01 demo]# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
1 //当前版本已保存
StatefulSet是为了解决有状态服务的问题(对应Deployments和ReplicaSets是为无状态服务而设计),其应用场景包括
从上面的应用场景可以发现,StatefulSet由以下几个部分组成:
StatefulSet中每个Pod的DNS格式为statefulSetName-{0…N-1}.serviceName.namespace.svc.cluster.local,其中
应用场景:数据库
官方资源:https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
[root@master01 demo]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx
[root@master01 demo]# kubectl create -f nginx-service.yaml //创建
service/nginx-service created
[root@master01 demo]# kubectl get svc //查看service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 25d
nginx-service NodePort 10.0.0.98 80:30249/TCP 5s
//重启网络以及docker
[root@node1 ~]# systemctl restart flanneld.service
[root@node1 ~]# systemctl restart docker
//查看群集间通讯
[root@node1 ~]# curl 10.0.0.98
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
//访问有效,说明服务已经提供出来
[root@master01 ~]# 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 ~]# kubectl apply -f headless.yaml
service/nginx created
[root@master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 25d
nginx ClusterIP None 80/TCP 13s //无头服务创建成功,群集地址为None
[root@master01 demo]# ls
coredns.yaml //dnsyaml文件
[root@master01 demo]# 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 demo]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-56684f94d6-crx4j 1/1 Running 0 29s
[root@master01 demo]# vim pod3.yaml //创建一个dns资源
apiVersion: v1
kind: Pod
metadata:
name: dns-test
spec:
containers:
- name: busybox
image: busybox:1.28.4 //镜像为busybox
args:
- /bin/sh
- -c
- sleep 36000
restartPolicy: Never
[root@master01 demo]# kubectl apply -f pod3.yaml
pod/dns-test created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 0 26s
[root@master01 demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 25d
nginx ClusterIP None 80/TCP 11m
nginx-service NodePort 10.0.0.98 80:30249/TCP 3h15m
[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.98 nginx-service.default.svc.cluster.local
[root@master01 ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.170.128:6443,192.168.170.129:6443 25d
nginx-service 172.17.12.2:80,172.17.12.3:80,172.17.86.2:80 170m
[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@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 23m
nginx-statefulset-1 1/1 Running 0 23m
nginx-statefulset-2 1/1 Running 0 22m
[root@master01 demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 25d
nginx ClusterIP None 80/TCP 24m
[root@master01 demo]# kubectl apply -f pod3.yaml //部署一个dns测试的资源
pod/dns-test created
//解析pod的唯一域名和自身的ip
[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.86.4 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.100.3 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.86.3 nginx-statefulset-2.nginx.default.svc.cluster.local
//可以看到每个资源解析出来的地址都是pod资源的地址
[root@master01 demo]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.170.128:6443,192.168.170.129:6443 25d
nginx 172.17.100.3:80,172.17.86.3:80,172.17.86.4:80 56m
应用场景:Agent
官方资源:https://kubernetes.io/docs/concepts/workloads/controllers/daemonset/
官方案例:(监控)
[root@localhost demo]# vim ds.yaml
apiVersion: apps/v1
kind: DaemonSet //控制器类型为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
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-4qfwv 1/1 Running 0 31s
nginx-deployment-rgvw4 1/1 Running 0 31s
//DaemonSet会在每个node节点都创建一个Pod
应用场景:离线数据处理,视频解码等业务
官方资源:https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
//重试次数默认是6次,修改为4次,当遇到异常时Never状态会重启,所以要设定次数
[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@node1 ~]# docker pull perl
Using default tag: latest
latest: Pulling from library/perl
376057ac6fa1: Pull complete
5a63a0a859d8: Pull complete
496548a8c952: Pull complete
2adae3950d4d: Pull complete
0ed5a9824906: Pull complete
24bc7d866f19: Pull complete
fab10d7383f1: Pull complete
Digest: sha256:047cbc54c1d8bc602c10fcb27357532be6ee4ee3dd7f438b72e4e60789f7e1bc
Status: Downloaded newer image for perl:latest
docker.io/library/perl:latest
[root@master01 demo]# kubectl apply -f job.yaml
job.batch/pi created
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pi-g5cs2 0/1 Completed 0 30s
//状态为comleted说明已经计算完成
[root@master01 demo]# kubectl logs pi-g5cs2
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[root@master01 demo]# kubectl get job
NAME COMPLETIONS DURATION AGE
pi 1/1 21s 1m36s
//清除job资源
[root@master01 demo]# kubectl delete -f job.yaml
job.batch "pi" deleted
官方资源:https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/
[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 apply -f cronjob.yaml
cronjob.batch/hello created
[root@master01 demo]# kubectl get cronjob //查看设置的计划
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 9s
[root@master01 demo]# kubectl get pods //状态表示已经完成一次计划
NAME READY STATUS RESTARTS AGE
hello-1590463740-6tdnw 0/1 Completed 0 24s
[root@master01 demo]# kubectl log hello-1590463740-6tdnw //查看资源的日志可以验证结果
log is DEPRECATED and will be removed in a future version. Use logs instead.
Tue May 26 03:29:20 UTC 2020
Hello from the Kubernetes cluster
//等待一分钟后又会再执行一次
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-1590463740-6tdnw 0/1 Completed 0 94s
hello-1590463800-bxgqn 0/1 Completed 0 34s
//因为设定的每分钟执行一次,所以每过一分钟都会执行一次
[root@master01 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-1590463800-bxgqn 0/1 Completed 0 2m32s
hello-1590463860-4jjlz 0/1 Completed 0 92s
hello-1590463920-kbv8q 0/1 Completed 0 32s