最简单的方法是卷曲您的/load
REST 端点。这是一种方法:
我用来替代您的应用程序的 Pod 定义(用于测试目的):
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: mendhak/http-https-echo
我使用此图像是因为它将各种 HTTP 请求属性发送回客户端。
为 pod 创建一个服务:
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp the selector
ports:
- protocol: TCP
port: 80 #Port that service is available on
targetPort: 80 #Port that app listens on
创建一个 CronJob:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: curljob
spec:
jobTemplate:
metadata:
name: curljob
spec:
template:
metadata:
spec:
containers:
- command:
- curl
- http://myapp-service:80/load
image: curlimages/curl
imagePullPolicy: Always
name: curljobt
restartPolicy: OnFailure
schedule: '*/1 * * * *'
或者,您可以使用命令来启动它:
kubectl create cronjob --image curlimages/curl curljob -oyaml --schedule "*/1 * * * *" -- curl http://myapp-service:80/load
何时"*/1 * * * *"
将指定CronJob
运行的频率。我已将其设置为每分钟运行一次。您可以在此处和此处查看有关如何设置 cron 作业的更多信息
这是来自作业 pod 之一的 kubectl 日志的结果:
{
"path": "/load",
"headers": {
"host": "myapp-service",
"user-agent": "curl/7.68.0-DEV",
"accept": "*/*"
},
"method": "GET",
"body": "",
"fresh": false,
"hostname": "myapp-service",
"ip": "::ffff:192.168.197.19",
"ips": [],
"protocol": "http",
"query": {},
"subdomains": [],
"xhr": false,
"os": {
"hostname": "myapp-pod"
如您所见,应用程序接收GET
到带有 path: 的请求 /load
。
让我知道这是否有帮助。