best_of_man
best_of_man

Reputation: 728

couldn't find key MYSQL_KEY in Secret default/mysql-secret

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

Upvotes: 1

Views: 4528

Answers (2)

Ramya Musunuru
Ramya Musunuru

Reputation: 471

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

In this MYSQL_KEY is missing under data.

The mapping goes like this

  env:
    - name: MYSQL_ROOT_PASSWORD //The name that you used in ur properties
      valueFrom:
        secretKeyRef:
          name: mysql-secret    //The name that you used under  metadata: name: in your secret file
          key: MYSQL_ROOT_PASSWORD //The key of key-value pair under data: in your secretfile, 

Upvotes: 0

Guru
Guru

Reputation: 1410

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.

Upvotes: 2

Related Questions