我有一个在 Kubernetes 集群的 Docker 容器中运行的 webapp。该应用程序有一个我想定期调用的端点。应用程序在多个节点/Pod 上运行,重要的是只有一个节点执行端点发起的任务。我查看了 Kubernetes Cron Jobs,但没有找到任何关于从 Kubernetes Cron Job 调用端点的文档。有人对解决这个问题有任何建议吗?在只有一个节点执行任务至关重要的集群中,您如何处理调度?
问问题
15972 次
1 回答
38
CronJob
s 是个不错的选择。这是一个运行 3 个 nginx pod 并接受所有流量的快速布局。每分钟,Job
卷曲 3 个豆荚中的 1 个(总是同一个豆荚)。
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
于 2017-11-11T16:32:22.023 回答