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
相关推荐
hay_lee23 分钟前
Kubernetes StatefulSet:OrderedReady 极简指南
云原生·容器·kubernetes
晚风吹长发36 分钟前
Docker使用——Docker容器及相关命令
linux·运维·服务器·docker·容器·架构
ShiXZ2131 小时前
Docker Compose 安装与配置指南
运维·docker·容器
BullSmall2 小时前
Anolis OS 8.10 Docker 部署 SonarQube 9.9 完整教程
运维·docker·容器
人间凡尔赛2 小时前
AI-Native 云原生架构:2026 年从容器编排到智能体编排的范式革命
后端·云原生·架构
成为你的宁宁2 小时前
【K8s HPA 水平扩缩容】
kubernetes·hpa
欢醉3 小时前
k8s调度容器失败The node had condition: [DiskPressure]
kubernetes·springcloud
Linux-187413 小时前
分布式链路追踪系统之docker-compose安装skywalking
云原生·docker-compose·skywalking·分布式链路追踪系统·应用程序性能监控
生活爱好者!18 小时前
我把NAS当作下载机,docker一键部署qb
运维·docker·容器
隔窗听雨眠18 小时前
Spring Boot在云原生时代的编程范式革新研究
spring boot·后端·云原生