从 Kubernetes Cron Job 调用端点

Call endpoint from Kubernetes Cron Job

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

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

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