菜单
文档breadcrumb arrow Grafana Alloybreadcrumb arrow 参考breadcrumb arrow 组件breadcrumb arrow lokibreadcrumb arrow loki.source.kubernetes
正式发布 (GA) 开源

loki.source.kubernetes

loki.source.kubernetes 使用 Kubernetes API 从 Kubernetes 容器中尾随日志。

注意

此组件收集 Kubernetes Pod 中的日志。您不能使用此组件收集 Kubernetes 节点中的日志。

此组件相比 loki.source.file 具有以下优势:

  • 它无需特权容器即可工作。
  • 它无需 root 用户即可工作。
  • 它无需访问 Kubernetes 节点的文件系统即可工作。
  • 它不需要 DaemonSet 来收集日志,因此一个 Alloy 实例可以收集整个集群的日志。

注意

由于 loki.source.kubernetes 使用 Kubernetes API 尾随日志,因此它比 loki.source.file 使用更多的网络流量和 Kubelet 的 CPU 消耗。

您可以通过为多个 loki.source.kubernetes 组件指定不同的标签来使用它们。

用法

alloy
loki.source.kubernetes "<LABEL>" {
  targets    = <TARGET_LIST>
  forward_to = <RECEIVER_LIST>
}

参数

该组件为每个给定的 targets 启动一个新的读取器,并将日志条目分发到 forward_to 中传递的接收器列表。

您可以配合 loki.source.kubernetes 使用以下参数:

名称类型描述默认值必需
forward_tolist(LogsReceiver)发送日志条目的接收器列表。
targetslist(map(string))要读取的文件列表。

targets 中的每个目标必须包含以下标签:

  • __meta_kubernetes_namespace__pod_namespace__ 用于指定要尾随日志的 Pod 的命名空间。
  • __meta_kubernetes_pod_container_name__pod_container_name__ 用于指定要尾随日志的 Pod 中的容器。
  • __meta_kubernetes_pod_name__pod_name__ 用于指定要尾随日志的 Pod 的名称。
  • __meta_kubernetes_pod_uid__pod_uid__ 用于指定要尾随日志的 Pod 的 UID。

默认情况下,当使用 discovery.kubernetes 输出时,所有这些标签都存在。

对于 targets 中的每个唯一目标,都会启动一个日志尾随器。如果日志流在容器永久终止之前返回,日志尾随器会以指数退避方式重新连接到 Kubernetes。

您可以配合 loki.source.kubernetes 使用以下块:

描述必需
client配置用于尾随日志的 Kubernetes 客户端。
client > authorization配置端点的通用授权。
client > basic_auth配置 basic_auth 以进行端点认证。
client > oauth2配置 OAuth 2.0 以进行端点认证。
client > oauth2 > tls_config配置连接到端点的 TLS 设置。
client > tls_config配置连接到端点的 TLS 设置。
clustering配置组件以在 Alloy 以集群模式运行时使用。

The > symbol indicates deeper levels of nesting. For example, client > basic_auth refers to a basic_auth block defined inside a client block.

client

> 符号表示更深的嵌套级别。例如,client > basic_auth 指的是在 client 块内部定义的 basic_auth 块。

The client block configures the Kubernetes client used to tail logs from containers. If the client block isn’t provided, the default in-cluster configuration with the service account of the running Alloy Pod is used.

名称类型描述默认值必需
client 块配置用于从容器尾随日志的 Kubernetes 客户端。如果未提供 client 块,则使用运行中的 Alloy Pod 的服务账号的默认集群内配置。stringThe following arguments are supported
支持以下参数stringapi_server
api_serverURL of the Kubernetes API server.Kubernetes API 服务器的 URL。
bearer_token_filebearer_token_fileFile containing a bearer token to authenticate with.包含用于认证的持有者令牌的文件。
bearer_tokenbearer_token_filebearer_token包含用于认证的持有者令牌的文件。
secretsecretBearer token to authenticate with.
用于认证的持有者令牌。stringenable_http2
enable_http2stringbool
boolsecretWhether HTTP2 is supported for requests.
请求是否支持 HTTP2。bearer_token_filetruetrue
follow_redirectsstringfollow_redirects

Whether redirects returned by the server should be followed.

  • 是否应遵循服务器返回的重定向。
  • http_headers
  • http_headers
  • map(list(secret))
  • map(list(secret))

Custom HTTP headers to be sent along with each request. The map key is the header name.

要随每个请求发送的自定义 HTTP 头。映射键是头名称。

kubeconfig_file

kubeconfig_file

名称类型描述默认值必需
Path of the kubeconfig file to use for connecting to Kubernetes.string用于连接到 Kubernetes 的 kubeconfig 文件路径。
no_proxyURL of the Kubernetes API server.no_proxy
Comma-separated list of IP addresses, CIDR notations, and domain names to exclude from proxying.string要从代理中排除的 IP 地址、CIDR 表示法和域名的逗号分隔列表。

proxy_connect_header

proxy_connect_header

名称类型描述默认值必需
Specifies headers to send to proxies during CONNECT requests.string指定在 CONNECT 请求期间发送到代理的头。
proxy_from_environmentURL of the Kubernetes API server.proxy_from_environment
Use the proxy URL indicated by environment variables.string使用环境变量指示的代理 URL。

false

false

名称类型描述默认值必需
proxy_urlstringproxy_url
HTTP proxy to send requests through.string用于发送请求的 HTTP 代理。
At most, one of the following can be providedURL of the Kubernetes API server.以下参数中最多只能提供一个
authorization blockauthorizationbasic_auth block
follow_redirectsstringfollow_redirects
enable_http2stringbool
请求是否支持 HTTP2。bearer_token_filetruetrue
boolsecretWhether HTTP2 is supported for requests.
basic_authbearer_token_file argumentbearer_token_file 参数
bearer_token argumentstringbearer_token 参数

oauth2 block

oauth2

Custom HTTP headers to be sent along with each request. The map key is the header name.

要随每个请求发送的自定义 HTTP 头。映射键是头名称。

kubeconfig_file

no_proxy can contain IPs, CIDR notations, and domain names. IP and domain names can contain port numbers. proxy_url must be configured if no_proxy is configured.

名称类型描述默认值必需
no_proxy 可以包含 IP、CIDR 表示法和域名。IP 和域名可以包含端口号。如果配置了 no_proxy,则必须配置 proxy_urlstringproxy_from_environment uses the environment variables HTTP_PROXY, HTTPS_PROXY, and NO_PROXY (or the lowercase versions thereof). Requests use the proxy from the environment variable matching their scheme, unless excluded by NO_PROXY. proxy_url and no_proxy must not be configured if proxy_from_environment is configured.
proxy_from_environment 使用环境变量 HTTP_PROXY, HTTPS_PROXY 和 NO_PROXY(或其小写版本)。请求使用与其方案匹配的环境变量中的代理,除非被 NO_PROXY 排除。如果配置了 proxy_from_environment,则不得配置 proxy_urlno_proxystringproxy_connect_header should only be configured if proxy_url or proxy_from_environment are configured.
仅当配置了 proxy_urlproxy_from_environment 时才应配置 proxy_connect_headerstringauthorization
authorizationstringcredentials_file
credentials_filebearer_token_fileFile containing the secret value.
包含 Secret 值的文件。stringcredentials
credentialsURL of the Kubernetes API server.Secret value.
Secret 值。stringtype
typestringAuthorization type, for example, “Bearer”.

认证类型,例如,“Bearer”。

  • credential and credentials_file are mutually exclusive, and only one can be provided inside an authorization block.
  • credentialcredentials_file 相互排斥,且在 authorization 块内只能提供其中一个。
  • basic_auth

basic_auth

password_file

  • password_file
  • File containing the basic auth password.
  • 包含基本认证密码的文件。
  • password

clustering

名称类型描述默认值必需
passwordbearer_token_fileBasic auth password.

基本认证密码。

username

username

  • Basic auth username.
  • 基本认证用户名。
  • password and password_file are mutually exclusive, and only one can be provided inside a basic_auth block.
  • passwordpassword_file 相互排斥,且在 basic_auth 块内只能提供其中一个。
  • oauth2
  • oauth2
  • client_id
  • client_id
  • OAuth2 client ID.
  • OAuth2 客户端 ID。
  • client_secret_file
  • client_secret_file

File containing the OAuth2 client secret.

包含 OAuth2 客户端 Secret 的文件。

client_secret

client_secret

OAuth2 client secret.

OAuth2 客户端 Secret。

  • endpoint_params
  • endpoint_params
  • map(string)
  • map(string)

Optional parameters to append to the token URL.

要添加到令牌 URL 的可选参数。

scopes

scopes

alloy
discovery.kubernetes "pods" {
  role = "pod"
}

loki.source.kubernetes "pods" {
  targets    = discovery.kubernetes.pods.targets
  forward_to = [loki.write.local.receiver]
}

loki.write "local" {
  endpoint {
    url = sys.env("<LOKI_URL>")
  }
}

兼容组件

list(string)

  • list(string)
  • List of scopes to authenticate with.

注意

用于认证的范围列表。