k8s 创建token

方法一,使用kubectl直接创建serviceaccount

1.创建serviceaccount

bash 复制代码
kubectl create serviceaccount my-serviceaccount -n test-deploy-ns-20260113171918
  1. 绑定角色
bash 复制代码
kubectl create clusterrolebinding my-clusterrolebinding --clusterrole=cluster-admin --serviceaccount=test-deploy-ns-20260113171918:my-serviceaccount
  1. 创建Secret
yaml 复制代码
# sa-token.yaml
apiVersion: v1
kind: Secret
metadata:
  name: my-user-token
  namespace: test-deploy-ns-20260113171918
  annotations:
    kubernetes.io/service-account.name: my-serviceaccount
type: kubernetes.io/service-account-token
bash 复制代码
kubectl apply -f sa-token.yaml
  1. 获取token
bash 复制代码
kubectl get secret my-user-token -n test-deploy-ns-20260113171918 -o jsonpath='{.data.token}' | base64 --decode

或者使用describe

bash 复制代码
kubectl describe secret my-user-token

方法二,使用yaml创建

  • 创建serviceaccount my-serviceaccount.yaml
yaml 复制代码
apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-serviceaccount
  namespace: default
  • 创建ClusterRoleBinding my-clusterrolebinding.yaml
yaml 复制代码
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: my-clusterrolebinding
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: my-serviceaccount
  namespace: default
  • 创建secret my-user-token.yaml
yaml 复制代码
apiVersion: v1
kind: Secret
metadata:
  name: my-user-token
  namespace: default
  annotations:
    kubernetes.io/service-account.name: my-serviceaccount
type: kubernetes.io/service-account-token
  • apply上述三个yaml
bash 复制代码
kubectl apply -f my-serviceaccount.yaml
kubectl apply -f my-clusterrolebinding.yaml
kubectl apply -f my-user-token.yaml
相关推荐
阿里云云原生2 天前
阿里云获评 Agentic AI 开发平台领导者,函数计算 AgentRun 赢下关键分!
云原生
蝎子莱莱爱打怪2 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀2 天前
Docker部署Nacos
docker·容器
缓解AI焦虑2 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
阿里云云原生2 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生2 天前
当 AI Agent 接管手机:移动端如何进行观测
云原生·agent
阿里云云原生2 天前
AI 原生应用开源开发者沙龙·深圳站精彩回顾 & PPT下载
云原生
阿里云云原生2 天前
灵感启发:日产文章 100 篇,打造“实时热点洞察”引擎
云原生
1candobetter2 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
~莫子2 天前
Haproxy七层负载详解+实验详细代码
云原生