Kubernetes cronjob cannot access database service

9/2/2020

I am trying to run a cron job in kubernetes that needs to access a database. This is the database yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    component: db
  name: db
spec:
  selector:
    matchLabels:
      component: db
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        component: db
    spec:
      containers:
        - name: db
          image: mysql:5.7
          ports:
            - containerPort: 3306
          args:
            - --transaction-isolation=READ-COMMITTED
            - --binlog-format=ROW
            - --max-connections=1000
            - --bind-address=0.0.0.0
          env:
            - name: MYSQL_DATABASE
              valueFrom:
                secretKeyRef:
                  key: MYSQL_DATABASE
                  name: db-secrets
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: MYSQL_PASSWORD
                  name: db-secrets
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: MYSQL_ROOT_PASSWORD
                  name: db-secrets
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  key: MYSQL_USER
                  name: db-secrets
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: db-persistent-storage
      restartPolicy: Always
      volumes:
        - name: db-persistent-storage
          persistentVolumeClaim:
            claimName: db-pvc

And this is the yaml for the cronjob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cron
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: cron
              image: iulbricht/shopware-status-tool:1.0.0
              env:
                - name: USERNAME
                  valueFrom:
                    secretKeyRef:
                      key: USERNAME
                      name: cron-secrets
                - name: PASSWORD
                  valueFrom:
                    secretKeyRef:
                      key: PASSWORD
                      name: cron-secrets
                - name: DATABASE_DSN
                  valueFrom:
                    secretKeyRef:
                      key: DATABASE_DSN
                      name: cron-secrets
                - name: DHL_API_KEY
                  valueFrom:
                    secretKeyRef:
                      key: DHL_API_KEY
                      name: cron-secrets
                - name: SHOP_API
                  valueFrom:
                    secretKeyRef:
                      key: SHOP_API
                      name: cron-secrets
          restartPolicy: OnFailure

When the cronjob runs I always get the following message: default addr for network 'db:3306' unknown. The mysql connection string is as follows: mysql://username:password@db:3306/shopware

I am using Kustomization and the db and cron are in the save namespace.

Can anyone help me find a way to solve this?

-- Knerd
kubernetes

1 Answer

9/2/2020

Can you please try this connection string

username:password@tcp(db:3306)/shopware
-- Roar S.
Source: StackOverflow