Reputation: 639
I create secret redis-secret with following command.
kubectl create secret generic redis-secret --from-literal=password=0123456
After that I create pod secrets-via-file, using the redis image, which mounts secret name redis-secret at /secrets.
kubectl run secret-via-file --image=redis --dry-run=client -o yaml > pod.yaml
I edited the create pod.yaml file.
apiVersion: v1
kind: Pod
metadata:
labels:
run: secret-via-file
name: secret-via-file
spec:
containers:
- image: redis
name: secret-via-file
volumeMounts:
- name: redis-secret
mountPath: /secrets
volumes:
- name: redis-secret
secret:
secretName: redis-secret
I created second pod name secret-via-env, using the redis image, which exports password as PASSWORD.
kubectl run secret-via-env --image=redis --dry-run=client -o yaml > pod2.yaml
I edited the pod2.yaml file.
apiVersion: v1
kind: Pod
metadata:
labels:
run: secrets-via-env
name: secrets-via-env
spec:
containers:
- image: redis
name: secrets-via-env
env:
- name: PASSWORD
valueFrom:
secretKeyRef:
name: redis-secret
key: password
I connect to the pod secrets-via-env with following command.
kubectl exec -it secret-via-file -- redis-cli
I try to verify if the secret is mounted to the pods. In the second pod I want to use the variable PASSWORD to retrieve the assigned value(0123456). I used the command below but it does not work.
SECRET GET PASSWORD
Upvotes: 0
Views: 1733
Reputation: 17621
tried as below. i see the PASSWORD secret is getting listed as env inside pod
# create secret
kubectl create secret generic redis-secret --from-literal=password=0123456
# create pod
apiVersion: v1
kind: Pod
metadata:
labels:
run: secrets-via-env
name: secrets-via-env
spec:
containers:
- image: redis
name: secrets-via-env
env:
- name: PASSWORD
valueFrom:
secretKeyRef:
name: redis-secret
key: password
# check PASSWORD secret
master $ kubectl exec -it secrets-via-env sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
# echo $PASSWORD
0123456
# from first pod
---
apiVersion: v1
kind: Pod
metadata:
labels:
run: secret-via-file
name: secret-via-file
spec:
containers:
- image: redis
name: secret-via-file
volumeMounts:
- name: redis-secret
mountPath: /secrets
volumes:
- name: redis-secret
secret:
secretName: redis-secret
controlplane $ kubectl exec -it secret-via-file sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
# ls -l /secrets
total 0
lrwxrwxrwx 1 root root 15 Jul 22 09:45 password -> ..data/password
# cat /secrets/password
0123456#
Upvotes: 2