本文为 SPIRE 官方文档 plugin_server_notifier_k8sbundle 的中文译本,以英文原文为准。 🔗 穿越源码分析:Server 详解 · Notifier
Server 插件:Notifier "k8sbundle"
k8sbundle 插件会在证书包加载/更新事件(bundle loaded/updated)发生时作出响应,从信任包(trust bundle)中获取最新的根 CA 证书,并将其推送到 Kubernetes 的 ConfigMap 中,还可选地推送到 Webhook 和 APIService。
ConfigMap 中的证书可用于引导(bootstrap)SPIRE agent。
该插件接受以下配置选项:
| 配置项 | 说明 | 默认值 |
|---|---|---|
| namespace | ConfigMap 所在的命名空间(namespace) | spire |
| config_map | ConfigMap 的名称 | spire-bundle |
| config_map_key | ConfigMap 中存放证书包的键(key) | bundle.crt |
| kube_config_file_path | 磁盘上 kubeconfig 的路径,其中包含用于与 Kubernetes API server 交互的配置。若未设置,则假定通知器运行于集群内(in-cluster),并使用集群内凭据。配置远程集群时必填。配置多个远程集群请参见 clusters 设置。 | |
| api_service_label | 若设置,则在带有该标签且其值为 true 的 API service 中轮换 CA 证书包。 | |
| webhook_label | 若设置,则在带有该标签且其值为 true 的校验型(validating)和变更型(mutating)webhook 中轮换 CA 证书包。 | |
| clusters | 远程集群配置列表。若设置,可用于配置多个集群。每个集群支持与根配置相同的取值。 |
配置 Kubernetes
要设置该插件,需要执行以下操作:
- 将可以对 ConfigMap 执行
get和patch的 ClusterRole 或 Role 绑定到 Service Account。- 对于集群内(in-cluster)的 SPIRE server,即运行 SPIRE Server 的 Service Account。
- 对于集群外(out-of-cluster)的 SPIRE Server,即与 Kubernetes API server 交互的 Service Account。
- 若设置了
webhook_label,该 ClusterRole 或 Role 还需要对mutatingwebhookconfigurations和validatingwebhookconfigurations具备get、list、patch和watch权限。 - 若设置了
api_service_label,该 ClusterRole 或 Role 还需要对apiservices具备get、list、patch和watch权限。
- 创建插件所推送的 ConfigMap。
例如:
在本例中,假设 Service Account 为 spire-server。
yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: spire-server-cluster-role
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: spire-server-cluster-role-binding
subjects:
- kind: ServiceAccount
name: spire-server
namespace: spire
roleRef:
kind: ClusterRole
name: spire-server-cluster-role
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
name: spire-bundle
namespace: spire轮换 Webhook 和 API Service CA 证书包时的配置
在轮换 webhook 和 API Service 的 CA 证书包时,使用下面的 ClusterRole:
yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: spire-server-cluster-role
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "patch"]
- apiGroups: ["admissionregistration.k8s.io"]
resources: ["mutatingwebhookconfigurations", "validatingwebhookconfigurations"]
verbs: ["get", "list", "patch", "watch"]
- apiGroups: ["apiregistration.k8s.io"]
resources: ["apiservices"]
verbs: ["get", "list", "patch", "watch"]示例配置
默认集群内部署,仅轮换 ConfigMap
以下配置将证书包内容从集群内的 SPIRE server 推送到 spire:spire-bundle ConfigMap 中的 bundle.crt 键。
hcl
Notifier "k8sbundle" {
plugin_data {
}
}集群外部署
以下配置将证书包内容从集群外的 SPIRE server 推送到 infra:agents ConfigMap 中的 boostrap.crt 键,使用 /path/to/kubeconfig 文件中的凭据。
hcl
Notifier "k8sbundle" {
plugin_data {
namespace = "infra"
config_map = "agents"
config_map_key = "bootstrap.crt"
kube_config_file_path = "/path/to/kubeconfig"
}
}默认集群内部署,同时轮换 ConfigMap、Webhook 和 APIService
以下配置将证书包内容从集群内的 SPIRE server 推送到:
spire:spire-bundleConfigMap 中的bundle.crt键- 带有标签
spiffe.io/webhook: true的校验型和变更型 webhook - 带有标签
spiffe.io/api_service: true的 API service
hcl
Notifier "k8sbundle" {
plugin_data {
webhook_label = "spiffe.io/webhook"
api_service_label = "spiffe.io/api_service"
}
}多集群
hcl
Notifier "k8sbundle" {
plugin_data {
# 本地集群
namespace = "spire"
# 额外集群
clusters = [
{
kube_config_file_path = "/cluster2/file/path"
},
{
kube_config_file_path = "/cluster3/file/path"
}
]
}
}