I need your help in understanding how to create a Kubernetes cronjob that will use curl using service nginx-server-service to get /customdir/index.html is there any way to do that?
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
I will very much appreciate any help in digging into taht
I'm working on solution so may i ask you if it will work :
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 * * * *'
i edited a bit but not sure why its not working :( begging for help
first of all you you are using a ClusterIP
service type as a service. so you can't provide NodePort
here.
then when you are trying to access a pod with service you need to call with a dns name that maintain a template.
The template is like my_Service_Name.my_Namespace.svc.cluster-domain.example
, but you can skip the cluster-domain.example
part. Only Service_Name.Namespace.svc
will work fine.
so here you need to curl
http://nginx-server-service.default.svc/customdir/index.html
this address.
here i have given the whole yaml that worked for me;
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
---
here is the updated 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 * * * *'