Questão 13 - Múltiplos Containers e Volume Compartilhado
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 criar um yaml a partir de um template e configurá-lo. É bom estar familiarizado com emptydir e environments nods pods. Primeiramente os pods são criados depois sobem os containers. Algumas variáveis podem ser referenciadas do próprio 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 # change
resources: {}
env: # add
- name: MY_NODE_NAME # add
valueFrom: # add
fieldRef: # add
fieldPath: spec.nodeName # add
volumeMounts: # add
- name: vol # add
mountPath: /vol # add
- image: busybox:1.31.1 # add
name: c2 # add
command: ["sh", "-c", "while true; do date >> /vol/date.log; sleep 1; done"] # add
volumeMounts: # add
- name: vol # add
mountPath: /vol # add
- image: busybox:1.31.1 # add
name: c3 # add
command: ["sh", "-c", "tail -f /vol/date.log"] # add
volumeMounts: # add
- name: vol # add
mountPath: /vol # add
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes: # add
- name: vol # add
emptyDir: {} # add
Vamos aplicar
kubectl apply -f opt/course/13/multi-container.yaml
# conferindo
k get pod multi-container-playground
NAME READY STATUS RESTARTS AGE
multi-container-playground 3/3 Running 0 95s
# Vamos conferir dentro dos podes algumas coisas
# Se a variavel de ambiente esta correta no container1
k exec multi-container-playground -c c1 -- env | grep MY_NODE_NAME
MY_NODE_NAME=cluster1-node2
# O container3 c3 esta rodando o comando tail que joga pra saída no console, vamos conferir o log
k logs multi-container-playground -c c3