kubernetes 二次开发-认证,鉴权(1)
基于webhook的认证
授权过程
认证授权服务需要满足如下kubernetes的规范
kubernetes api-server组件发送 http post 请求 url:https://authn.example.com/authenticate json body信息:
{ "apiVersion": "authentication.k8s.io/v1beta1", "kind": "TokenReview",
"spec": { "token": // 授权token
"(BEARERTOKEN)" } }
授权服务返回的json body信息:
{ "apiVersion": "authentication.k8s.io/v1beta1",
"kind": "TokenReview",
"status": {
"authenticated": true, // true 代表授权成功 false代表授权失败
"user": {
"username": "janedoe@example.com",
"uid": "42",
"groups": [
"developers",
"qa"
]}}
}
我们只需要启动一个http服务接受到api-server发送过来的token我们就能对这个token进行验证,比如说jwt验证。
api-server 配置授权
- 编辑api-server 授权配置文件
如果我们是用Kubeadm安装的kubenetes。
我们需要挂载如下json配置文件到api-server所在容器中。
{
"kind": "Config",
"apiVersion": "v1",
"preferences": {},
"clusters": [
{
"name": "github-authn",
"cluster": {
"server": "http://localhost:3000/authenticate" // 授权服务url
} }
],
"users": [
{
"name": "authn-apiserver",
"user": {
"token": "secret"
} }
],
"contexts": [
{
"name": "webhook",
"context": {
"cluster": "github-authn",
"user": "authn-apiserver"
} }
],
"current-context": "webhook" }
- 挂载api-server配置文件
编辑kube-apiserver.yaml文件
command指令增加:
--authentication-token-webhook-config-file=/etc/config/auth.json你的授权配置文件路径(容器内的路径)
volumeMounts(容器内路径)增加:
- mountPath: /etc/config
name: etc-config
readOnly: true
volumes(主机路径)增加:
- hostPath:
path: /etc/config # 你的授权配置文件所在路径
type: DirectoryOrCreate
name: etc-config # 此名字需要和上面mountPath下的name一模一样
- admin.conf 增加token
users:
- name: username
user:
token: <mytoken> # 生成的授权token。
4. 使用kubectl 指定用户名访问
kubectl get po --user username
此时api-server 就会拿着token 去访问我们的授权服务认证这个token是不是对的,对的就直接告诉api-server 这个token是有效的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程