Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

nginx - Cronjob for service

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

question from:https://stackoverflow.com/questions/65869883/cronjob-for-service

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 * * * *'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...