k8s部署loki(distributed模式)

安装说明
shell 复制代码
因为官网弃用了promtail明年3月彻底弃用。使用alloy 功能更强大
helm repo add grafana https://grafana.github.io/helm-charts
helm pull grafana/loki
helm pull  grafana/alloy

分布式日志服务安装

shell 复制代码
helm -n loki install loki loki-6.49.0.tgz -f values.yaml
最后安装
shell 复制代码
helm -n loki install my-alloy grafana/alloy --set "configMap.content=loki.write \"loki\" {\n  endpoint {\n    url = \"http://loki-write:3100/loki/api/v1/push\"\n  }\n}"
在安装granfna
shell 复制代码
kubectl apply -f grafana.yaml

安装完后需要alloy的添加参数如下configmap修改:

yaml 复制代码
#在配置项最后添加
discovery.kubernetes "pod" {
  role = "pod"
  selectors {
    role = "pod"
    field = "spec.nodeName=" + coalesce(sys.env("HOSTNAME"), constants.hostname)
  }
}


discovery.relabel "pod_logs" {
  targets = discovery.kubernetes.pod.targets

  rule {
    source_labels = ["__meta_kubernetes_namespace"]
    action = "replace"
    target_label = "namespace"
  }

  rule {
    source_labels = ["__meta_kubernetes_pod_name"]
    action = "replace"
    target_label = "pod"
  }

  rule {
    source_labels = ["__meta_kubernetes_pod_container_name"]
    action = "replace"
    target_label = "container"
  }

  rule {
    source_labels = ["__meta_kubernetes_pod_label_app_kubernetes_io_name"]
    action = "replace"
    target_label = "app"
  }

  rule {
    source_labels = ["__meta_kubernetes_namespace", "__meta_kubernetes_pod_container_name"]
    action = "replace"
    target_label = "job"
    separator = "/"
    replacement = "$1"
  }


  rule {
    source_labels = ["__meta_kubernetes_pod_uid", "__meta_kubernetes_pod_container_name"]
    action = "replace"
    target_label = "__path__"
    separator = "/"
    replacement = "/var/log/pods/*$1/*.log"
  }

  rule {
    source_labels = ["__meta_kubernetes_pod_container_id"]
    action = "replace"
    target_label = "container_runtime"
    regex = "^(\\S+):\\/\\/.+$"
    replacement = "$1"
  }
}

loki.source.kubernetes "pod_logs" {
  targets    = discovery.relabel.pod_logs.output
  forward_to = [loki.process.pod_logs.receiver]
}

#集群事件收集
loki.source.kubernetes_events "cluster_events" {
  job_name   = "integrations/kubernetes/eventhandler"
  log_format = "logfmt"
  forward_to = [
    loki.process.cluster_events.receiver,
  ]
}

loki.process "cluster_events" {
  forward_to = [loki.write.loki.receiver]

  stage.static_labels {
    values = {
      cluster = "my-events",
    }
  }
  
  stage.labels {
    values = {
      kubernetes_cluster_events = "job",
    }
  }
}



loki.process "pod_logs" {
  stage.static_labels {
      values = {
        cluster = "my-alloy2",
      }
  }

  forward_to = [loki.write.loki.receiver]
}


loki.write "loki" {
    endpoint {
    name = "loki-write"
    url = "http://loki-write:3100/loki/api/v1/push"
    headers = {
      "X-Scope-OrgID" = "fake",
   }
  }
  external_labels = {
   
      cluster = "my-alloy",
      source  = "alloy",
    }
  }

grafana

或者用的yaml

yaml 复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-datasources
data:
  loki-datasource.yaml: |
    apiVersion: 1
    datasources:
      - name: Loki
        type: loki
        access: proxy
        url: http://loki-gateway:80  #如果是 SingleBinary 模式下直接用 loki:3100
        version: 1
        editable: true
        jsonData:
          httpHeaderName1: "X-Scope-OrgID"
        secureJsonData:
          httpHeaderValue1: "fake"  # 如果是多租户模式,替换为实际 tenant ID

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
spec:
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      securityContext:
        fsGroup: 472
        supplementalGroups:
          - 0
      containers:
        - name: grafana
          image: grafana/grafana:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3000
              name: http-grafana
              protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /robots.txt
              port: 3000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 30
            successThreshold: 1
            timeoutSeconds: 2
          livenessProbe:
            failureThreshold: 3
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            tcpSocket:
              port: 3000
            timeoutSeconds: 1
          resources:
            requests:
              cpu: 250m
              memory: 750Mi
          volumeMounts:
            - mountPath: /var/lib/grafana
              name: grafana-pv
            # 新增:挂载数据源 provisioning 配置
            - mountPath: /etc/grafana/provisioning/datasources/loki-datasource.yaml
              name: grafana-datasources
              subPath: loki-datasource.yaml
      # 新增 volumes
      volumes:
        - name: grafana-pv
          persistentVolumeClaim:
            claimName: grafana-pvc
        - name: grafana-datasources
          configMap:
            name: grafana-datasources

---
apiVersion: v1
kind: Service
metadata:
  name: grafana
spec:
  ports:
    - port: 3000
      protocol: TCP
      targetPort: http-grafana
  selector:
    app: grafana
  sessionAffinity: None
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-pvc
spec:
  storageClassName: "local-path"
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
相关推荐
openFuyao15 小时前
新增Agent沙箱调度能力!openFuyao v26.09社区发行版上线,AI原生基础设施能力进一步升级
云原生·ai-native·ai推理·openfuyao·多样化算力集群软件
谢亮_vipxieliang15 小时前
Kubernetes 1.36 深度解读:从 kubelet 安全到 GPU 原生调度的 70 项改进
安全·kubernetes·kubelet
阿里云云原生18 小时前
云效工作项智能优化:先澄清,再开工
云原生
阿里云云原生18 小时前
阿里云日志服务 SLS 全新升级,打造 Agent 时代的智能数据引擎
云原生
阿里云云原生18 小时前
从“治已病”到“治未病”:浩瀚能源与阿里云共建充电补能异地双活高可用体系实践
云原生
wzq11_66618 小时前
kubernetes集群——灰度发布
云原生·容器·kubernetes
阿里云云原生20 小时前
阿里云发布 Alibaba Cloud AI Agent Handbook,阐述“智能面积”
云原生
灯澜忆梦21 小时前
【docker】#1 | Docker 初识
运维·docker·容器
骑着蜗牛撵大象3271 天前
穿越 Docker 内核迷雾:镜像分层的叠加态与卷挂载的空间穿梭
运维·docker·容器·联合文件系统·卷挂载·镜像分层·存储驱动
吴声子夜歌1 天前
Docker入门与实战——Docker数据管理
docker·容器