来自Kubernetes Cron Job

时间:2017-11-09 13:27:01

标签: cron kubernetes kubernetes-cronjob

我在Kubernetes集群中的Docker容器中运行了一个webapp。该应用程序有一个我想定期调用的端点。该应用程序在多个节点/ pod上运行,重要的是只有一个节点执行端点启动的任务。我看过Kubernetes Cron Jobs,但没有找到任何关于从Kubernetes Cron Job调用端点的文档。有没有人提出解决这个问题的建议?如何在集群中处理调度,只有一个节点执行任务至关重要?

1 个答案:

答案 0 :(得分:7)

CronJob是个不错的选择。这是一个快速布局,运行3个接受所有流量的nginx pod。每分钟,Job卷曲3个豆荚中的一个(总是相同的豆荚)。

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: main
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

---

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: singleton
  labels:
    app: nginx
    special: singleton
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      special: singleton
  template:
    metadata:
      labels:
        app: nginx
        special: singleton
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

---

kind: Service
apiVersion: v1
metadata:
  name: allpods
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

---

kind: Service
apiVersion: v1
metadata:
  name: singleton
spec:
  selector:
    special: singleton
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: callout
spec:
  schedule: "*/1 * * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: callout
            image: buildpack-deps:curl
            args:
            - /bin/sh
            - -ec
            - curl http://singleton
          restartPolicy: Never