「Kubernetes」- 管理具体的工作负载

  CREATED BY JENKINSBOT

7.1. Running a Batch Job

运行批处理,比如要进行某个非常耗时的任务。

此时可以创建JOb对象:

kind: Job
apiVersion: batch/v1
metadata:
  name: counter
spec:
  template:
    metadata:
      name: counter
    spec:
      containers:
      - name: counter
        image: busybox
        command:
          - "sh"
          - "-c"
          - "for i in 1 2 3; do echo $i ; done"
      restartPolicy: Never

查看Job日志:kubectl logs jobs/counter

删除Job对象:kubectl delete jobs/counter

7.2. Running a Task on a Schedule Within a Pod

可以使用k8s的CronJob对象,来运行定时任务。CronJob集成了更加通用的Job对象。

定时CronJob时额外增加一个schedule一节,其采用的是crontab格式:

kind: CronJob
apiVersion: batch/v2alpha1
metadata:
  name: hourly-date
spec:
  schedule: "0 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          container:
          - name: date
            image: busybox
            command:
            - "sh"
            - "-c"
            - "date"
          restartPolicy: OnFailure

参考CronJob手册:「CronJob

7.3. Running Infrastructure Daemons per Node

启动日志收集器或者监控代理等基础设施服务,并确保每个节点上运行一个POD:使用DaemonSet服务。

例如每个节点上运行一个Fluentd服务,并按照以下的例子编写一个fluentddaemonset.yaml文件:

kind: DaemonSet
apiVersion: extensions/v1beat1
metadata:
  name: fluentd
spec:
  template:
    metadata:
      name: fluentd
      labels:
        app: fluented
    spec:
      containers:
        - name: fluentd
          image: gci.io/google_conntainers/fluentd-elasticsearch:1.3
          env:
            - name: FLUENTD_ARGS
              value: -qq
          columeMounts:
            - name: varlog
              mountPath: /var/log
            - name: containers
              mountPath: /var/lib/docker/containers
       volumes:
         - hostPath:
             path: /var/log
           name: varlog
         - hostPath:
             path: /var/lib/docker/containers
           name: containers

kubectl craete -f fluentddaemonset.yaml

kubectl get ds

kubectl describe ds/fluentd

7.4. Managing Stateful and Leader/Follower Apps

StatefulSet是一个控制器,为监控的POD提供唯一的身份。

出于安全考虑,删除POD不会删除对应的卷。

可以参考「cockroachdb-statefulset.yaml」的示例。

7.5. Influencing Pods’ Startup Behavior

影响Pod的启动行为,在某个POD启动后再启动另外换一个POD:

向往常一样创建一个普通的POD,然后以如下方式定义一个依赖的POD:

kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: nginx
spec:
  relicas: 1
  template:
   metadata:
     lable:
       app: nginx
   spec:
     containers:
     - name: webserver
       image: nginx
       ports:
       - containerPort: 80
     initContainers:
     - name: checkabackend
       image: busybox
       command: ["sh", "-c", "until nslookup..........."]

有关内容参考官方「Init Containers」手册。