Skip to main content

Pregunta 13 - Múltiples Containers y Volumen Compartido

Question 13 | Multi Containers and Pod shared Volume

Use context: kubectl config use-context k8s-c1-H

Create a Pod named multi-container-playground in Namespace default with three containers, named c1, c2 and c3. There should be a volume attached to that Pod and mounted into every container, but the volume shouldn't be persisted or shared with other Pods.

Container c1 should be of image nginx:1.17.6-alpine and have the name of the node where its Pod is running available as environment variable MY_NODE_NAME.

Container c2 should be of image busybox:1.31.1 and write the output of the date command every second in the shared volume into file date.log. You can use while true; do date >> /your/vol/path/date.log; sleep 1; done for this.

Container c3 should be of image busybox:1.31.1 and constantly send the content of file date.log from the shared volume to stdout. You can use tail -f /your/vol/path/date.log for this.

Check the logs of container c3 to confirm correct setup.


Vamos a crear un yaml a partir de una plantilla y configurarlo. Es bueno estar familiarizado con emptydir y environments de nodos pods. Primero se crean los pods, después se levantan los containers. Algunas variables pueden ser referenciadas del propio container.

k run multi-container-playground --image=nginx:1.17.6-alpine --dry-run=client -o yaml > opt/course/13/multi-container.yaml

vim opt/course/13/multi-container.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: multi-container-playground
name: multi-container-playground
spec:
containers:
- image: nginx:1.17.6-alpine
name: c1 # cambiar
resources: {}
env: # agregar
- name: MY_NODE_NAME # agregar
valueFrom: # agregar
fieldRef: # agregar
fieldPath: spec.nodeName # agregar
volumeMounts: # agregar
- name: vol # agregar
mountPath: /vol # agregar
- image: busybox:1.31.1 # agregar
name: c2 # agregar
command: ["sh", "-c", "while true; do date >> /vol/date.log; sleep 1; done"] # agregar
volumeMounts: # agregar
- name: vol # agregar
mountPath: /vol # agregar
- image: busybox:1.31.1 # agregar
name: c3 # agregar
command: ["sh", "-c", "tail -f /vol/date.log"] # agregar
volumeMounts: # agregar
- name: vol # agregar
mountPath: /vol # agregar
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes: # agregar
- name: vol # agregar
emptyDir: {} # agregar

Vamos a aplicar

kubectl apply -f opt/course/13/multi-container.yaml
# verificando
k get pod multi-container-playground
NAME READY STATUS RESTARTS AGE
multi-container-playground 3/3 Running 0 95s

# Vamos a verificar dentro de los pods algunas cosas
# Si la variable de entorno está correcta en el container1
k exec multi-container-playground -c c1 -- env | grep MY_NODE_NAME
MY_NODE_NAME=cluster1-node2

# El container3 c3 está ejecutando el comando tail que envía a la salida en consola, vamos a verificar el log
k logs multi-container-playground -c c3