当前位置: 首页 > article >正文

java 调用 k8s 的 apiserver

创建serviceaccount

serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: myadmin
  namespace: default

创建集群角色ClusterRole

clusterrole.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: my-clusterrole
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["*"]

创建集群角色绑定ClusterRoleBinding

clusterrolebinding.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: my-clusterrolebinding
subjects:
# 此集群角色绑定允许 "my-group-manager" 组中的任何人访问任何名字空间中的 Secret 资源
- kind: Group
  name: my-group-manager
  apiGroup: rbac.authorization.k8s.io
- kind: ServiceAccount
  name: myadmin
  namespace: default
roleRef:
  kind: ClusterRole
  name: my-clusterrole
  apiGroup: rbac.authorization.k8s.io

获取apiserver的地址

https://cloud.tencent.com/developer/article/2242229

kubectl cluster-info

获取tonken:https://www.cnblogs.com/lori/p/18380527

kubectl proxy --port=8001
curl 'http://127.0.0.1:8001/api/v1/namespaces/default/serviceaccounts/myadmin/token' \
  -H "Content-Type:application/json" -X POST -d '{}'

default为namespace名;myadmin为serviceaccount名

或者

kubectl -n default create token myadmin

创建永久token

apiVersion: v1
kind: Secret
metadata:
  name: admin-token
  namespace: default
  annotations:
    kubernetes.io/service-account.name: "myadmin"   
type: kubernetes.io/service-account-token  
kubectl get secret admin-token -n default -o jsonpath={".data.token"} | base64 -d
curl -H "Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ4Slh0Mk9lMzd0TXNlZW9sbGRRMUVfRWtYSHVnNnFwMG11TmhYR3dWM2cifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNzI5MTM4NzExLCJpYXQiOjE3MjkxMzUxMTEsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJkZWZhdWx0Iiwic2VydmljZWFjY291bnQiOnsibmFtZSI6Im15YWRtaW4iLCJ1aWQiOiIxNzQ2YzY1Yy00ZjVlLTQ5ODMtYWJmOS1kZjM5NzkzNGI0ZTgifX0sIm5iZiI6MTcyOTEzNTExMSwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmRlZmF1bHQ6bXlhZG1pbiJ9.Se3DvijQSXeP8tfJ6bdp_97TKvD1FSS3NkRcU5IO6ZcXhuodhPCfR7X9k6oDjwNYXuhW4v3rIYLSR5C79K_lwVWWKFxNAfkMXgl_gGu0CeIJdMOaO2vTOHAc8vqr1SWmw9NhX6oIqPfgDCCVBKtpEog43VbPKRniDVGEn2MAbrXf6qb0uncIYfzk9hJ4zo14-fM_6VPaW-PmKl5PgbfsZFY9B8nQW9G4ivEjQLQMV3VLB5shdf8JDVJYJL_EQT9skO8fmLomCnF5s02XMDAzmtILGGhummLk0prIqbLUyNUOCfkuUKAwatse51Qup7itFPiATzmRcmsGE5DhnSevzQ" https://10.0.2.11:6443/api/v1/namespaces/kube-system/services -k

-k忽略https的证书校验

参考

https://www.cnblogs.com/niuben/p/18408731
https://www.cnblogs.com/lori/p/18380527
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/authentication/
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/
https://blog.csdn.net/fly910905/article/details/101345091

http://www.kler.cn/a/355060.html

相关文章:

  • 大语言模型中的Agent;常见的Agent开发工具或框架
  • CH32V307VCT6---工程template创建
  • 《Cocos Creator游戏实战》非固定摇杆实现原理
  • 基于STM32F103控制L298N驱动两相四线步进电机
  • Datawhale AI 冬令营学习笔记-零编程基础制作井字棋小游戏
  • WWW23-多行为级联|级联图卷积网络的多行为推荐
  • 公开选拔!产业实践教授
  • RHCE--at,crontab例行性工作
  • 滚雪球学Redis[5.3讲]:Redis持久化优化深度解析:RDB与AOF的策略选择与实践
  • Unity3D 框架如何实现道路引导 UV 动画详解
  • 如何优化API以提高数据获取的准确性?
  • 从MySQL到OceanBase离线数据迁移的实践
  • 鸿蒙跨设备协同开发06——应用接续
  • SpringCloud Gateway 网关路由全自动实现方案
  • MongoDB未授权访问
  • 《Spring Boot 应用开发研究》
  • 【OSCP Proving Grounds 靶场系列】Slort
  • oracle查询数据库占用大小
  • VTK的学习方法-第一类型应用
  • 后端——eclipse实现前端后端的交互(1)
  • SpringCloud学习记录|day5
  • 常用的字符集(ASCII、GBK)
  • 速卖通关键字搜索接口技术解析及Python代码示例
  • leetcode哈希表(三)-两数之和
  • Nordic 学习小记录
  • R语言:ERGM指数随机图模型