1. DaemonSet trong Kubernetes
DaemonSet (ds) đảm bảo chạy trên mỗi NODE một bản copy của POD. Triển khai DaemonSet khi cần ở mỗi máy (Node) một POD, thường dùng cho các ứng dụng như thu thập log, tạo ổ đĩa trên mỗi Node … Dưới đây là ví dụ về DaemonSet, nó tạo tại mỗi Node một POD chạy nginx
root@k8s-standalone:/home# kubectl get no -owide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master1 Ready control-plane,master 5d17h v1.23.8 192.168.13.211 <none> Ubuntu 18.04.6 LTS 4.15.0-191-generic containerd://1.6.8
master2 Ready control-plane,master 5d17h v1.23.8 192.168.13.212 <none> Ubuntu 18.04.6 LTS 4.15.0-191-generic containerd://1.6.8
master3 Ready control-plane,master 5d17h v1.23.8 192.168.13.213 <none> Ubuntu 18.04.6 LTS 4.15.0-191-generic containerd://1.6.8
worker1 Ready <none> 5d17h v1.23.8 192.168.13.214 <none> Ubuntu 18.04.6 LTS 4.15.0-191-generic containerd://1.6.8
worker2 Ready <none> 5d17h v1.23.8 192.168.13.215 <none> Ubuntu 18.04.6 LTS 4.15.0-191-generic containerd://1.6.8
worker3 Ready <none> 5d17h v1.23.8 192.168.13.216 <none> Ubuntu 18.04.6 LTS 4.15.0-191-generic containerd://1.6.8
Tạo file prometheus-ds.yaml
cat > ./prometheus-ds.yaml << OEF
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-prometheus
spec:
selector:
matchLabels:
app: DaemonSet-prometheus
template:
metadata:
labels:
app: DaemonSet-prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus
resources:
limits:
memory: "128Mi"
cpu: "100m"
ports:
- containerPort: 9090
OEF
Triển khai manifest
root@k8s-standalone:/home# kubectl apply -f ./prometheus-ds.yaml
daemonset.apps/dsapp created
Liệt kê các DaemonSet
root@k8s-standalone:/home# kubectl get ds -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
daemonset-prometheus 3 3 3 3 3 <none> 11s prometheus prom/prometheus app=DaemonSet-prometheus
ingress-nginx-ingress 3 3 3 3 3 <none> 5d17h ingress-nginx-ingress nginx/nginx-ingress:2.3.1 app=ingress-nginx-ingress
Liệt kê các POD theo nhãn
root@k8s-standalone:/home# kubectl get po -l "app=DaemonSet-prometheus" -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
daemonset-prometheus-9gjfv 1/1 Running 0 63s 10.244.235.135 worker1 <none> <none>
daemonset-prometheus-dsl9c 1/1 Running 0 63s 10.244.189.72 worker2 <none> <none>
daemonset-prometheus-zh79j 1/1 Running 0 63s 10.244.182.7 worker3 <none> <none>
Chi tiết về DaemonSet
root@k8s-standalone:/home# kubectl describe ds/daemonset-prometheus
Name: daemonset-prometheus
Selector: app=DaemonSet-prometheus
Node-Selector: <none>
Labels: <none>
Annotations: deprecated.daemonset.template.generation: 1
Desired Number of Nodes Scheduled: 3
Current Number of Nodes Scheduled: 3
Number of Nodes Scheduled with Up-to-date Pods: 3
Number of Nodes Scheduled with Available Pods: 3
Number of Nodes Misscheduled: 0
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=DaemonSet-prometheus
Containers:
prometheus:
Image: prom/prometheus
Port: 9090/TCP
Host Port: 0/TCP
Limits:
cpu: 100m
memory: 128Mi
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 111s daemonset-controller Created pod: daemonset-prometheus-9gjfv
Normal SuccessfulCreate 111s daemonset-controller Created pod: daemonset-prometheus-dsl9c
Normal SuccessfulCreate 111s daemonset-controller Created pod: daemonset-prometheus-zh79j
Xóa DaemonSet
root@k8s-standalone:/home# kubectl delete ds/daemonset-prometheus
daemonset.apps "daemonset-prometheus" deleted
2. Job trong Kubernetes
Job (jobs) có chức năng tạo các POD đảm bảo nó chạy và kết thúc thành công. Khi các POD do Job tạo ra chạy và kết thúc thành công thì Job đó hoàn thành. Khi bạn xóa Job thì các Pod nó tạo cũng xóa theo. Một Job có thể tạo các Pod chạy tuần tự hoặc song song. Sử dụng Job khi muốn thi hành một vài chức năng hoàn thành xong thì dừng lại (ví dụ backup, kiểm tra …)
Khi Job tạo Pod, Pod chưa hoàn thành nếu Pod bị xóa, lỗi Node … nó sẽ thực hiện tạo Pod khác để thi hành tác vụ.
Tạo file manifest nội dung như dưới
cat > ./busybox-jod.yaml >> OEF
apiVersion: batch/v1
kind: Job
metadata:
name: myjob
spec:
# Số lần chạy POD thành công
completions: 10
# Số lần tạo chạy lại POD bị lỗi, trước khi đánh dấu job thất bại
backoffLimit: 3
# Số POD chạy song song
parallelism: 2
# Số giây tối đa của JOB, quá thời hạn trên hệ thống ngắt JOB
activeDeadlineSeconds: 120
template:
spec:
containers:
- name: busybox
image: busybox
command:
- /bin/sh
- -c
- date; echo "Job executed"
restartPolicy: Never
OEF
Triển khai 1 job
root@k8s-standalone:/home# kubectl apply -f busybox-jod.yaml
job.batch/myjob created
Thông tin job có tên myjob
root@k8s-standalone:/home# kubectl describe job/myjob
Name: myjob
Namespace: default
Selector: controller-uid=44eba19f-c0e9-493e-94a6-7e7cb3ef91b2
Labels: controller-uid=44eba19f-c0e9-493e-94a6-7e7cb3ef91b2
job-name=myjob
Annotations: <none>
Parallelism: 2
Completions: 10
Completion Mode: NonIndexed
Start Time: Mon, 03 Oct 2022 08:33:25 +0000
Completed At: Mon, 03 Oct 2022 08:34:04 +0000
Duration: 39s
Active Deadline Seconds: 120s
Pods Statuses: 0 Active / 10 Succeeded / 0 Failed
Pod Template:
Labels: controller-uid=44eba19f-c0e9-493e-94a6-7e7cb3ef91b2
job-name=myjob
Containers:
busybox:
Image: busybox
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
date; echo "Job executed"
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 41s job-controller Created pod: myjob-8wk4g
Normal SuccessfulCreate 41s job-controller Created pod: myjob-rvkv2
Normal SuccessfulCreate 30s job-controller Created pod: myjob-wlfdl
Normal SuccessfulCreate 27s job-controller Created pod: myjob-ptnpq
Normal SuccessfulCreate 23s job-controller Created pod: myjob-jgkmr
Normal SuccessfulCreate 20s job-controller Created pod: myjob-9gmg5
Normal SuccessfulCreate 17s job-controller Created pod: myjob-sh4s8
Normal SuccessfulCreate 14s job-controller Created pod: myjob-5ldmw
Normal SuccessfulCreate 11s job-controller Created pod: myjob-98wzb
Normal SuccessfulCreate 8s job-controller (combined from similar events): Created pod: myjob-z9fmj
Normal Completed 2s job-controller Job completed
Các job đã hoàn thành
root@k8s-standalone:/home# kubectl get po -l 'job-name=myjob'
NAME READY STATUS RESTARTS AGE
myjob-5ldmw 0/1 Completed 0 2m23s
myjob-8wk4g 0/1 Completed 0 2m50s
myjob-98wzb 0/1 Completed 0 2m20s
myjob-9gmg5 0/1 Completed 0 2m29s
myjob-jgkmr 0/1 Completed 0 2m32s
myjob-ptnpq 0/1 Completed 0 2m36s
myjob-rvkv2 0/1 Completed 0 2m50s
myjob-sh4s8 0/1 Completed 0 2m26s
myjob-wlfdl 0/1 Completed 0 2m39s
myjob-z9fmj 0/1 Completed 0 2m17s
3. CronJob trong Kubernetes
CronJob (cj) – chạy các Job theo một lịch định sẵn. Việc lên lịch cho CronJob khai báo giống Cron của Linux
Tạo file manifest như dưới
cat > ./busybox-mycronjob.yaml << OEF
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: mycronjob
spec:
# Một phút chạy một Job
schedule: "*/1 * * * *"
# Số Job lưu lại
successfulJobsHistoryLimit: 3
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- date; echo "Job in CronJob"
restartPolicy: Never
OEF
Triển khai nó
root@k8s-standalone:/home# kubectl apply -f ./busybox-mycronjob.yaml
cronjob.batch/mycronjob created
Danh sách các cronjob
root@k8s-standalone:/home# kubectl get cj -o wide
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE CONTAINERS IMAGES SELECTOR
mycronjob */1 * * * * False 0 <none> 10s busybox busybox <none>
Danh sách các Job
root@k8s-standalone:/home# kubectl get jobs -o wide
NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
mycronjob-27746440 1/1 6s 71s busybox busybox controller-uid=154e14bb-87d8-4f86-953b-eecf5771a9c0
mycronjob-27746441 1/1 7s 11s busybox busybox controller-uid=cb8c66f1-f239-4b6a-9d7b-ef5a820f2e19
Danh sách các cronjob đã hoàn thành
root@k8s-standalone:/home# kubectl get po
NAME READY STATUS RESTARTS AGE
mycronjob-27746440-8d5p5 0/1 Completed 0 2m11s
mycronjob-27746441-wkstq 0/1 Completed 0 71s
mycronjob-27746442-krqkt 0/1 Completed 0 11s