我需要你的帮助来理解如何创建一个Kubernetes cronjob,它将使用curl使用服务nginx-server-service来获取/customdir/index.html有什么办法吗?
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-server
labels:
tier: application
spec:
replicas: 1
selector:
matchLabels:
app: nginx-server
template:
metadata:
labels:
app: nginx-server
spec:
volumes:
- name: index-html
emptyDir: {}
initContainers:
- name: setup
image: alpine:3.8
command:
- /bin/sh
- -c
- echo kubernetes works > /task-dir/index.html
volumeMounts:
- name: index-html
mountPath: "/task-dir"
containers:
- name: nginx
image: nginx:mainline
ports:
- containerPort: 80
volumeMounts:
- name: index-html
mountPath: /usr/share/nginx/html/customdir
---
# next, a service is required to handle traffic to the pods created by the deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-server-service
labels:
tier: networking
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30001
selector:
app: nginx-server
type: ClusterIP
我将非常感谢任何帮助,深入研究
我正在研究解决方案,所以我可以问你它是否有效:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: curljob
spec:
jobTemplate:
metadata:
name: curljob
spec:
template:
metadata:
spec:
containers:
- command:
- curl
- http://myapp-service:30001/customdir
image: curlimages/curl
imagePullPolicy: Always
name: curljobt
restartPolicy: OnFailure
schedule: '*/1 * * * *'
我编辑了一点,但不确定为什么不工作:(乞求帮助
首先,您使用ClusterIP
服务类型作为服务。所以你不能在这里提供NodePort
那么当你试图访问一个有服务的pod时,你需要调用一个维护模板的DNS名称。
模板与my_Service_Name.my_Namespace.svc.cluster-domain.example
类似,但可以跳过cluster-domain.example
部分。只有Service_Name.Namespace.svc
可以正常工作。
所以这里你需要curl
http://nginx-server-service.default.svc/customdir/index.html
这个地址。
在这里,我给出了为我工作的整个yaml;
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-server
labels:
tier: application
spec:
replicas: 1
selector:
matchLabels:
app: nginx-server
template:
metadata:
labels:
app: nginx-server
spec:
volumes:
- name: index-html
emptyDir: {}
initContainers:
- name: setup
image: alpine:3.8
command:
- /bin/sh
- -c
- echo kubernetes works > /task-dir/index.html
volumeMounts:
- name: index-html
mountPath: "/task-dir"
containers:
- name: nginx
image: nginx:mainline
ports:
- containerPort: 80
volumeMounts:
- name: index-html
mountPath: /usr/share/nginx/html/customdir
---
# next, a service is required to handle traffic to the pods created by the deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-server-service
labels:
tier: networking
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-server
type: ClusterIP
---
下面是更新后的cronjob
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: curljob
spec:
jobTemplate:
metadata:
name: curljob
spec:
template:
metadata:
spec:
containers:
- command:
- curl
- http://nginx-server-service.default.svc/customdir/index.html
image: curlimages/curl
imagePullPolicy: Always
name: curljobt
restartPolicy: OnFailure
schedule: '*/1 * * * *'