我有一些kubernetes应用程序,它们记录到文件而不是stdout/stderr,我用Promtail sidecars收集它们。但是由于侧车执行";localhost";target,我没有一个kubernetes_sd_config
来为我的标签应用pod元数据。所以我只能静态地声明我的标签。
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
labels:
app: promtail
name: sidecar-promtail
data:
config.yml: |
client:
url: http://loki.loki.svc.cluster.local:3100/loki/api/v1/push
backoff_config:
max_period: 5m
max_retries: 10
min_period: 500ms
batchsize: 1048576
batchwait: 1s
external_labels: {}
timeout: 10s
positions:
filename: /tmp/promtail-positions.yaml
server:
http_listen_port: 3101
target_config:
sync_period: 10s
scrape_configs:
- job_name: sidecar-logs
static_configs:
- targets:
- localhost
labels:
job: sidecar-logs
__path__: "/sidecar-logs/*.log"
----
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-logger
spec:
selector:
matchLabels:
run: test-logger
template:
metadata:
labels:
run: test-logger
spec:
volumes:
- name: nfs
persistentVolumeClaim:
claimName: nfs-claim
- name: promtail-config
configMap:
name: sidecar-promtail
containers:
- name: sidecar-promtail
image: grafana/promtail:2.1.0
volumeMounts:
- name: nfs
mountPath: /sidecar-logs
- mountPath: /etc/promtail
name: promtail-config
- name: simple-logger
image: foo/simple-logger
volumeMounts:
- name: nfs
mountPath: /logs
根据父pod的元数据标记收集的日志的最佳方式是什么?
您可以执行以下操作:
在sidecar容器中,将pod名称、节点名称和其他需要的信息作为环境变量公开,然后在promtail配置文件中添加标志'-config.expand-env'
以启用环境扩展,例如:
...
- name: sidecar-promtail
image: grafana/promtail:2.1.0
# image: grafana/promtail:2.4.1 # use this one if environment expansion is not available in 2.1.0
args:
# Enable environment expansion in promtail config file
- '-config.expand-env'
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
...
然后在configMap中,在static_config标签中添加环境变量,如下所示:
...
scrape_configs:
- job_name: sidecar-logs
static_configs:
- targets:
- localhost
labels:
job: sidecar-logs
pod: ${POD_NAME}
node_name: ${NODE_NAME}
__path__: "/sidecar-logs/*.log"
...