(图片来源网络,侵删)
采集k8s挂载Nas后的日志该文档主要介绍使用logtail以两种不同的方式进行k8s挂载Nas后的日志采集两种采集方式的实现原理是一样的,都是通过将Logtail和业务容器挂载到相同的NAS上,使Logtail和业务容器的日志数据共享,以此实现日志采集下面是两种采集方式的各自特点:SideCar模式比较灵活、适合水平扩容,适用于数据量较大的场景;单独部署Logtail的Deployment资源消耗比较低、但灵活性以及伸缩性不强,适用于整体集群数据量较少的场景(建议整体日志量不超过每秒10M)1. Sidecar NAS采集方式通过 链接 使用PV&PVC的方式配置挂载Nas的nas-pvc步骤一 创建pv步骤二 创建pvc步骤三 根据下面的yaml模板创建含有logtail的Pod,进行单个Pod的内部采集sideCar模式实验yaml内容:apiVersion: batch/v1kind: Jobmetadata: name: nginx-log-sidecar1-demospec: template: metadata: name: nginx-log-sidecar-demo spec: # volumes配置 volumes: - name: nginx-log persistentVolumeClaim: claimName: nas-pvc containers: # 主容器配置 - name: nginx-log-demo image: registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest command: [\"/bin/mock_log\"] args: [\"--log-type=nginx\", \"--stdout=false\", \"--stderr=true\", \"--path=/var/log/nginx/access.log\", \"--total-count=1000000000\", \"--logs-per-sec=100\"] volumeMounts: - name: nginx-log mountPath: /var/log/nginx # Logtail的Sidecar容器配置 - name: logtail image: registry.cn-hangzhou.aliyuncs.com/log-service/logtail:latest env: # user id - name: \"ALIYUN_LOGTAIL_USER_ID\" value: \"${your_aliyun_user_id}\" # user defined id - name: \"ALIYUN_LOGTAIL_USER_DEFINED_ID\" value: \"${your_machine_group_user_defined_id}\" # config file path in logtail's container - name: \"ALIYUN_LOGTAIL_CONFIG\" value: \"/etc/ilogtail/conf/${your_region_config}/ilogtail_config.json\" # env tags config - name: \"ALIYUN_LOG_ENV_TAGS\" value: \"_pod_name_|_pod_ip_|_namespace_|_node_name_|_node_ip_\" - name: \"_pod_name_\" valueFrom: fieldRef: fieldPath: metadata.name - name: \"_pod_ip_\" valueFrom: fieldRef: fieldPath: status.podIP - name: \"_namespace_\" valueFrom: fieldRef: fieldPath: metadata.namespace - name: \"_node_name_\" valueFrom: fieldRef: fieldPath: spec.nodeName - name: \"_node_ip_\" valueFrom: fieldRef: fieldPath: status.hostIP # 和主容器共享volume volumeMounts: - name: nginx-log mountPath: /var/log/nginx # 健康检查 livenessProbe: exec: command: - /etc/init.d/ilogtaild - status initialDelaySeconds: 30 periodSeconds: 30 restartPolicy: \"Never\"SLS控制台采集配置设置如下图:日志路径与被采集容器的日志所在路径一致注意:由于NAS路径已经挂载到了Logtail容器上,所以不需要打开docker文件的按钮采集上来的系统默认字段含义:__source__: pod容器内部IP__tag__:__hostname__: pod名称__tag__:__path__: 日志路径__tag__:__receive_time__: 采集时间__tag__:__user_defined_id__: 用户自定义标识__tag__:_namespace_: pod所属namaspace__tag__:_node_ip_: pod所在Node的IP地址__tag__:_node_name_: pod所属Node的name__tag__:_pod_ip_: pod容器内部IP__tag__:_pod_name_: pod名称用户参数:2. 一个Logtail采集所有POD的NAS数据注意项:副本数spec.replicas只能为1,不能更多,多了会重复采集首先,创建一个logtail的deployment,以下是本次使用的模板:apiVersion: apps/v1kind: Deploymentmetadata: name: logtail-deployment namespace: kube-system labels: k8s-app: nas-logtail-collecterspec: replicas: 1 selector: matchLabels: k8s-app : nas-logtail-collecter template: metadata: name: logtail-deployment labels: k8s-app : nas-logtail-collecter spec: containers: # Logtail的配置 - name: logtail image: registry.cn-hangzhou.aliyuncs.com/log-service/logtail:latest env: # aliuid - name: \"ALIYUN_LOGTAIL_USER_ID\" value: \"${your_aliyun_user_id}\" # user defined id - name: \"ALIYUN_LOGTAIL_USER_DEFINED_ID\" value: \"${your_machine_group_user_defined_id}\" # config file path in logtail's container - name: \"ALIYUN_LOGTAIL_CONFIG\" value: \"/etc/ilogtail/conf/${your_region_config}/ilogtail_config.json\" volumeMounts: - name: nginx-log mountPath: /var/log/nginx # volumes配置 volumes: - name: nginx-log persistentVolumeClaim: claimName: pvc-test-nginx__注意:__这里的 claimName: pvc-test-nginx 以及mountPath: /var/log/nginx 是将logtail的/var/log/nginx挂载了Nas下的/nginx文件夹相关参数设置请参考方案1中的表格说明logtail运行成功之后,可以在SLS控制台根据模板中的ALIYUN_LOGTAIL_USER_DEFINED_ID创建对应的机器组,请参考方案1中的表格说明这里新建2个Pod来测试采集是否成功,其中一个POD的模板为:apiVersion: v1kind: Podmetadata: name: \"test-nginx-2\"spec: containers: - name: \"nginx-log-demo\" image: \"registry.cn-hangzhou.aliyuncs.com/log-service/docker-log-test:latest\" command: [\"/bin/mock_log\"] args: [\"--log-type=nginx\", \"--stdout=false\", \"--stderr=true\", \"--path=/var/log/nginx/access.log\", \"--total-count=1000000000\", \"--logs-per-sec=100\"] volumeMounts: - name: \"nas2\" mountPath: \"/var/log/nginx\" volumes: - name: \"nas2\" flexVolume: driver: \"alicloud/nas\" options: server: \"Nas挂载地址\" path: \"/nginx/test2\" vers: \"4.0\"另一个Pod将 /var/log/nginx 挂载在了 /nginx/test1 目录下;结合logtail的挂载情况,现在两个Pod分别挂载在 /nginx/test1 和 /nginx/test2,而logtail挂载在了 /nginx 下最后配置logtail的采集配置因为logtail也挂载了相同的Nas,所以logtail只需要采集自身文件夹下的日志就可以了,这里的是否为docker文件选项关闭注意:由于NAS路径已经挂载到了Logtail容器上,所以不需要打开docker文件的按钮作者:元乙
0 评论