返回总目录页

kubernetes运行应用Controller3之Job、CronJob详解

 

成功启动一个Job

1、Job.spec.template.metadata,没有空格符的错误

[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml 
apiVersion: batch/v1
kind: Job
metadata:
  name: mcwJob
spec:
  template:
    metadata:
      name:mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["echo","hello k8s job!"]
      restartPolicy: Never
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml 
error: error validating "mcwJob1.yml": error validating data: ValidationError(Job.spec.template.metadata): invalid type for io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta: got "string", expected "map"; if you choose to ignore these errors, turn validation off with --validate=false


Job.metadata.name 名字大写是错误的,Job.spec.template.metadata.name大写是可以的
[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml 
apiVersion: batch/v1
kind: Job
metadata:
  name: mcwJob
spec:
  template:
    metadata:
      name: mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["echo","hello k8s job!"]
      restartPolicy: Never
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml 
The Job "mcwJob" is invalid: metadata.name: Invalid value: "mcwJob": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')

正确编写格式的yml文件

[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml  #文件
apiVersion: batch/v1
kind: Job
metadata:
  name: mcw-job
spec:
  template:
    metadata:
      name: mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["echo","hello k8s job!"]
      restartPolicy: Never
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml  #启动Job
job.batch/mcw-job created
[machangwei@mcwk8s-master ~]$ kubectl get job  #查看Job的状态
NAME      COMPLETIONS   DURATION   AGE
mcw-job   0/1           16s        16s
[machangwei@mcwk8s-master ~]$ kubectl get pod ##已经完成的Job
NAME            READY   STATUS      RESTARTS   AGE
mcw-job-j8jtl   0/1     Completed   0          28s
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get job #已经完成
NAME      COMPLETIONS   DURATION   AGE
mcw-job   1/1           24s        9m7s
[machangwei@mcwk8s-master ~]$ kubectl logs mcw-job-j8jtl  #由于Firewalls阻挡,没有查出日志
Error from server: Get "https://10.0.0.6:10250/containerLogs/default/mcw-job-j8jtl/hello": dial tcp 10.0.0.6:10250: connect: no route to host
[machangwei@mcwk8s-master ~]$ kubectl logs mcw-job-j8jtl #去节点上将这个防火墙停掉,然后查看到日志打印信息
hello k8s job!
[machangwei@mcwk8s-master ~]$ 

Pod失败的情况

1、restartPolicy: Never

将command 中echo修改为invald_command
[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml
apiVersion: batch/v1
kind: Job
metadata:
  name: mcw-job
spec:
  template:
    metadata:
      name: mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["invald_command","hello k8s job!"]
      restartPolicy: Never
[machangwei@mcwk8s-master ~]$ kubectl delete -f mcwJob1.yml  #修改后,以文件做删除操作不影响。
job.batch "mcw-job" deleted
[machangwei@mcwk8s-master ~]$ kubectl get pod  
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml  #启动Job
job.batch/mcw-job created
[machangwei@mcwk8s-master ~]$ kubectl get job  #0/1 Job应该有一个,成功0个
NAME      COMPLETIONS   DURATION   AGE
mcw-job   0/1           14s        14s
[machangwei@mcwk8s-master ~]$ kubectl get job -o wide
NAME      COMPLETIONS   DURATION   AGE   CONTAINERS   IMAGES    SELECTOR
mcw-job   0/1           25s        25s   hello        busybox   controller-uid=1c2444d0-4f2f-4080-b13b-90a2f1c60fe7
[machangwei@mcwk8s-master ~]$ kubectl get pod  #查看pod,很多个失败的.当第一个POD启动时容器失败退出,因为设置重启策略是从不,
NAME            READY   STATUS               RESTARTS   AGE #此失败容器不会重启,但是Job的pod需要成功一个目前没有成功的,
mcw-job-9bmvs   0/1     ContainerCannotRun   0          34s #不满足,索引controller会启动新的POD,直到有一个成功。
mcw-job-kz6s4   0/1     ContainerCreating    0          17s  #而这里这个例子是永远不会成功
mcw-job-qxhs9   0/1     ContainerCannotRun   0          53s
[machangwei@mcwk8s-master ~]$ kubectl describe pod mcw-job-qxhs9 #查看一个失败pod详情
Name:         mcw-job-qxhs9
Namespace:    default
Priority:     0
Node:         mcwk8s-node2/10.0.0.6
Start Time:   Tue, 18 Jan 2022 22:24:41 +0800
Labels:       controller-uid=1c2444d0-4f2f-4080-b13b-90a2f1c60fe7
              job-name=mcw-job
Annotations:  <none>
Status:       Failed
IP:           10.244.0.23
IPs:
  IP:           10.244.0.23
Controlled By:  Job/mcw-job
Containers:
  hello:
    Container ID:  docker://4b795658c37e4a29fc410dd0a81a2fff6977393cf35b58bfea168e10c8e0b30e
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Port:          <none>
    Host Port:     <none>
    Command:
      invald_command
      hello k8s job!
    State:          Terminated
      Reason:       ContainerCannotRun
      Message:      OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "invald_command": executable file not found in $PATH: unknown
      Exit Code:    127
      Started:      Tue, 18 Jan 2022 22:24:58 +0800
      Finished:     Tue, 18 Jan 2022 22:24:58 +0800
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7jxxj (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-7jxxj:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age   From               Message
  ----     ------     ----  ----               -------
  Normal   Scheduled  79s   default-scheduler  Successfully assigned default/mcw-job-qxhs9 to mcwk8s-node2
  Normal   Pulling    79s   kubelet            Pulling image "busybox"
  Normal   Pulled     63s   kubelet            Successfully pulled image "busybox" in 16.107623367s
  Normal   Created    63s   kubelet            Created container hello
  Warning  Failed     62s   kubelet            Error: failed to start container "hello": Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "invald_command": executable file not found in $PATH: unknown   #失败时执行文件没有找到,符合预期

2、restartPolicy: OnFailure

[machangwei@mcwk8s-master ~]$ kubectl delete -f mcwJob1.yml  #将之前的删掉
job.batch "mcw-job" deleted
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ vim mcwJob1.yml 
[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml #修改重启策略为OnFailure,
apiVersion: batch/v1
kind: Job
metadata:
  name: mcw-job
spec:
  template:
    metadata:
      name: mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["invald_command","hello k8s job!"]
      restartPolicy: OnFailure
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml #启动Job
job.batch/mcw-job created
[machangwei@mcwk8s-master ~]$ kubectl get job 
NAME      COMPLETIONS   DURATION   AGE
mcw-job   0/1           9s         9s
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME            READY   STATUS              RESTARTS     AGE
mcw-job-dmxrj   0/1     RunContainerError   0 (9s ago)   26s
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get pod  #过一段时间查看POD,没有启动成功,会重启.(RESTARTS增加)
NAME            READY   STATUS             RESTARTS      AGE
mcw-job-dmxrj   0/1     CrashLoopBackOff   1 (21s ago)   54s
[machangwei@mcwk8s-master ~]$ kubectl get pod #再过段时间查看,已经没有pod了,也没有重启
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get job #job还在。这是设置的重启策略,失败的时候,默认最多重启三次,三次重启后还不行就不会重启了
NAME      COMPLETIONS   DURATION   AGE
mcw-job   0/1           14m        14m

Job的并行性(并行处理场景,批处理程序)

如果不指定paralelism和completions,默认值均为1

1、paralelism,Job中并发pod数量

[machangwei@mcwk8s-master ~]$ vim mcwJob1.yml
[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml  #添加paralelism,设置并行Pod数量为2,
apiVersion: batch/v1
kind: Job
metadata:
  name: mcw-job
spec:
  parallelism: 2
  template:
    metadata:
      name: mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["echo","hello k8s job!"]
      restartPolicy: OnFailure
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml 
job.batch/mcw-job created
[machangwei@mcwk8s-master ~]$ kubectl get job 
NAME      COMPLETIONS   DURATION   AGE
mcw-job   0/1 of 2      7s         7s
[machangwei@mcwk8s-master ~]$ kubectl get job  #这里显示1/1 of 2 
NAME      COMPLETIONS   DURATION   AGE
mcw-job   1/1 of 2      19s        19s
[machangwei@mcwk8s-master ~]$ kubectl get pod  #Job一共启动了两个pod,而且age相同,可见是并行运行的。
NAME            READY   STATUS      RESTARTS   AGE
mcw-job-d726g   0/1     Completed   0          34s
mcw-job-wlnp4   0/1     Completed   0          34s
[machangwei@mcwk8s-master ~]$ kubectl get job #这里显示2/1 of 2 

NAME      COMPLETIONS   DURATION   AGE
mcw-job   2/1 of 2      19s        51s

2、completions  Job中成功完成pod总数,可以配合使用parallelism

[machangwei@mcwk8s-master ~]$ cat mcwJob1.yml  #设置Job成功完成pod总数为6,每次运行两个pod
apiVersion: batch/v1
kind: Job
metadata:
  name: mcw-job
spec:
  completions: 6
  parallelism: 2
  template:
    metadata:
      name: mcwJob
    spec:
      containers:
        - name: hello
          image: busybox
          command: ["echo","hello k8s job!"]
      restartPolicy: OnFailure
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwJob1.yml 
job.batch/mcw-job created
[machangwei@mcwk8s-master ~]$ kubectl get job
NAME      COMPLETIONS   DURATION   AGE
mcw-job   0/6           5s         5s
[machangwei@mcwk8s-master ~]$ kubectl get pod  
NAME            READY   STATUS              RESTARTS   AGE
mcw-job-7zxf7   0/1     ContainerCreating   0          16s
mcw-job-dqhhw   0/1     ContainerCreating   0          16s
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME            READY   STATUS              RESTARTS   AGE
mcw-job-4xtqm   0/1     ContainerCreating   0          5s
mcw-job-7zxf7   0/1     Completed           0          22s
mcw-job-dqhhw   0/1     Completed           0          22s
mcw-job-x8crl   0/1     ContainerCreating   0          4s
[machangwei@mcwk8s-master ~]$ kubectl get job  #由job和pod每次都是两个两个创建和完成,AGE两个两个的一致
NAME      COMPLETIONS   DURATION   AGE         #可知,每次运行两个pod。总共需要成功完成pod总数为6
mcw-job   2/6           29s        29s
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME            READY   STATUS              RESTARTS   AGE
mcw-job-4xtqm   0/1     Completed           0          20s
mcw-job-7zxf7   0/1     Completed           0          37s
mcw-job-c9qq2   0/1     ContainerCreating   0          3s
mcw-job-dqhhw   0/1     Completed           0          37s
mcw-job-jj5xj   0/1     ContainerCreating   0          2s
mcw-job-x8crl   0/1     Completed           0          19s
[machangwei@mcwk8s-master ~]$ kubectl get job
NAME      COMPLETIONS   DURATION   AGE
mcw-job   4/6           43s        43s
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get job
NAME      COMPLETIONS   DURATION   AGE
mcw-job   6/6           53s        55s
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide  #job的运行,是分配到节点上去了的
NAME            READY   STATUS      RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcw-job-4xtqm   0/1     Completed   0          46s   10.244.0.29   mcwk8s-node2   <none>           <none>
mcw-job-7zxf7   0/1     Completed   0          63s   10.244.0.28   mcwk8s-node2   <none>           <none>
mcw-job-c9qq2   0/1     Completed   0          29s   10.244.0.30   mcwk8s-node2   <none>           <none>
mcw-job-dqhhw   0/1     Completed   0          63s   10.244.0.26   mcwk8s-node1   <none>           <none>
mcw-job-jj5xj   0/1     Completed   0          28s   10.244.0.28   mcwk8s-node1   <none>           <none>
mcw-job-x8crl   0/1     Completed   0          45s   10.244.0.27   mcwk8s-node1   <none>           <none>

[machangwei@mcwk8s-master ~]$ kubectl get job  #删除job也可以这样删除
NAME      COMPLETIONS   DURATION   AGE
mcw-job   6/6           53s        43m
[machangwei@mcwk8s-master ~]$ kubectl delete job mcw-job
job.batch "mcw-job" deleted
[machangwei@mcwk8s-master ~]$ kubectl get job
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl get job
No resources found in default namespace.

定时Job (CronJob)

1、错误的定时任务(配置apiserver出错以及恢复)

[machangwei@mcwk8s-master ~]$ cat mcwCron.yml 
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
  name: mcw-dingshi-hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox
              command: ["echo","hello k8s job!"]
          restartPolicy: OnFailure
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwCron.yml 
error: unable to recognize "mcwCron.yml": no matches for kind "CronJob" in version "batch/v2alpha1"
[machangwei@mcwk8s-master ~]$ ls /etc/kubernetes/
admin.conf  controller-manager.conf  kubelet.conf  manifests  pki  scheduler.conf
[machangwei@mcwk8s-master ~]$ ls /etc/kubernetes/pki/
apiserver.crt              apiserver-etcd-client.key  apiserver-kubelet-client.crt  ca.crt  etcd                front-proxy-ca.key      front-proxy-client.key  sa.pub
apiserver-etcd-client.crt  apiserver.key              apiserver-kubelet-client.key  ca.key  front-proxy-ca.crt  front-proxy-client.crt  sa.key
[machangwei@mcwk8s-master ~]$ ls /etc/kubernetes/manifests/
etcd.yaml  kube-apiserver.yaml  kube-controller-manager.yaml  kube-scheduler.yaml
[machangwei@mcwk8s-master ~]$ ls /etc/kubernetes/manifests/kube-apiserver.yaml 
/etc/kubernetes/manifests/kube-apiserver.yaml
[machangwei@mcwk8s-master ~]$ vim /etc/kubernetes/manifests/kube-apiserver.yaml
[machangwei@mcwk8s-master ~]$ ll /etc/kubernetes/manifests/kube-apiserver.yaml
-rw-------. 1 root root 3347 Jan 12 23:14 /etc/kubernetes/manifests/kube-apiserver.yaml


在root用户下,进入/etc/kubernetes/manifests/kube-apiserver.yaml
在它下面添加- --runtime-config=batch/v2alpha1=true
- kube-apiserver

[root@mcwk8s-master ~]$ systemctl restart kubelet.service #重启,k8s_kube-apiserver_kube-apiserver起不来
[root@mcwk8s-master ~]$ docker logs 8126c
I0118 16:15:48.702168       1 server.go:565] external host was not specified, using 10.0.0.4
I0118 16:15:48.703312       1 server.go:172] Version: v1.23.1
E0118 16:15:49.581300       1 run.go:120] "command failed" err="group version batch/v2alpha1 that has not been registered"


普通用户下,也获取不到pod等信息
[machangwei@mcwk8s-master ~]$ kubectl get pod --all-namespaces
The connection to the server 10.0.0.4:6443 was refused - did you specify the right host or port?

删除添加的配置后,apiserver起来了。重启kukelet,这下面四个都会重启,但是短时间内第二次重启kubectl,有两个不是再次重启的
[root@mcwk8s-master ~]$ docker ps |grep minute
8cbb9bf1332b   b6d7abedde39                                        "kube-apiserver --ad…"   4 minutes ago    Up 4 minutes              k8s_kube-apiserver_kube-apiserver-mcwk8s-master_kube-system_429285c0d2c92c23be02783c184635f6_2
4cffcd921d7a   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                 4 minutes ago    Up 4 minutes              k8s_POD_kube-apiserver-mcwk8s-master_kube-system_429285c0d2c92c23be02783c184635f6_1
dba80e8b1fce   f51846a4fd28                                        "kube-controller-man…"   13 minutes ago   Up 13 minutes             k8s_kube-controller-manager_kube-controller-manager-mcwk8s-master_kube-system_d9f17dcbf3b105928155bc45fff440a9_8
1e6ff9100c86   71d575efe628                                        "kube-scheduler --au…"   13 minutes ago   Up 13 minutes             k8s_kube-scheduler_kube-scheduler-mcwk8s-master_kube-system_119aedc9dc885d479086cea38d1a8061_9
[root@mcwk8s-master ~]$ 

[root@mcwk8s-master ~]$ docker ps |grep minute|awk '{print $NF}' #重启的四个服务
k8s_kube-apiserver_kube-apiserver-mcwk8s-master_kube-system_429285c0d2c92c23be02783c184635f6_2
k8s_POD_kube-apiserver-mcwk8s-master_kube-system_429285c0d2c92c23be02783c184635f6_1
k8s_kube-controller-manager_kube-controller-manager-mcwk8s-master_kube-system_d9f17dcbf3b105928155bc45fff440a9_8
k8s_kube-scheduler_kube-scheduler-mcwk8s-master_kube-system_119aedc9dc885d479086cea38d1a8061_9
[root@mcwk8s-master ~]$ 

再次看,已经好了
[machangwei@mcwk8s-master ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                                    READY   STATUS             RESTARTS          AGE
kube-system   coredns-6d8c4cb4d-cnj2t                 1/1     Running            0                 6d1h

2、正确的写法:

查看api版本
[machangwei@mcwk8s-master ~]$ kubectl api-versions  #这里要使用batch/v1beta1作为定时任务的版本,就可以成功了,v1我不知道行不行,没试过,有时间验证下
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta2
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

查看定时任务yml文件
[machangwei@mcwk8s-master ~]$ cat mcwCron.yml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mcw-dingshi-hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox
              command: ["echo","hello k8s job!"]
          restartPolicy: OnFailure
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwCron.yml #成功创建定时任务
Warning: batch/v1beta1 CronJob is deprecated in v1.21+, unavailable in v1.25+; use batch/v1 CronJob
cronjob.batch/mcw-dingshi-hello created
[machangwei@mcwk8s-master ~]$ kubectl get cronjob  #查看定时任务
NAME                SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mcw-dingshi-hello   */1 * * * *   False     0        27s             36s
[machangwei@mcwk8s-master ~]$ kubectl get pod #查看pod,已经有一个已经完成了
NAME                               READY   STATUS      RESTARTS   AGE
mcw-dingshi-hello-27375392-l5j7n   0/1     Completed   0          46s
[machangwei@mcwk8s-master ~]$ kubectl get jobs #查看jobs,已经完成了一个,时间到第二分钟(这里周期是1分钟)
NAME                         COMPLETIONS   DURATION   AGE  #就开始创建第二个任务
mcw-dingshi-hello-27375392   1/1           17s        64s
mcw-dingshi-hello-27375393   0/1           4s         4s
[machangwei@mcwk8s-master ~]$ kubectl get pod #第二个pod开始创建
NAME                               READY   STATUS              RESTARTS   AGE
mcw-dingshi-hello-27375392-l5j7n   0/1     Completed           0          70s
mcw-dingshi-hello-27375393-l99l2   0/1     ContainerCreating   0          10s
[machangwei@mcwk8s-master ~]$ kubectl get pod  #第二个完成
NAME                               READY   STATUS      RESTARTS   AGE
mcw-dingshi-hello-27375392-l5j7n   0/1     Completed   0          98s
mcw-dingshi-hello-27375393-l99l2   0/1     Completed   0          38s
[machangwei@mcwk8s-master ~]$ kubectl get jobs #每隔一分钟启动一个job
NAME                         COMPLETIONS   DURATION   AGE
mcw-dingshi-hello-27375392   1/1           17s        2m
mcw-dingshi-hello-27375393   1/1           17s        60s
mcw-dingshi-hello-27375394   0/1           0s         0s
[machangwei@mcwk8s-master ~]$ kubectl get pod 
NAME                               READY   STATUS      RESTARTS   AGE
mcw-dingshi-hello-27375392-l5j7n   0/1     Completed   0          2m52s
mcw-dingshi-hello-27375393-l99l2   0/1     Completed   0          112s
mcw-dingshi-hello-27375394-ddm8s   0/1     Completed   0          52s
[machangwei@mcwk8s-master ~]$ kubectl logs mcw-dingshi-hello-27375394-ddm8s  #主节点上查看其中一个pod的打印信息
hello k8s job!

也可以找到mcw-dingshi-hello-27375393-l99l2第一个pod,到它所在的节点查看容器打印,
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                               READY   STATUS      RESTARTS   AGE     IP            NODE           NOMINATED NODE   READINESS GATES
mcw-dingshi-hello-27375393-l99l2   0/1     Completed   0          2m30s   10.244.0.31   mcwk8s-node2   <none>           <none>
mcw-dingshi-hello-27375394-ddm8s   0/1     Completed   0          90s     10.244.0.30   mcwk8s-node1   <none>           <none>
mcw-dingshi-hello-27375395-wxt48   0/1     Completed   0          30s     10.244.0.32   mcwk8s-node2   <none>           <none>
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 

打印结果如下,相同
[root@mcwk8s-node2 ~]$ docker ps -a|grep 27375393
4a46ea160748   busybox                                             "echo 'hello k8s job…"   2 minutes ago        Exited (0) 2 minutes ago              k8s_hello_mcw-dingshi-hello-27375393-l99l2_default_21d0c1ea-47cd-4394-89ae-cbc8e2f94bfd_0
848a1b050f0d   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                 3 minutes ago        Exited (0) 2 minutes ago              k8s_POD_mcw-dingshi-hello-27375393-l99l2_default_21d0c1ea-47cd-4394-89ae-cbc8e2f94bfd_0
[root@mcwk8s-node2 ~]$ docker logs 4a46ea
hello k8s job!
[root@mcwk8s-node
[root@mcwk8s-node2 ~]$ docker ps -a|grep 27375393 #再过一段时间,这个退出的容器已经被删除了。
[root@mcwk8s-node2 ~]$ 

3、定时任务解释:

apiVersion: batch/v1beta1 #当前CronJob的apiVersion
kind: CronJob #当前资源类型
metadata:
  name: mcw-dingshi-hello
spec:
  schedule: "*/1 * * * *" #指定什么时候运行Job。与Linux的定时任务一样的配置
  jobTemplate: #定义Job的模板,格式与前面的Job一致
    spec:
      template:
        spec:
          containers:
            - name: hello
              image: busybox
              command: ["echo","hello k8s job!"]
          restartPolicy: OnFailure

4、系统apiserver用的kube-apiserver.yaml

[root@mcwk8s-master ~]$ cat /etc/kubernetes/manifests/kube-apiserver.yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubeadm.kubernetes.io/kube-apiserver.advertise-address.endpoint: 10.0.0.4:6443
  creationTimestamp: null
  labels:
    component: kube-apiserver
    tier: control-plane
  name: kube-apiserver
  namespace: kube-system
spec:
  containers:
  - command:
    - kube-apiserver
    - --advertise-address=10.0.0.4
    - --allow-privileged=true
    - --authorization-mode=Node,RBAC
    - --client-ca-file=/etc/kubernetes/pki/ca.crt
    - --enable-admission-plugins=NodeRestriction
    - --enable-bootstrap-token-auth=true
    - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
    - --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
    - --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
    - --etcd-servers=https://127.0.0.1:2379
    - --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt
    - --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key
    - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
    - --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt
    - --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key
    - --requestheader-allowed-names=front-proxy-client
    - --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt
    - --requestheader-extra-headers-prefix=X-Remote-Extra-
    - --requestheader-group-headers=X-Remote-Group
    - --requestheader-username-headers=X-Remote-User
    - --secure-port=6443
    - --service-account-issuer=https://kubernetes.default.svc.cluster.local
    - --service-account-key-file=/etc/kubernetes/pki/sa.pub
    - --service-account-signing-key-file=/etc/kubernetes/pki/sa.key
    - --service-cluster-ip-range=10.96.0.0/12
    - --tls-cert-file=/etc/kubernetes/pki/apiserver.crt
    - --tls-private-key-file=/etc/kubernetes/pki/apiserver.key
    image: registry.aliyuncs.com/google_containers/kube-apiserver:v1.23.1
    imagePullPolicy: IfNotPresent
    livenessProbe:
      failureThreshold: 8
      httpGet:
        host: 10.0.0.4
        path: /livez
        port: 6443
        scheme: HTTPS
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    name: kube-apiserver
    readinessProbe:
      failureThreshold: 3
      httpGet:
        host: 10.0.0.4
        path: /readyz
        port: 6443
        scheme: HTTPS
      periodSeconds: 1
      timeoutSeconds: 15
    resources:
      requests:
        cpu: 250m
    startupProbe:
      failureThreshold: 24
      httpGet:
        host: 10.0.0.4
        path: /livez
        port: 6443
        scheme: HTTPS
      initialDelaySeconds: 10
      periodSeconds: 10
      timeoutSeconds: 15
    volumeMounts:
    - mountPath: /etc/ssl/certs
      name: ca-certs
      readOnly: true
    - mountPath: /etc/pki
      name: etc-pki
      readOnly: true
    - mountPath: /etc/kubernetes/pki
      name: k8s-certs
      readOnly: true
  hostNetwork: true
  priorityClassName: system-node-critical
  securityContext:
    seccompProfile:
      type: RuntimeDefault
  volumes:
  - hostPath:
      path: /etc/ssl/certs
      type: DirectoryOrCreate
    name: ca-certs
  - hostPath:
      path: /etc/pki
      type: DirectoryOrCreate
    name: etc-pki
  - hostPath:
      path: /etc/kubernetes/pki
      type: DirectoryOrCreate
    name: k8s-certs
status: {}

5、删除定时任务工作

[machangwei@mcwk8s-master ~]$ kubectl get job
NAME                         COMPLETIONS   DURATION   AGE
mcw-dingshi-hello-27376679   1/1           107s       2m30s
mcw-dingshi-hello-27376680   1/1           47s        90s
mcw-dingshi-hello-27376681   1/1           22s        30s
[machangwei@mcwk8s-master ~]$ kubectl get Cronjob
NAME                SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mcw-dingshi-hello   */1 * * * *   False     0        40s             21h
[machangwei@mcwk8s-master ~]$ kubectl delete Cronjob mcw-dingshi-hello
cronjob.batch "mcw-dingshi-hello" deleted
[machangwei@mcwk8s-master ~]$ kubectl get job
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                               READY   STATUS        RESTARTS   AGE
mcw-dingshi-hello-27376682-fq7l7   0/1     Terminating   0          11s
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                               READY   STATUS        RESTARTS   AGE
mcw-dingshi-hello-27376682-fq7l7   0/1     Terminating   0          18s
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get pod
No resources found in default namespace.

 

posted @ 2022-01-19 01:08  马昌伟  阅读(654)  评论(0编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/