题目内容
考点相关内容分析
日志架构
虽然 Kubernetes 没有为集群级日志记录提供原生的解决方案,但是提供了以下几种方式收集容器日志:
-
使用在每个节点上运行的节点级日志记录代理。
在每个节点上部署一个日志代理,该代理负责收集节点上所有容器的日志,并将它们发送到集中式的日志存储或分析系统中。
-
在应用程序的 Pod 中,包含专门记录日志的边车(Sidecar)容器。
该方案是将日志记录功能与应用程序容器分离开来,以确保日志记录不会影响应用程序的性能或稳定性,并且使得日志记录的配置更加灵活和可控。
Sidecar 配置文件示例
apiVersion: v1
kind: Pod
metadata:
name: counter # 设置 Pod 的名称为 "counter"
spec:
containers:
- name: count # 第一个容器的名称为 "count"
image: busybox:1.28 # 使用 BusyBox 1.28 镜像
args: # 定义容器的启动参数
- /bin/sh # 使用 shell 运行命令
- -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 # 使用名为 "varlog" 的卷
mountPath: /var/log # 将卷挂载到容器的 /var/log 目录下
- name: count-agent # 第二个容器的名称为 "count-agent"
image: registry.k8s.io/fluentd-gcp:1.30 # 使用 Fluentd GCP 镜像
env: # 定义环境变量
- name: FLUENTD_ARGS # 环境变量名称为 FLUENTD_ARGS
value: -c /etc/fluentd-config/fluentd.conf # 设置环境变量的值为 Fluentd 配置文件的路径
volumeMounts: # 挂载卷配置
- name: varlog # 使用名为 "varlog" 的卷
mountPath: /var/log # 将卷挂载到容器的 /var/log 目录下
- name: config-volume # 使用名为 "config-volume" 的卷
mountPath: /etc/fluentd-config # 将卷挂载到容器的 /etc/fluentd-config 目录下
volumes: # 卷配置
- name: varlog # 名称为 "varlog" 的卷
emptyDir: {} # 使用空目录作为卷,该目录会在 Pod 重新启动时被清空
- name: config-volume # 名称为 "config-volume" 的卷
configMap: # 使用 ConfigMap 作为卷
name: fluentd-config # ConfigMap 的名称为 "fluentd-config"
- 将日志直接从应用程序中推送到日志记录后端。
题目操作步骤
- (标准步骤) 进入题目指定集群
$ kubectl config use-context k8s
- 导出 Pod 配置参数导出到 yaml 文件
$ kubectl get pod 11-factor-app -o yaml > varlog.yaml
- 根据题目要求编辑配置文件内容
$ cp varlog.yaml varlog.yaml.bak #备份配置参数
$ vim varlog.yaml
spec:
containers:
- args:
- /bin/sh
- -c
- |
i=0; while true; do
echo "$(date) INFO $i" >> /var/log/11-factor-app.log;
i=$((i+1));
sleep 1;
done
image: busybox
imagePullPolicy: IfNotPresent
name: count
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-gt98r
readOnly: true
- name: varlog # 新增内容
mountPath: /var/log # 新增内容
- name: sidecar # 新增内容
image: busybox # 新增内容
imagePullPolicy: IfNotPresent # 新增内容
args: [/bin/sh, -c, 'tail -n+1 -f /var/log/11-factor-app.log'] #新增内容
volumeMounts: # 新增内容
- name: varlog # 新增内容
mountPath: /var/log # 新增内容
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node01
nodeSelector:
kubernetes.io/hostname: node01
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: varlog # 新增内容
emptyDir: {} # 新增内容
- name: kube-api-access-gt98r
projected:
defaultMode: 420
- 重新编辑 Pod
$ kubectl delete pod 11-factor-app # 删除原 Pod
$ kubectl apply -f varlog.yaml
检查命令
查看日志内容
# 日志内容不对扣四分
$ kubectl logs 11-factor-app sidecar
参考链接:https://kubernetes.io/zh-cn/docs/concepts/cluster-administration/logging/#sidecar-container-with-logging-agent