0

I have the following file that creates a mysql-secret for mysql deployment pod.

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  mysql-password: MTExMTEx
  mysql-root-password: MTExMTEx
  mysql-user: YQ==

The problem is that, previously I could deploy mysql on Kubernetes cluster using the secret key created by this command:

kubectl create secret generic mysql-secret --from-literal MYSQL_KEY=11111

And using MYSQL_KEY I could pass it to other deployment files like auth as you can see below:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
    spec:
      containers:
        - name: auth
          image: auth
          env:
            - name: MYSQL_URI
              value: 'mysql://auth-mysql-srv:3306/users_auth'
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: MYSQL_KEY

But now I don't have a key using a yaml file to create the secret key and I get the following error because of that:

 - deployment/auth-mysql-depl is ready. [3/5 deployment(s) still pending]
 - deployment/mysql: container mysql in error: &ContainerStateWaiting{Reason:CreateContainerConfigError,Message:couldn't find key MYSQL_KEY in Secret default/mysql-secret,}
    - pod/mysql-78bbf5f6f4-vbpkz: container mysql in error: &ContainerStateWaiting{Reason:CreateContainerConfigError,Message:couldn't find key MYSQL_KEY in Secret default/mysql-secret,}

How can I add a key: MY_SQL property to the secret.yaml file or find a way to eliminate it from the other deployment files like auth that uses it?

If I just eliminate key: MYSQL_KEY from auth deployment file I get this error:

 - The Deployment "auth-depl" is invalid: spec.template.spec.containers[0].env[1].valueFrom.secretKeyRef.key: Required value

EDIT: I tried to create the secret before I run skaffold dev using kubectl apply -f mysql-secret.yaml command and it worked. My question is, how can I say to skafoold please run kubectl apply -f on mysql-secret.yaml file before the other yaml files?

apiVersion: skaffold/v4beta1
kind: Config
build:
  artifacts:
  - image: auth
    context: auth-service
    sync:
      manual:
      - src: src/**/*.ts
        dest: .
    docker:
      dockerfile: Dockerfile
  - image: client
    context: client-service
    sync:
      manual:
      - src: lib/**/*.dart
        dest: .
    docker:
      dockerfile: Dockerfile
  local:
    push: false
manifests:
  rawYaml:
  - ./infra/k8s/*
deploy:
  kubectl: {}
  kubeContext: kind-kind
best_of_man
  • 643
  • 2
  • 15

1 Answers1

2

Looks like the deployment file is correct but your secret does not have the required keys. the keys are case sensitive. In your secret.yaml file I do not see MYSQL_KEY. Edit the file and add the key.

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  mysql-password: MTExMTEx
  mysql-root-password: MTExMTEx
  mysql-user: YQ==
  MYSQL_KEY: MTExMTEx

please note value of MYSQL_KEY in the above snippet is random. it should be base64 string of actual value

then run kubectl apply -f . Off course, Kubectl tool should be installed and should point to the correct cluster.

if this command runs well, your pods will be up in a few minutes.

Another way to edit the secret directly is to run kubectl edit secret secret-name or kubectl patch command. By this way you can edit k8s objects directly.

Guru
  • 1,303
  • 18
  • 32
  • Thank you, it seems it solved my previous problem, but I don't know why do I still get `- deployment/auth-depl: container auth in error: &ContainerStateWaiting{Reason:CreateContainerConfigError,Message:secret "mysql-secret" not found,} - pod/auth-depl-8659c55b76-nbfl7: container auth in error: &ContainerStateWaiting{Reason:CreateContainerConfigError,Message:secret "mysql-secret" not found,}` – best_of_man Jan 16 '23 at 08:24
  • try to remove the pod, and check secret is created properly. if things still not work may be your cluster admin added some security checks (I don't belive though) please consider rating / accepting this answer – Guru Jan 16 '23 at 08:38
  • it clearly says secret not found, are you creating a secret and deployment in the same namespace? check spellings it is case sensitive – Guru Jan 16 '23 at 08:40
  • I do all in the same namespace. Actually I have created a Skaffold.yaml file and it does the job. – best_of_man Jan 16 '23 at 18:16
  • I can create the secret via `kubectl create secret generic mysql-secret --from-literal MYSQL_KEY=11111` command. – best_of_man Jan 16 '23 at 18:17
  • I found the problem. Please read the `EDIT` part I added to my question. – best_of_man Jan 16 '23 at 18:44
  • great you found answer on your own :) – Guru Jan 17 '23 at 05:07