k8s的job任务

/ k8s / 没有评论 / 77浏览

Environment

k8s集群 1.15版本

有网

docker :18.06

Foreword

容器按照持续运行的时间可分为两类:服务类容器和工作类容器。

服务类容器通常持续提供服务,需要一直运行,比如 http server,daemon 等。工作类容器则是一次性任务,比如批处理程序,完成后容器就退出。

Kubernetes 的 Deployment、ReplicaSet 和 DaemonSet 都用于管理服务类容器;对于工作类容器,我们用 Job。

【单词】

completions n.完成( completion的名词复数 )结束(房地产等的)完成交易,完成交割

parallelism 美: [ˈpærəlɛlˌɪzəm]n.平行,对应,类似

job yaml templet

# cat echo.yaml
apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  completions: 6
  parallelism: 2
  template:
    metadata:
      name: myjob
    spec:
      containers:
      - name: hello
        image: busybox
        command: ["echo", "hello CloudDeveloper"]
      restartPolicy: OnFailure
# kubectl apply -f echo.yaml

The current pod status

2个pod并行运行,总共运行3轮,总共运行6次

# kubectl get pod -A
NAMESPACE     NAME                           READY   STATUS              RESTARTS   AGE
default       myjob-2d9wl                    0/1     Completed           0          20s
default       myjob-6r2nr                    0/1     ContainerCreating   0          1s
default       myjob-fwhjd                    0/1     Completed           0          13s
default       myjob-lnf47                    0/1     Completed           0          68s
default       myjob-swljr                    0/1     ContainerCreating   0          7s
default       myjob-zvqdc                    0/1     Completed           0          68s
kube-system   coredns-5c98db65d4-stxrq       1/1     Running             1314       5d23h
kube-system   coredns-5c98db65d4-vscdl       1/1     Running             1305       5d23h
kube-system   etcd-adou                      1/1     Running             2          5d23h
kube-system   kube-apiserver-adou            1/1     Running             1172       5d23h
kube-system   kube-controller-manager-adou   1/1     Running             4          5d23h
kube-system   kube-flannel-ds-amd64-xqtf9    1/1     Running             2          5d23h
kube-system   kube-proxy-8k2ch               1/1     Running             2          5d23h
kube-system   kube-scheduler-adou            1/1     Running             4          5d23h
ns-monitor    grafana-7bcb754f9d-wbmnh       1/1     Running             1          5d21h
ns-monitor    node-exporter-k4c7j            1/1     Running             5          5d22h
ns-monitor    prometheus-b54b8f85-t4brv      1/1     Running             6          5d22h


# kubectl get pod -A
NAMESPACE     NAME                           READY   STATUS      RESTARTS   AGE
default       myjob-2d9wl                    0/1     Completed   0          72s
default       myjob-6r2nr                    0/1     Completed   0          53s
default       myjob-fwhjd                    0/1     Completed   0          65s
default       myjob-lnf47                    0/1     Completed   0          2m
default       myjob-swljr                    0/1     Completed   0          59s
default       myjob-zvqdc                    0/1     Completed   0          2m
kube-system   coredns-5c98db65d4-stxrq       1/1     Running     1314       5d23h
kube-system   coredns-5c98db65d4-vscdl       1/1     Running     1305       5d23h
kube-system   etcd-adou                      1/1     Running     2          5d23h
kube-system   kube-apiserver-adou            1/1     Running     1172       5d23h
kube-system   kube-controller-manager-adou   1/1     Running     4          5d23h
kube-system   kube-flannel-ds-amd64-xqtf9    1/1     Running     2          5d23h
kube-system   kube-proxy-8k2ch               1/1     Running     2          5d23h
kube-system   kube-scheduler-adou            1/1     Running     4          5d23h
ns-monitor    grafana-7bcb754f9d-wbmnh       1/1     Running     1          5d21h
ns-monitor    node-exporter-k4c7j            1/1     Running     5          5d22h
ns-monitor    prometheus-b54b8f85-t4brv      1/1     Running     6          5d22h

The current docker status

运行完就自动退出,状态为Exited

# docker ps -a |grep hello
b3adfa19c76f        busybox                "echo 'hello CloudDe…"   2 minutes ago       Exited (0) 2 minutes ago                         k8s_hello_myjob-6r2nr_default_29e2054c-0c34-4fd2-b22d-aacacd5c5bb9_0
38e6f4f9cd00        busybox                "echo 'hello CloudDe…"   2 minutes ago       Exited (0) 2 minutes ago                         k8s_hello_myjob-swljr_default_2b7367d9-ecf1-4a1a-bf7c-4b768a872ee5_0
491be5406d79        busybox                "echo 'hello CloudDe…"   2 minutes ago       Exited (0) 2 minutes ago                         k8s_hello_myjob-fwhjd_default_4e343d03-b598-435d-90ae-df2d99b4271f_0
1434bc5cd1e9        busybox                "echo 'hello CloudDe…"   2 minutes ago       Exited (0) 2 minutes ago                         k8s_hello_myjob-2d9wl_default_f32ec2c6-16d6-4fc1-b282-d8a560b2fc78_0
d25438091196        busybox                "echo 'hello CloudDe…"   2 minutes ago       Exited (0) 2 minutes ago                         k8s_hello_myjob-lnf47_default_6b704542-7796-4fc5-b507-cdd8b3edcbba_0
1c1cfe9938c4        busybox                "echo 'hello CloudDe…"   2 minutes ago       Exited (0) 2 minutes ago                         k8s_hello_myjob-zvqdc_default_bb1308f1-4b3d-432e-9c89-21eda9e9c797_0

The current job source of k8s

# kubectl get job -A -o wide
NAMESPACE   NAME    COMPLETIONS   DURATION   AGE    CONTAINERS   IMAGES    SELECTOR
default     myjob   6/6           79s        5m3s   hello        busybox   controller-uid=c4db6b2d-810a-47bd-aa0e-445ea1fc58ed

Logs of job

# kubectl logs -f myjob-2d9wl
hello CloudDeveloper

对于 Job,只能设置为 Never 或者 OnFailure,失败不重启策略和失败重启策略

Clear job source

# kubectl delete -f echo.yaml
job.batch "myjob" deleted