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 * * * *'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…