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
相关推荐
与海boy20 分钟前
docker compose minio
docker·容器·eureka
星辰徐哥42 分钟前
云原生核心特性:容器化、微服务与DevOps的通俗解读
微服务·云原生·devops
武子康1 小时前
调查研究-167 Docker Compose 详解:从单容器到多服务编排的工程化入口
运维·docker·云原生·容器·kubernetes·k8s·docker-compose
heimeiyingwang2 小时前
【架构实战】分布式会话:从Session到JWT的演进
微服务·云原生·架构
旅僧2 小时前
Ubantu docker环境配置(前置)
运维·docker·容器
正经教主4 小时前
【docker基础】第六课:Web应用与数据库容器部署
网络·docker·容器
Shacoray4 小时前
K8s 中 Ingress 的 HTTPS 证书 如何生成?
容器·https·kubernetes
开发者联盟league4 小时前
使用Jenkins整合Sonarqube/Gitlab/Harbor/Kubernetes的Demo工程
kubernetes·gitlab·jenkins
Patrick_Wilson4 小时前
Node.js SSR 内存治理:为什么 --max-old-space-size 不等于进程内存
kubernetes·node.js·v8