|NO.Z.00015|——————————|CloudNative|——|Kubernetes&pod操作.V03|--------------------------------------|service操作|一次性任務|定時任務|
阿新 • • 發佈:2022-03-28
[CloudNative:Kubernetes&pod操作.V03] [Applications.CloudNative] [controller Deployment概述和應用場景/釋出|service概述/三種類型|] [controller statefulset部署有狀態應用|controller daemonset部署守護程序|] [controller job和cronjob一次任務和定時任務|配置管理secret|配置管理configmap|]
一、service概述
二、services定義pod的訪問規則
三、通過yaml檔案進行部署service### --- 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檔案進行部署 ~~~ 將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
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
來自為知筆記(Wiz)