0

I want to run a curl command inside a Kubernetes pod as a cron job.

Further down, you find my YAML files. The marked lines cause the issues. The script does not reach the end.

Since after deleting it, everything works fine. I tried to replace it with curl stackoverflow.com, and it works. That means that there is a connection to the internet. Moreover omitting -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/JSON" also works. In the sense that the script reaches the end. I also echo'ed the CFKEY and it returns the right value.

I also test this script inside the interactive mode in a docker container docker run -it huyled/curl-Perl /bin/sh, and it also works with the -H curl option.

Why does the pod hang in the marked curl line

# configmap.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: dyndns-updater
  namespace: default
  labels:
    app.kubernetes.io/name: dyndns-updater
    app.kubernetes.io/instance: dyndns-updater
data:
  dyndns-updater.sh: |
    #!/bin/sh
    set -o errexit
    set -o nounset
    set -o pipefail

    CFKEY=$CLOUDKEY 
    CFUSER=
    CFZONE_NAME=
    CFRECORD_NAME=
    CFRECORD_TYPE=A
    CFTTL=120
       
    WANIPSITE="http://ipv4.icanhazip.com"
   
    WAN_IP=`curl -s ${WANIPSITE}`
    echo "$WAN_IP"
    
    echo "Updating zone_identifier & record_identifier"
    # the following line causes problems 
    curl -s -X GET -H "X-Auth-Email: $CFUSER" -H "X-Auth-Key: $CFKEY" -H "Content-Type: application/json" "https://api.cloudflare.com/client/v4/zones?name=$CFZONE_NAME"
   
    echo "end"
# cronjob.yaml
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: default
  name: dyndns-updater
spec:
  schedule: "0 * * * *"
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 3
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          imagePullSecrets:
          - name: regcred
          containers:
          - name: dyndns-updater
            image: huyled/curl-perl 
            imagePullPolicy: IfNotPresent
            envFrom:
            - secretRef:
                name: do-token
            command:
            - "/bin/sh"
            - "-ec"
            - "/app/dyndns-updater.sh"
            volumeMounts:
            - name: dyndns-updater
              mountPath: /app/dyndns-updater.sh
              subPath: dyndns-updater.sh
              readOnly: true
          volumes:
          - name: dyndns-updater
            projected:
              defaultMode: 0775
              sources:
              - configMap:
                  name: dyndns-updater
                  items:
                  - key: dyndns-updater.sh
                    path: dyndns-updater.sh

# secret.yaml
---
apiVersion: v1
kind: Secret
metadata:
  name: do-token
  namespace: default
type: Opaque
data:
  # Your domain name in base64
  CLOUDKEY: <base64 secret>
David Maze
  • 130,717
  • 29
  • 175
  • 215
A.Dumas
  • 2,619
  • 3
  • 28
  • 51
  • If the curl command is returning non zero, since your bash script has `set -o errexit`, the following echo command will not be executed. Can you check the exit status of curl by removing errexit and echoing `$?` ? – whites11 Apr 22 '21 at 09:56

1 Answers1

0

Maybe the curl command is not the real one - it's just one from a busybox with not all features compiled into it. You can test this with

ls -l $(which curl)

I also found How do I run curl command from within a Kubernetes pod

Gerald Hansen
  • 1,010
  • 8
  • 9