Bhargav Behara
Bhargav Behara

Reputation: 315

Use a secret to store sensitive data in helm kubernetes

I have a secret.yaml file inside the templates directory with the following data:

apiVersion: v1
kind: Secret
metadata: 
  name: appdbpassword
stringData:
  password: password

I also have a ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: appdbconfigmap
data:
  jdbcUrl: jdbc:oracle:thin:@proxy:service
  username: bhargav

I am using the following pod:

apiVersion: v1
kind: Pod
metadata:
  name: expense-pod-sample-1
spec:
  containers:
    - name: expense-container-sample-1
      image: exm:1
      command: [ "/bin/sh", "-c", "--" ]
      args: [ "while true; do sleep 30; done;" ]
      envFrom:
      - configMapRef:
              name: appdbconfigmap
      env:
      - name: password
        valueFrom:
          secretKeyRef:
            name: appdbpassword
            key: password

When I use the helm install command I see the pod running but if I try to use the environment variable ${password} in my application, it just does not work. It says the password is wrong. It does not complain about the username which is a ConfigMap. This happens only if I use helm. If I don't use helm and independently-run all the YAML files using kubectl, my application access both username and password correctly.

Am I missing anything here ?

Upvotes: 1

Views: 1438

Answers (1)

Harsh Manvar
Harsh Manvar

Reputation: 30208

apiVersion: v1
kind: Secret
metadata:
  name: test-secret
data:
  password : cGFzc3dvcmQK

You can also add the secret like this where converting data into base64 format. While stringData do it automatically when you create secret.

Trying Adding the secrets in environment like this way

envFrom:
        - secretRef:
            name: test-secret

Upvotes: 2

Related Questions