Skip to main content

Pregunta 19 - Crear Secret y Montar en Pod

Question 19 | Create Secret and mount into Pod

NOTE: This task can only be solved if questions 18 or 20 have been successfully implemented and the k8s-c3-CCC cluster has a functioning worker node

Use context: kubectl config use-context k8s-c3-CCC

Do the following in a new Namespace secret. Create a Pod named secret-pod of image busybox:1.31.1 which should keep running for some time.

There is an existing Secret located at /opt/course/19/secret1.yaml, create it in the Namespace secret and mount it readonly into the Pod at /tmp/secret1.

Create a new Secret in Namespace secret called secret2 which should contain user=user1 and pass=1234. These entries should be available inside the Pod's container as environment variables APP_USER and APP_PASS.

Confirm everything is working.


Podemos ver que una pregunta puede depender de la otra. Si el no fuera corregido el cluster que vamos a usar que estaría con problemas para ser resuelto en preguntas anteriores no es posible hacer. Vamos a asumir que está ok.

kubectl config use-context k8s-c3-CCC
k create ns secret

# Fue dicho que existe un template para una secret que debe ser creada en el namespace secret y montada en el pod.
cp /opt/course/19/secret1.yaml new_secret1.yaml

vim new_secret1.yaml

Analizando lo que vamos a aplicar y haciendo los cambios

apiVersion: v1
kind: Secret
metadata:
creationTimestamp: null
name: secret1
namespace: secret # alterar para el namespace pedido
data:
halt: IyEgL2Jpbi9zaAo...
k -f 19_secret1.yaml create

# Ahora vamos a crear la segunda secret que fue pedida
k create secret -n secret generic secret2 --from-literal=user=user1 --from-literal=pass=1234

# Vamos a crear un template para el pod. Este pod necesita estar ejecutándose cuando el evaluador vaya a ver las cosas, entonces vamos a darle unos días
k run secret-pod -n secret --image=busybox:1.31.1 --dry-run=client -o yaml -- sh -c "sleep 10d" > /opt/course/19/pod.yaml

vim /opt/course/19/pod.yaml

Ahora vamos a montar las secrets dentro. La secret1 como volume la secret2 como env.


# /opt/course/19/pod.yaml

apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: secret-pod
name: secret-pod
namespace: secret
spec:
containers:
- args:
- sh
- -c
- sleep 10d
image: busybox:1.31.1
name: secret-pod
resources: {}
env: # agregar
- name: APP_USER # agregar
valueFrom: # agregar
secretKeyRef: # agregar
name: secret2 # agregar
key: user # agregar
- name: APP_PASS # agregar
valueFrom: # agregar
secretKeyRef: # agregar
name: secret2 # agregar
key: pass # agregar
volumeMounts: # agregar
- name: secret1 # agregar
mountPath: /tmp/secret1 # agregar
readOnly: true # agregar
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes: # agregar
- name: secret1 # agregar
secret: # agregar
secretName: secret1 # agregar
status: {}

Y vamos a aplicar y verificar.

k create -f /opt/course/19/pod.yaml

# Sólo para verificar
k exec -n secret secret-pod -- env | grep APP
APP_PASS=1234
APP_USER=user1

➜ k exec -n secret secret-pod -- find /tmp/secret1
/tmp/secret1
/tmp/secret1/..data
/tmp/secret1/halt
/tmp/secret1/..2019_12_08_12_15_39.463036797
/tmp/secret1/..2019_12_08_12_15_39.463036797/halt

➜ k -n secret exec secret-pod -- cat /tmp/secret1/halt

#! /bin/sh
### BEGIN INIT INFO
# Provides: halt
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop: 0
# Short-Description: Execute the halt command.
# Description:
...