You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GKE环境无Helm部署Loki+Promtail转发日志遇端口配置问题

问题描述
  • 作为K8S基础设施新手,正将VM架构迁移至GCP/GKE环境,未使用Helm部署Loki与Promtail
  • Loki Pod运行状态与VM中Docker部署一致,但创建Promtail Service时出现报错:

The Service "promtail" is invalid: spec.ports: Required value

  • 需求:实现Promtail无需暴露端口即可正常运行

配置文件

loki-config.yml

auth_enabled: false

server:
  http_listen_port: 3100
  grpc_listen_port: 9096

ingester:
  wal:
    enabled: true
    dir: /tmp/wal
  lifecycler:
    address: 127.0.0.1
    ring:
      kvstore:
        store: inmemory
      replication_factor: 1
    final_sleep: 0s
  chunk_idle_period: 1h       # Any chunk not receiving new logs in this time will be flushed
  max_chunk_age: 1h           # All chunks will be flushed when they hit this age, default is 1h
  chunk_target_size: 1048576  # Loki will attempt to build chunks up to 1.5MB, flushing first if chunk_idle_period or max_chunk_age is reached first
  chunk_retain_period: 30s    # Must be greater than index read cache TTL if using an index cache (Default index read cache TTL is 5m)
  max_transfer_retries: 0     # Chunk transfers disabled

schema_config:
  configs:
    - from: 2020-10-24
      store: boltdb-shipper
      object_store: filesystem
      schema: v11
      index:
        prefix: index_
        period: 24h

storage_config:
  boltdb_shipper:
    active_index_directory: /tmp/loki/boltdb-shipper-active
    cache_location: /tmp/loki/boltdb-shipper-cache
    cache_ttl: 24h         # Can be increased for faster performance over longer query periods, uses more disk space
    shared_store: filesystem
  filesystem:
    directory: /tmp/loki/chunks

compactor:
  working_directory: /tmp/loki/boltdb-shipper-compactor
  shared_store: filesystem

limits_config:
  reject_old_samples: true
  reject_old_samples_max_age: 168h
  ingestion_burst_size_mb: 16
  ingestion_rate_mb: 16
chunk_store_config:
  max_look_back_period: 0s

table_manager:
  retention_deletes_enabled: false
  retention_period: 0s

ruler:
  storage:
    type: local
    local:
      directory: /tmp/loki/rules
  rule_path: /tmp/loki/rules-temp
  alertmanager_url: http://localhost:9093
  ring:
    kvstore:
      store: inmemory
  enable_api: true

promtail-config.yml

server:
  http_listen_port: 9080
  grpc_listen_port: 0

# this is the place where promtail will store the progress about how far it has read the logs
positions:
  filename: /tmp/positions.yaml

# address of loki server to which promtail should push the logs
clients:
  - url: https://999999:...=@logs-prod3.grafana.net/api/prom/push
# which logs to read/scrape
scrape_configs:
  - job_name: system
    static_configs:
    - targets:
        - localhost
      labels:
        job: varlogs
        __path__: /var/log/*log
  - job_name: node
    static_configs:
    - targets:
        - localhost
      labels:
        job: node  # label-1
        host: localhost    # label-2
        __path__: /var/lib/docker/containers/*/*log

loki-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: loki
spec:
  selector:
    matchLabels:
      app: loki
      network: cluster-1
  replicas: 1
  template:
    metadata:
      labels:
        app: loki
        network: cluster-1
    spec:
      containers:
        - name: loki
          image:  grafana/loki
          ports:
            - containerPort: 3100
          volumeMounts:
            - name: loki-config-volume
              mountPath: /etc/loki/loki.yml
              subPath: loki.yml
      volumes:
        - name: loki-config-volume
          configMap:
            name: "loki-config"
---
apiVersion: v1
kind: Service
metadata:
  name: loki
  namespace: monitoring
spec:
  selector:
    app: loki
  type: NodePort
  ports:
  - name: loki
    protocol: TCP
    port: 3100

promtail-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: promtail
spec:
  selector:
    matchLabels:
      app: promtail
      network: cluster-1
  replicas: 1
  template:
    metadata:
      labels:
        app: promtail
        network: cluster-1
    spec:
      containers:
        - name: promtail
          image:  grafana/promtail
          volumeMounts:
            - name: promtail-config-volume
              mountPath: /mnt/config/promtail-config.yml
              subPath: promtail.yml
      volumes:
        - name: promtail-config-volume
          configMap:
            name: "promtail-config"
---
apiVersion: v1
kind: Service
metadata:
  name: promtail
  namespace: monitoring
解决方案

核心处理:删除Promtail的Service定义

Promtail的工作逻辑是主动向配置的Loki服务推送日志(此处你配置的是Grafana Cloud的Loki地址),不需要接收集群内其他服务的请求,因此完全不需要为Promtail创建Service。

直接删除promtail-deploy.yaml中最后一段Service的配置即可:

---
apiVersion: v1
kind: Service
metadata:
  name: promtail
  namespace: monitoring

可选:若需访问Promtail接口的配置

如果之后需要访问Promtail的HTTP接口(比如查看metrics或健康状态),再添加Service并配置对应端口,示例如下:

apiVersion: v1
kind: Service
metadata:
  name: promtail
  namespace: monitoring
spec:
  selector:
    app: promtail
  ports:
  - name: http
    protocol: TCP
    port: 9080
    targetPort: 9080

(对应你Promtail配置中server.http_listen_port: 9080的端口)

额外优化:完善日志目录挂载

你的Promtail部署目前未挂载主机的日志目录,无法采集节点或容器日志,需在Deployment中添加以下挂载配置:

# 在promtail的Deployment spec.containers下新增volumeMounts:
volumeMounts:
  # 保留原有配置文件挂载
  - name: promtail-config-volume
    mountPath: /mnt/config/promtail-config.yml
    subPath: promtail.yml
  # 添加主机日志目录挂载
  - name: varlog
    mountPath: /var/log
  - name: dockercontainers
    mountPath: /var/lib/docker/containers
    readOnly: true

# 在Deployment spec.volumes下新增主机目录挂载:
volumes:
  # 保留原有ConfigMap挂载
  - name: promtail-config-volume
    configMap:
      name: "promtail-config"
  # 新增主机目录挂载
  - name: varlog
    hostPath:
      path: /var/log
  - name: dockercontainers
    hostPath:
      path: /var/lib/docker/containers

内容的提问来源于stack exchange,提问作者Matt - Block-Farms.io

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 15:54:24