Data from volumes as kubernetes secrets

6/14/2019

I have an application that starts with docker-compose up. Some ssh credentials are provided with a json file, in a volume, in the host machine. I want to run the app in kubernetes, how can I provide the credentials using kubernetes secrets? my json file looks like:

{
  "HOST_USERNAME"="myname",
  "HOST_PASSWORD"="mypass",
  "HOST_IP"="myip"
}

I created a file named mysecret.yml with base64 and I applied in kubernetes

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  HOST_USERNAME: c2gaQ=
  HOST_PASSWORD: czMxMDIsdaf0NjcoKik=
  HOST_IP: MTcyLjIeexLjAuMQ==

How I have to write the volumes in deployment.yml in order to use the secret properly?

-- Stavros Droutsas
docker
docker-compose
kubernetes
node.js

1 Answer

6/15/2019
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: redis
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret

This is the above example of using secret as volumes. You can use the same to define a deployment.

Please refer to official kubernetes documentation for further info: https://kubernetes.io/docs/concepts/configuration/secret/

-- Malathi
Source: StackOverflow