DockerDesktop使用自带的k8s v1.24.0 使用kubectl获取不到token的问题

DockerDesktop使用自带的k8s v1.24.0 使用kubectl获取不到token的问题

1、前言

最近Docker Desktop发布了新版本,我习惯性的点了更新,更新完成发现DockerDesktop自带的k8s版本也更新到了v1.24.0,然后发现我使用kubectl命令获取不到了token。最开始我以为是自己创建用户出现了错误,反复验证发现创建的用户没有问题,然后我开始怀疑是不是因为版本更新删除或修改一些命令。我去百度上搜索找到关于dockerDeskto最新的信息是5月份v1.22.0的信息,v1.22.0版本还是支持使用kubectl命令获取token的。无奈我只好FQ去谷歌碰碰运气看看是否能找到相关的资料,幸运的是我找到一个名为Kubernetes 1.24 – What’s new? 文档,这份文档完整的记录了k8s v1.24.0版本所有变更信息,我在里面找到关于auth的变更内容如下:

#2799 Reduction of Secret-based Service Account Tokens

Stage: Graduating to Beta

Feature group: auth
Feature gate: LegacyServiceAccountTokenNoAutoGeneration Default value: true

Now that the TokenRequest API has been stable since Kubernetes 1.22, it is time to do some cleaning and promote the use of this API over the old tokens.

Up until now, Kubernetes automatically created a service account Secret when creating a Pod. That token Secret contained the credentials for accessing the API.

Now, API credentials are obtained directly through the TokenRequest API, and are mounted into Pods using a projected volume. Also, these tokens will be automatically invalidated when their associated Pod is deleted. You can still create token secrets manually if you need it.

The features to track and clean up existing tokens will be added in future versions of Kubernetes.

原文地址:https://sysdig.com/blog/kubernetes-1-24-whats-new/

大致的意思是从v1.22.0版本就推出了TokenRequest API,现在v1.24.0版本 TokenRequest API已经稳定了是时候推广使用了而不在使用旧的令牌。下面我们开始使用TokenRequest API获取token。

2、使用yaml配置创建用户和绑定角色

# 创建用户duzhaosongyue
cat <<EOF > dashboard-duzhaosongyue.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: duzhaosongyue
  namespace: kube-system
EOF

# 应用创建用户配置
kubectl apply -f dashboard-duzhaosongyue.yaml

# 绑定角色到用户
cat <<EOF > dashboard-ClusterRoleBinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: duzhaosongyue
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: duzhaosongyue
  namespace: kube-system
EOF

# 应用绑定角色
kubectl apply -f dashboard-ClusterRoleBinding.yaml

3、使用TokenRequest API获取token

3.1 开启代理。

Kubernetes API 的端口是6443 但是这个是Kubernetes内部ip的端口,我们本地是无法访问的,所以需要使用代理:

# 使用kubectl 开启代理并且设置代理端口为8001
kubectl proxy --port=8001
# 检查是否开启成功
curl http://localhost:8001/api/

# 成功会返回
{
  "kind": "APIVersions",
  "versions": [
    "v1"
  ],
  "serverAddressByClientCIDRs": [
    {
      "clientCIDR": "0.0.0.0/0",
      "serverAddress": "192.168.65.4:6443"
    }
  ]
}

3.2 检查用户是否存在

# 根据工作空间获取所有serviceaccount信息的接口
# GET /api/v1/namespaces/{namespace}/serviceaccounts   
# 根据工作空间获取单个serviceaccount信息的接口
# GET /api/v1/namespaces/{namespace}/serviceaccounts/{name} 
# 获取所有的serviceaccount信息 不分工作空间
# GET /api/v1/serviceaccounts 获取所有

# 获取kube-system工作空间下名为duzhaosongyue的serviceaccount信息
curl 127.0.0.1:8001/api/v1/namespaces/kube-system/serviceaccounts/duzhaosongyue

3.3 获取token

# 获取token接口地址
# POST /api/v1/namespaces/{namespace}/serviceaccounts/{name}/token
# 获取token带有参数
curl 'http://127.0.0.1:8001/api/v1/namespaces/kube-system/serviceaccounts/duzhaosongyue/token' -H "Content-Type:application/json" -X POST -d '{"kind":"TokenRequest","apiVersion":"authentication.k8s.io/v1","metadata":{"name":"duzhaosongyue","namespace":"kube-system"},"spec":{"audiences":["https://kubernetes.default.svc.cluster.local"],"expirationSeconds":7600}}'

# 获取token不带参数
curl 'http://127.0.0.1:8001/api/v1/namespaces/kube-system/serviceaccounts/duzhaosongyue/token' -H "Content-Type:application/json" -X POST -d '{}'

官方文档地址:https://kubernetes.io/docs/reference/kubernetes-api/authentication-resources/token-request-v1/#TokenRequest

4、其他

4.1 k8s默认端口文档地址

官方文档地址:https://kubernetes.io/docs/reference/ports-and-protocols/

4.2 设置代理文档地址

官方文档地址:https://kubernetes.io/docs/tasks/extend-kubernetes/http-proxy-access-api/

posted @ 2022-06-06 16:34  时光城主  阅读(2799)  评论(0编辑  收藏  举报