Why doesn't my GKE cronjob exit after running `echo`?

5/9/2019

I'm trying to run a thor task from my Rails app's server image. The cronjob runs one time, but it never exits. I've tested with the example "hello world" job and that seems to work:

# hello-world-cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo "Hello, World!"
          restartPolicy: OnFailure

$ kubectl get pods

NAME                                                           READY     STATUS      RESTARTS   AGE
hello-1557392100-2hnjc                                         0/1   

    Completed   0          3m
    hello-1557392160-58mwb                                         0/1       Completed   0          2m
    hello-1557392220-qbstx                                         0/1      
    send-reminders-events-starting-in-two-hours-1557391560-2qwtv   1/2       Running     0          11m
    send-reminders-events-starting-in-two-hours-1557391740-9dm6q   1/2       Running     0          8m
    send-reminders-events-starting-in-two-hours-1557391800-2tjdt   1/2       Running     0          8m
    send-reminders-events-starting-in-two-hours-1557391860-q6qgb   1/2       Running     0          7m
    send-reminders-events-starting-in-two-hours-1557391920-j9kdn   1/2       Running     0          6m
    send-reminders-events-starting-in-two-hours-1557391980-sqg28   1/2       Running     0          5m
    send-reminders-events-starting-in-two-hours-1557392040-twr4t   1/2       Running     0          4m
    send-reminders-events-starting-in-two-hours-1557392100-skzbz   1/2       Running     0          3m
    send-reminders-events-starting-in-two-hours-1557392160-2qgxl   1/2       Running     0          2m
    send-reminders-events-starting-in-two-hours-1557392220-z7tk4   1/2       Running     0          1m
    send-reminders-users-that-has-not-replied-1557391560-tmlnb     1/2       Running     0          11m

This is my cronjob.yaml. I only see one echo, then it hangs forever. Any idea why?

# cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: send-reminders-events-starting-in-two-hours

spec:
  schedule: "*/1 * * * *"    
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
            - name: cloudsql-instance-credentials
              secret:
                secretName: cloudsql-instance-credentials

          containers:
          - name: events-starting-in-two-hours
            image: eu.gcr.io/example/pepper:latest
            args:
            - /bin/sh
            - -c
            - echo "triggering send-reminders-events-starting-in-two-hours ======="

Dockerfile

FROM ruby:2.6.2-slim-stretch

COPY Gemfile* /app/
WORKDIR /app
RUN gem update --system
RUN gem install bundler
RUN bundle install --jobs 20 --retry 5

# Set the timezone to Stockholm
ENV TZ=Europe/Stockholm
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

COPY . /app/

RUN echo echo "RAILS_ENV is \$RAILS_ENV" >> ~/.bashrc

WORKDIR /app

CMD ["/usr/local/bundle/bin/rails", "s", "-b", "0.0.0.0", "-p", "3001"]
-- martins
cron
google-kubernetes-engine

1 Answer

5/24/2019

I fixed the issue by enabling private IP on the CloudSQL instance and connected directly to that IP from my cronjob. That way I could skip the cloudsql-proxy sidecar. Make sure you have enabled VPC-native support for your GKE cluster also.

-- martins
Source: StackOverflow