模拟题目:

设置配置环境:

[candidate@node-1] $ kubectl config use-context k8s

Task

  • default 命名空间创建一个 deployment 名为 deploymenb-web
  • 包含一个主容器 lfccncf/busybox:1 ,名称 logger-123
  • 包含一个边车容器 lfccncf/fluentd:v0.12 ,名称 adaptor-dev
  • 在两个容器上挂载一个共享卷 /ckad/log ,当 pod 删除,这个卷不会持久
  • logger-123 容器运行以下命令:
    while true; do
    echo "i luv cncf" >> /ckad/log/input.log; sleep 10;
    done

    结果会文本输出到 /ckad/log/input.log ,格式示例如下: i luv cncf i luv cncf i luv cncf

  • adaptor-dev 容器读取 /ckad/log/input.log ,并将数据输出到 **/ckad/log/output.*** 格式为 Fluentd JSON

    请注意 :完成此任务不需要了解 Fluentd , 完成此任务所需要的知识 . 从/ckad/KDMC00102/fluentd-configmap.yaml 提供规范文件中创建 configmap ,并将该 configmap 挂载到边车容器 adapter-dev 中的 /fluentd/etc

参考:

https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/logging/

apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  containers:
  - name: count
    image: busybox:1.28
    args:
    - /bin/sh
    - -c
    - >
      i=0;
      while true;
      do
        echo "$i: $(date)" >> /var/log/1.log;
        echo "$(date) INFO $i" >> /var/log/2.log;
        i=$((i+1));
        sleep 1;
      done      
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-1
    image: busybox:1.28
    args: [/bin/sh, -c, 'tail -n+1 -F /var/log/1.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: count-log-2
    image: busybox:1.28
    args: [/bin/sh, -c, 'tail -n+1 -F /var/log/2.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}

解答:

切换环境

kubectl config use-context k8s

编辑fluentdyaml文件,考试时一般是正确的不需要修改

cp /ckad/KDMC00102/fluentd-configmap.yaml fluentd-configmap.yaml.bak
vim fluentd-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
data:
  fluent.conf: |
    
      @type tail
      format none
      path /ckad/log/input.log #修改
      tag cncf #修改
      
        @type none
      
    
     #修改
      @type file
      path /ckad/log/output #修改
    

应用

kubectl apply -f fluentd-configmap.yaml

创建sidecar yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  deploymend-web
  namespace: default
  labels:
    app:  deploymend-web
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app:  nginx
    spec:
      containers:
      - image:  lfccncf/busybox:1 
        name:  logger-123
        args:
        - /bin/sh
        - -c
        - >
          while true; do
            echo "i luv cncf" >> /ckad/log/input.log; sleep 10;
          done
        volumeMounts:
        - name: log
          mountPath: /ckad/log
      - image:   lfccncf/fluentd:v0.12
        name:  adaptor-dev
        volumeMounts:
        - name: log
          mountPath: /ckad/log
        - name: config
          mountPath: /fluentd/etc
      volumes:
      - name: log
        emptyDir: {}
      - name: config 
        configMap:
          name: fluentd-config

应用

kubectl apply -f sidecar.yaml

检查

kubectl get deployments,pod
kubectl exec deploymend-web-cfbccb8c7-xts4f -c adaptor-dev -- tail /ckad/log/input.log

file