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 配置授权

  1. 编辑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" }
  1. 挂载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一模一样
  1. admin.conf 增加token
users:
  - name: username
  user:
  token: <mytoken> #  生成的授权token。

4. 使用kubectl 指定用户名访问

kubectl get po --user username

此时api-server 就会拿着token 去访问我们的授权服务认证这个token是不是对的,对的就直接告诉api-server 这个token是有效的。

posted on   biwentao  阅读(198)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示