该文档主要介绍使用logtail以两种不同的方式进行k8s挂载Nas后的日志采集。两种采集方式的实现原理是一样的,都是通过将Logtail和业务容器挂载到相同的NAS上,使Logtail和业务容器的日志数据共享,以此实现日志采集。下面是两种采集方式的各自特点:
通过 链接 使用PV&PVC的方式配置挂载Nas的nas-pvc
apiVersion: batch/v1
kind: Job
metadata:
name: nginx-log-sidecar1-demo
spec:
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"
__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名称
参数
|
说明
|
${your_region_config}
|
该参数由日志服务Project所在Region以及网络类型决定,请根据网络类型输入正确的格式。包括:
其中,region为 表一,请根据Project地域选择正确的参数。
|
${your_aliyun_user_id}
|
用户标识,请替换为您的阿里云
主账号用户ID。主账号用户ID为字符串形式,如何查看ID请参考 用户标识配置中的2.1节。
说明?用户标识一定是?
主账号用户ID,子账号ID没有任何意义。
|
${your_machine_group_user_defined_id}
|
您集群的机器组自定义标识。需确保该标识在您的日志服务所在Region内唯一。详细内容可参考 创建用户自定义标识机器组。
|
注意项:副本数spec.replicas只能为1,不能更多,多了会重复采集。
首先,创建一个logtail的deployment,以下是本次使用的模板:
apiVersion: apps/v1
kind: Deployment
metadata:
name: logtail-deployment
namespace: kube-system
labels:
k8s-app: nas-logtail-collecter
spec:
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
logtail运行成功之后,可以在SLS控制台根据模板中的ALIYUN_LOGTAIL_USER_DEFINED_ID创建对应的机器组,请参考方案1中的表格说明。
这里新建2个Pod来测试采集是否成功,其中一个POD的模板为:
apiVersion: v1
kind: Pod
metadata:
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文件的按钮