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是有效的。
浙公网安备 33010602011771号