Task weight: 4%
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.
译文:
在Namespace default中创建一个名为 multi-container-playground 的Pod,有三个容器,分别名为 c1 、c2 和 c3 。应该有一个卷连接到该Pod并挂载到每个容器中,但该卷不应该被持久化或与其他Pod共享。
容器 c1 应该是 nginx:1.17.6-alpine 镜像,并且其Pod运行的节点名称可以作为环境变量MY_NODE_NAME。
容器 c2 应该是 busybox:1.31.1 镜像,并在共享卷中每秒钟将date命令的输出写入文件 date.log 中。你可以使用 while true; do date >> /your/vol/path/date.log; sleep 1; done 来实现这一点。
容器 c3 应该是 busybox:1.31.1 镜像 ,并不断地从共享卷中发送 date.log 文件的内容到stdout。你可以使用 tail -f /your/vol/path/date.log 来实现。
检查容器 c3 的日志以确认正确的设置。
解答:
kubectl config use-context k8s-c1-H
创建一个pod模板
k run multi-container-playground --image=nginx:1.17.6-alpine $do > 13.yaml
vim 13.yaml
13.yaml
# 13.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
status: {}
创建pod
k -f 13.yaml create
检查验证C1 env , c3 logs
k get pod multi-container-playground
k exec multi-container-playground -c c1 -- env | grep MY
k logs multi-container-playground -c c3