Post

Kubernetes - Secrets

Lab assignment for creating secrets using kubectl

Kubernetes - Secrets

Prerequisites

  • Kubernetes
  • kubectl

Assignment

  • Lets start by creating a secret to pass the environmental variables to the pod.

Job

1️⃣ Create a secret yaml using the –from-literal parameter. Compare the base64 encoded value in the secret definition to the value generated using base64.

1
kubectl create secret generic colour-secret --from-literal=COLOUR=red --from-literal=KEY=value --dry-run=client -o yaml
1
echo -n value | base64
1
echo dmFsdWU= | base64 -d

2️⃣ Create a secret and check the yaml definition.

1
kubectl create secret generic colour-secret --from-literal=COLOUR=red --from-literal=KEY=value
1
kubectl get secrets
1
kubectl get secret/colour-secret -o yaml

3️⃣ Create a pod that dumps environmental variables and sleeps for infinity. Review the environmental variables dumped by the pod.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cat <<EOF > env-dump-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: ubuntu
  name: ubuntu
spec:
  containers:
  - command:
    - bash
    - -c
    - env; sleep infinity
    image: ubuntu
    name: ubuntu
    resources: {}
    envFrom:
    - secretRef:
        name: colour-secret
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}
EOF
1
kubectl apply -f env-dump-pod.yaml
1
kubectl logs ubuntu

4️⃣ Delete the pod and secrets.

1
kubectl delete pod/ubuntu secret/colour-secret --now
1
rm env-dump-pod.yaml
This post is licensed under CC BY 4.0 by the author.