Kubernetes(k8s)如何使用Webhook身份验证器进行外部认证?
xiaolong88:Kubernetes通过Webhook Token身份验证实现外部认证,步骤如下:
部署外部认证服务:
开发或部署支持HTTPS的认证服务,接收包含token的POST请求(如https://auth-service/authenticate)。
服务需返回JSON响应,格式为{"apiVersion": "authentication.k8s.io/v1", "kind": "TokenReview", "status": {"authenticated": true, "user": {"username": "user1", "groups": ["dev"]}}}。
配置Kubernetes API Server:
创建Webhook配置文件(如webhook-config.yaml):
apiVersion: v1
kind: Config
clusters:
- name: auth-webhook
cluster:
server: https://auth-service/authenticate
certificate-authority: /path/to/ca.crt
users: []
current-context: webhook
contexts:
- context:
cluster: auth-webhook
user: ""
name: webhook
在kube-apiserver启动参数中添加:
--authentication-token-webhook-config-file=/etc/kubernetes/webhook-config.yaml
--authentication-token-webhook-cache-ttl=60s
证书配置:
若使用自签名证书,将CA证书挂载到API Server的Pod中(路径需与配置文件一致)。
测试认证:
curl -k -H "Authorization: Bearer <TOKEN>" https://<API-SERVER>/api
检查API Server日志或认证服务日志验证流程。
RBAC授权:
创建RoleBinding,将认证后的用户/组绑定到对应权限(如kubectl create rolebinding dev-user --user=user1 --role=read-only)。
常见问题排查:
证书错误:检查API Server与认证服务的证书链是否信任。
HTTP 503:确认认证服务可达且响应格式正确。
权限不足:通过kubectl auth can-i <verb> <resource>验证RBAC配置。
497
2025-02-21 17:57:00