Search code examples
kuberneteskubectl

Kubernetes env-file


env.example

MYSQL_PASSWORD=password
MYSQL_DATABASE=db
MYSQL_ROOT_PASSWORD=password
MYSQL_USER=user

I created secret

kubectl create secret generic prod-secrets --from-file=env.example

Specified secret in mysql yaml file

spec:
      containers:
      - name: mysql-container
        image: mysql:dev
        imagePullPolicy: "IfNotPresent"
        envFrom:
          - secretRef:
             name: prod-secrets
        ports:
        - containerPort: 3306
         # container (pod) path
        volumeMounts:
          - name: mysql-persistent-storage
            mountPath: /data/db

Pod goes into CrashLoopBackOff

This is from kubectl describe pod

Environment Variables from:

prod-secrets Secret Optional: false

Environment:

What's wrong with this setup ?

Secret output:

kubectl describe secret prod-secrets
Name:         prod-secrets
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
env.example:  96 bytes

EDIT

After trying suggestion from answer, pod still crashing

mysql-container-ffdc44fc6-w9qqm     0/1     Error     1          23s
mysql-container-ffdc44fc6-w9qqm     0/1     CrashLoopBackOff   1          34s
mysql-container-ffdc44fc6-w9qqm     1/1     Running            2          34s
mysql-container-ffdc44fc6-w9qqm     0/1     Error              2          44s
mysql-container-ffdc44fc6-w9qqm     0/1     CrashLoopBackOff   2          56s
mysql-container-ffdc44fc6-w9qqm     1/1     Running            3          72s
mysql-container-ffdc44fc6-w9qqm     0/1     Error              3          82s

Secret is shown correctly

kubectl describe secret prod-secrets
Name:         prod-secrets
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
MYSQL_DATABASE:       7 bytes
MYSQL_PASSWORD:       8 bytes
MYSQL_ROOT_PASSWORD:  8 bytes
MYSQL_USER:           4 bytes

I'm running only 1-node cluster (1 master node), can this cause this issue ?

Found and issue:

specified root as DB_USER in env file, once i changed it all started working


Solution

  • Create the secret using the option --from-env-file

    kubectl create secret prod-secrets \
           --from-env-file=env.example