kubernetes日志采集Loki(四)

2023-05-04 11:45:46 浏览数 (1)

部署日志收集器

最后,可以使用以下命令,部署Loki的日志收集器:

代码语言:javascript复制
apiVersion: v1
kind: ConfigMap
metadata:
  name: loki-agent-config
  namespace: monitoring
data:
  promtail-local-config.yaml: |-
    server:
      http_listen_port: 9080
      grpc_listen_port: 0
    positions:
      filename: /tmp/positions.yaml
    clients:
      - url: http://loki:3100/loki/api/v1/push
        tenant_id: k8s
        external_labels:
          cluster: test
          replica: "1"
          namespace: default
          pod_name: nginx-xxxxx
          container_name: nginx
    scrape_configs:
      - job_name: nginx
        static_configs:
          - targets: ["localhost"]
            labels:
              job: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: loki-agent
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: loki-agent
  replicas: 1
  template:
    metadata:
      labels:
        app: loki-agent
    spec:
      serviceAccountName: loki-datasource
      containers:
        - name: promtail
          image: grafana/promtail:latest
          imagePullPolicy: Always
          command: ["/usr/bin/promtail", "-config.file=/etc/promtail-local-config.yaml"]
          ports:
            - name: http
              containerPort: 9080
          volumeMounts:
            - name: config
              mountPath: /etc/promtail-local-config.yaml
              subPath: promtail-local-config.yaml
            - name: var-log
              mountPath: /var/log/nginx
              readOnly: true
          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
      volumes:
        - name: config
          configMap:
            name: loki-agent-config
        - name: var-log
          hostPath:
            path: /var/log/nginx

在此示例中,首先定义了一个ConfigMap对象,其中包含了Promtail的配置信息。然后,创建了一个Deployment对象,用于部署Loki的日志收集器。在Deployment对象中,指定了Promtail的镜像以及容器的端口号。此外,还指定了一个名为loki-agent-config的ConfigMap,用于存储Promtail的配置文件。在容器中,使用/etc/promtail-local-config.yaml路径挂载此ConfigMap。

在容器中,还指定了一个名为var-log的主机路径卷,用于存储Nginx的日志文件。为了将Nginx的日志文件发送到Loki,使用了Promtail的客户端库将日志发送到Loki中。在此示例中,将Nginx的日志发送到http://loki:3100/loki/api/v1/push中。

0 人点赞