一、service概述
![](https://i-blog.csdnimg.cn/blog_migrate/b0a0bd11daee8a3ca268472dd04639d6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/705bd9181742e07c3f93e2f4f7ad6bbb.png)
![](https://i-blog.csdnimg.cn/blog_migrate/5a5011967f2852b119f4ff8e79ee7097.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b69795ca03c77bfafff2daff23bf06e4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c708ed6a44de5dde6bcd42fbe0f25ff5.png)
二、services定义pod的访问规则
### --- services定义pod的访问规则
### --- 防止pod失联
~~~ 某一个服务想要访问到哪一个Pod,由servvices来负责,起到一个负载均衡的作用。
~~~ 定义Pod的负载均衡。
~~~ services存在的意义:防止pod失联,pod负载均衡的作用
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-4msbd 1/1 Running 0 133m 10.244.1.5 k8s-node1 <none> <none>
web-bbcf684cb-2g2wr 1/1 Running 0 104m 10.244.2.13 k8s-node2 <none> <none>
web-bbcf684cb-672j9 1/1 Running 0 104m 10.244.1.11 k8s-node1 <none> <none>
web-bbcf684cb-bjbrj 1/1 Running 0 104m 10.244.2.14 k8s-node2 <none> <none>
web-bbcf684cb-ctpv2 1/1 Running 0 104m 10.244.2.15 k8s-node2 <none> <none>
web-bbcf684cb-hr8j5 1/1 Running 0 104m 10.244.1.9 k8s-node1 <none> <none>
web-bbcf684cb-jbm8l 1/1 Running 0 104m 10.244.1.12 k8s-node1 <none> <none>
web-bbcf684cb-rn4pt 1/1 Running 0 104m 10.244.1.10 k8s-node1 <none> <none>
web-bbcf684cb-vr2pb 1/1 Running 0 104m 10.244.2.16 k8s-node2 <none> <none>
web-bbcf684cb-w894j 1/1 Running 0 107m 10.244.1.8 k8s-node1 <none> <none>
web-bbcf684cb-xgdfq 1/1 Running 0 104m 10.244.2.12 k8s-node2 <none> <none>
三、通过yaml文件进行部署service
### --- 通过yaml文件进行部署
~~~ 将sts.yaml上传到服务器;创建三个副本,三个pod
~~~ 查看pod,每个pod的名称为唯一的
[root@k8s-master ~]# 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/v1
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
[root@k8s-master ~]# kubectl apply -f sts.yaml
service/nginx created
statefulset.apps/nginx-statefulset created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-statefulset-0 1/1 Running 0 2m27s
nginx-statefulset-1 1/1 Running 0 119s
nginx-statefulset-2 1/1 Running 0 88s
~~~ 查看创建之后的services,services是无头的
~~~ None表示为无头的
~~~ 唯一个网络标识,如何约定的
[root@k8s-master ~]# kubectl get svc
nginx ClusterIP None <none> 80/TCP 4m43s
### --- 确保所有node运行在同一个pod中
~~~ 通过DaemonSet部署所有node运行在同一个pod当中
~~~ 将ds.yaml上传到服务器
[root@k8s-master ~]# cat ds.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: ds-test
labels:
app: filebeat
spec:
selector:
matchLabels:
app: filebeat
template:
metadata:
labels:
app: filebeat
spec:
containers:
- name: logs
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: varlog
mountPath: /tmp/log
volumes:
- name: varlog
hostPath:
path: /var/log
~~~ 删除所有的pod
[root@k8s-master ~]# kubectl delete statefulset --all
statefulset.apps "nginx-statefulset" deleted
[root@k8s-master ~]# kubectl delete svc nginx
service "nginx" deleted
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d5h
~~~ 删除所有的pod
[root@k8s-master ~]# kubectl delete pod --all
~~~ 执行ds.yaml文件
[root@k8s-master ~]# kubectl apply -f ds.yaml
daemonset.apps/ds-test created
[root@k8s-master ~]# kubectl get pods
ds-test-fv2kx 0/1 ContainerCreating 0 20s
ds-test-gzxlq 0/1 ContainerCreating 0 20s
~~~ 进入某一个pod里面,查看日志
~~~ 进入pod里面
[root@k8s-master ~]# kubectl exec -it ds-test-fv2kx bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@ds-test-fv2kx:/# ls /tmp/log/ # 已经采集到日志数据
anaconda boot.log btmp cron dmesg.old grubby_prune_debug maillog pods secure tallylog vmware-vgauthsvc.log.0 wtmp
audit boot.log-20210222 containers dmesg firewalld lastlog messages rhsm spooler tuned vmware-vmsvc.log yum.log
四、一次性任务
### --- job(一次性任务)
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
ds-test-fv2kx 1/1 Running 0 4m3s
ds-test-gzxlq 1/1 Running 0 4m3s
~~~ 创建一次性任务;将该文件job.yaml上传到服务器中
[root@k8s-master ~]# 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@k8s-master ~]# kubectl create -f job.yaml
job.batch/pi created
[root@k8s-master ~]# kubectl get pods
pi-p4tlp 0/1 ContainerCreating 0 13s
### --- 查看定时任务
[root@k8s-master ~]# kubectl get jobs
NAME COMPLETIONS DURATION AGE
pi 1/1 2m8s 2m10s
~~~ 查看pod属于哪个node节点
[root@k8s-master ~]# kubectl get pods -o wide
pi-p4tlp 0/1 running 0 64s <none> k8s-node1 <none> <none>
~~~ 可以查看到镜像下载状态
[root@k8s-node1 ~]# docker pull perl
~~~ 因为它是一次性任务,所以会显示Completed
[root@k8s-master ~]# kubectl get pods
pi-p4tlp 0/1 Completed 0 13m
### --- 通过日志查看它的运算
[root@k8s-master ~]# kubectl logs pi-p4tlp
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
~~~ 删除掉job.yaml文件后,才会删除掉pods
[root@k8s-master ~]# kubectl delete -f job.yaml
job.batch "pi" deleted
[root@k8s-master ~]# kubectl get pods
五、定时任务
### --- controller(定时任务)
~~~ 将cronjob.yaml上传到服务器
[root@k8s-master ~]# 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
[root@k8s-master ~]# kubectl apply -f cronjob.yaml
cronjob.batch/hello created
[root@k8s-master ~]# kubectl get pods
hello-1614003120-5tmz6 0/1 ContainerCreating 0 10s
[root@k8s-master ~]# kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 1 51s 107s
[root@k8s-master ~]# kubectl logs hello-1614003120-5tmz6
Mon Feb 22 14:12:51 UTC 2021
Hello from the Kubernetes cluster
### --- 每隔一段时间执行一次,历史的会变成completed
[root@k8s-master ~]# kubectl get pods
hello-1614003120-5tmz6 0/1 Completed 0 86s
hello-1614003180-2fppm 0/1 ContainerCreating 0 26s