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
相关推荐
FoldWinCard10 小时前
基于k8s高可用web集群
前端·容器·kubernetes
cui_hao_nan11 小时前
Docker学习3
docker·容器
玉&心11 小时前
在Docker中部署前端和后端的实现
运维·docker·容器
wdfk_prog11 小时前
Docker 常用语法与命令:CLI、Dockerfile 与 Compose 速查
运维·docker·容器
无敌糖果11 小时前
K8S network namespace错误分析
docker·容器·kubernetes
郝开12 小时前
Docker Compose 模块化多环境配置规范指南:示例搭建Kibana 9.4.2
docker·容器·kibana
运维老郭12 小时前
Prometheus 监控 K8s 从入门到入坑:工作原理 + 部署实操 + 避坑指南
云原生·容器·kubernetes
AAA@峥12 小时前
从零搭建 Prometheus 完整监控告警体系|Linux 部署 + node_exporter+Grafana 可视化
云原生·grafana·prometheus
虎王物联13 小时前
Docker BuildKit多阶段构建:IoT固件交叉编译流水线实战
运维·物联网·ci/cd·docker·容器·物联网嵌入式
10mAh14 小时前
【Docker】磁盘空间被占满怎么清理?——overlay2、容器日志与 Build Cache 排查实战
docker·容器·eureka