<p>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?</p>
<pre><code>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 &gt; /task-dir/index.html
        volumeMounts:
        - name: index-html
          mountPath: &quot;/task-dir&quot;
      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

</code></pre>
<p>I will very much appreciate any help in digging into taht</p>
<p>I'm working on solution so may i ask you if it will work :</p>
<pre><code>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 * * * *'
</code></pre>
<p>i edited a bit but not sure why its not working :( begging for help</p>

Kwaker
<p>first of all you you are using a <code>ClusterIP</code> service type as a service. so you can't provide <code>NodePort</code> here.</p>
<p>then when you are trying to access a pod with service you need to call with a dns name that maintain a template.</p>
<p>The template is like <code>my_Service_Name.my_Namespace.svc.cluster-domain.example</code> , but you can skip the <code>cluster-domain.example</code> part. Only <code>Service_Name.Namespace.svc</code> will work fine.</p>
<p>so here you need to <code>curl</code> <code>http://nginx-server-service.default.svc/customdir/index.html</code>  this address.</p>
<p>here i have given the whole yaml that worked for me;</p>
<pre><code>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 &gt; /task-dir/index.html
        volumeMounts:
        - name: index-html
          mountPath: &quot;/task-dir&quot;
      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

---
</code></pre>
<p>here is the updated cronjob</p>
<pre><code>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 * * * *'
</code></pre>
