一、pod的控制器:
pod的控制器:工作负载 workload 介于k8s集群和节点之间。
中间层。
确保pod资源符合预期的状态。
pod资源出现故障的时候,会自动重启。
对基于控制器创建的pod,delete pod相当于对pod的重启,并不能删除pod
必须要把控制器删除,pod也会自动删除
1.1、pod的控制器的类型:
1、replicas
1、replicas:结合控制器一块,控制副本数量。
2、Deployment:
2、Deployment:最常见,最好用的控制器。
3、DaemonSet:
3、DaemonSet:确保在每个节点上都会部署一个pod。用于系统后台的支撑业务。
特性:服务是无状态应用。
delete pod:也相当于服务器
需要在每个节点都部署,或者是需要在后台运行的pod ELK E L
[root@master01 ~]# kubectl explain daemonset
[root@master01 k8s-yaml]# vim daemon.yaml
#定义好pvc的请求:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx1
labels:
app: nginx1
spec:
replicas: 3
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
containers:
- name: nginx1
image: nginx:1.22
[root@master01 k8s-yaml]# kubectl apply -f daemon.yaml
error: error validating "daemon.yaml": error validating data: ValidationError(DaemonSet.spec): unknown field "replicas" in io.k8s.api.apps.v1.DaemonSetSpec; if you choose to ignore these errors, turn validation off with --validate=false
##不能定义副本数
[root@master01 k8s-yaml]# vim daemon.yaml
#定义好pvc的请求:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx1
labels:
app: nginx1
spec:
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
containers:
- name: nginx1
image: nginx:1.22
[root@master01 k8s-yaml]# kubectl apply -f daemon.yaml
daemonset.apps/nginx1 created
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfs1-76f66b958-7g8jt 1/1 Running 0 17h 10.244.2.165 node02 <none> <none>
nginx1-5lb8f 1/1 Running 0 16s 10.244.1.221 node01 <none> <none>
nginx1-blf2v 1/1 Running 0 16s 10.244.2.172 node02 <none> <none>
指定node01部署
[root@master01 k8s-yaml]# vim daemon.yaml
#定义好pvc的请求:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx1
labels:
app: nginx1
spec:
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
containers:
- name: nginx1
image: nginx:1.22
nodeName: "node01"
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfs1-76f66b958-7g8jt 1/1 Running 0 17h 10.244.2.165 node02 <none> <none>
nginx1-kbgnq 1/1 Running 0 6s 10.244.1.222 node01 <none> <none>
4、StateFulSet:
4、StateFulSet:有状态的部署,pod的名称是固定的,一旦创建永久不变,增加副本数,也是有序的递增。
web-0
web-1
web-2
web-3
web-4
可用指定副本数,pod可用扩容和缩容
delete pod 也是相当于重启,只不过pod的名称不再发生变化。
主要用于数据库,数据库必须要有指定的编号。redis主从。对数据独立性要求比较高的应用。
Cluster Ip 为空 None
无头服务 :headless
有状态的副本需要持久化存储。每一个pod都有一个自己的独立的挂载卷。
hostpath
动态pv自动给每个pod创建挂载卷
[root@master01 k8s-yaml]# vim StateFulSet.yaml
#定义service
apiVersion: v1
kind: Service
metadata:
name: nginx-web
labels:
app: svc1
spec:
ports:
- port: 80
#service的暴露的端口
targetPort: 80
#容器的端口
clusterIP: None
selector:
app: nginx1
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
labels:
app: nginx1
spec:
replicas: 3
selector:
matchLabels:
app: nginx1
serviceName: "nginx-web"
#这个字段必须要加,名称和service名字一一对应
template:
metadata:
labels:
app: nginx1
spec:
terminationGracePeriodSeconds: 10
#pod被终止时,强制等待容器退出的时间是10秒,可以不加。也可以在命令行--force.
containers:
- name: nginx
image: nginx:1.22
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: ["ReadWriteMany"]
storageClassName: "nfs-client-storageclass"
resources:
requests:
storage: 1Gi
#所有的pod都会自动生成一个pv,在存储节点目录创建一个独立的数据卷,不能作为统一的数据同步到容器。
[root@master01 k8s-yaml]# kubectl apply -f StateFulSet.yaml
[root@k8s5 opt]# cd /opt/k8s/
[root@k8s5 opt]#ll
drwxrwxrwx. 2 root root 24 9月 6 10:13 default-www-web-0-pvc-b98dbae2-e4d6-43f4-95db-fbfcfd30902a
drwxrwxrwx. 2 root root 24 9月 6 10:14 default-www-web-1-pvc-807832a4-8014-4b40-b293-592e4f732bcc
drwxrwxrwx. 2 root root 24 9月 6 10:16 default-www-web-2-pvc-5c63ec61-d0f8-4e5b-a2ed-cd606a2532a0
[root@k8s5 k8s]# cd default-www-web-0-pvc-b98dbae2-e4d6-43f4-95db-fbfcfd30902a/
[root@k8s5 default-www-web-0-pvc-b98dbae2-e4d6-43f4-95db-fbfcfd30902a]# echo 123 > index.html
[root@k8s5 k8s]# cd default-www-web-1-pvc-807832a4-8014-4b40-b293-592e4f732bcc
[root@k8s5 default-www-web-1-pvc-807832a4-8014-4b40-b293-592e4f732bcc]# echo 456 > index.html
[root@k8s5 k8s]# cd default-www-web-2-pvc-5c63ec61-d0f8-4e5b-a2ed-cd606a2532a0
[root@k8s5 default-www-web-2-pvc-5c63ec61-d0f8-4e5b-a2ed-cd606a2532a0]# echo 789 > index.html
[root@master01 k8s-yaml]# kubectl get pod -o wide
web-0 1/1 Running 0 28s 10.244.2.179 node02 <none> <none>
web-1 1/1 Running 0 35s 10.244.1.225 node01 <none> <none>
web-2 1/1 Running 0 38s 10.244.2.178 node02 <none> <none>
以下
----------------------------------------
[root@master01 k8s-yaml]# kubectl get pod -o wide
web-0 1/1 Running 0 28s 10.244.2.179 node02 <none> <none>
web-1 1/1 Running 0 35s 10.244.1.225 node01 <none> <none>
web-2 1/1 Running 0 38s 10.244.2.178 node02 <none> <none>
[root@master01 k8s-yaml]# kubectl delete pod web-0
[root@master01 k8s-yaml]# kubectl get pod -o wide
web-0 1/1 Running 0 8s 10.244.2.180 node02 <none> <none>
web-1 1/1 Running 0 13m 10.244.1.225 node01 <none> <none>
web-2 1/1 Running 0 13m 10.244.2.178 node02 <none> <none>
----------------------------------------------------
以上发现web-0不变,ip在变化
[root@master01 k8s-yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10d
nginx-web ClusterIP None <none> 80/TCP 32m
nginx1 NodePort 10.96.38.241 <none> 80:30972/TCP 23h
[root@master01 k8s-yaml]# curl 192.168.168.81:30972
123
[root@master01 k8s-yaml]# curl 192.168.168.81:30972
456
[root@master01 k8s-yaml]# curl 192.168.168.81:30972
789
[root@master01 k8s-yaml]# curl 192.168.168.81:30972
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>
一台服务器布置多少pod
nginx:
deployment 2个3
6个nginx pod
service……nginx…NodePort…ingress
6个pod
kafka
3个
deployment
redis-0
redis-1
redis-2
3个
业务容器
6个
2 1
4 3
24个pod
5、job控制器:
任务类型的控制器:
普通任务 job
定时任务:CronJob
批量处理脚本:数据库迁移,视频解码一次性的任务
需要定期执行的任务,环境扫描,健康检查。
[root@master01 k8s-yaml]# vim job.yaml
#job,普通类型的任务
apiVersion: batch/v1
kind: Job
metadata:
name: centos
spec:
template:
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash","-c","test -e /etc/passwd"]
restartPolicy: Never
#job类型的pod重启只能是Never或者0nFailure
backoffLimit: 4
#表示任务失败,重试的次数,默认是6次
[root@master01 k8s-yaml]# kubectl apply -f job.yaml
job.batch/centos created
[root@master01 k8s-yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
centos-vwckq 0/1 Completed 0 11s
[root@master01 k8s-yaml]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
centos 1/1 1s 2m19s
正常运行退出
[root@master01 k8s-yaml]# vim job.yaml
#job,普通类型的任务
apiVersion: batch/v1
kind: Job
metadata:
name: centos
spec:
template:
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash","-c","test -e /etcwe/passwd"]
restartPolicy: Never
#job类型的pod重启只能是Never或者0nFailure
backoffLimit: 4
#表示任务失败,重试的次数,默认是6次
[root@master01 k8s-yaml]# kubectl get jobs.batch
NAME COMPLETIONS DURATION AGE
centos 0/1 1s 2m19s
不正常运行退出
定时调度
[root@master01 k8s-yaml]# kubectl explain CronJob
KIND: CronJob
VERSION: batch/v1beta1
[root@master01 k8s-yaml]# vim cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: test1-1
spec:
schedule: "*/1 * * * *"
#定时任务执行的时间周期。*****
jobTemplate:
#指定定时任务的执行模板,镜像和任务
spec:
template:
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash","-c","test -e /etc/passwd"]
restartPolicy: OnFailure
#重启策略只能是Never或者OnFailure
[root@master01 k8s-yaml]# kubectl apply -f cronjob.yaml
cronjob.batch/test1-1 created
[root@master01 k8s-yaml]# kubectl get pod
test1-1-1725593400-sjskn 0/1 Completed 0 3m7s
test1-1-1725593460-xbcql 0/1 Completed 0 2m7s
test1-1-1725593520-l49dw 0/1 Completed 0 67s
test1-1-1725593580-76jql 0/1 Completed 0 7
定时调度
[root@master01 k8s-yaml]# vim cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: test1-1
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Allow
startingDeadlineSeconds: 15
#要保留的失败的完成作业数 默认1个
#pod必须在15秒内开始执行定时任务的内容,超过这个时间运行,任务不运行而且标记为失败
#定时任务执行的时间周期。*****
jobTemplate:
#指定定时任务的执行模板,镜像和任务
spec:
template:
spec:
containers:
- name: centos
image: centos:7
command: ["/bin/bash","-c","test -e /etc123/passwd"]
restartPolicy: OnFailure
#重启策略只能是Never或者OnFailure
[root@master01 k8s-yaml]# kubectl apply -f cronjob.yaml
[root@master01 k8s-yaml]# kubectl get pod