落烨无痕

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  37 随笔 :: 0 文章 :: 5 评论 :: 12万 阅读

一、前置条件和说明:

1.已安装k3s,并完成kubeconfig配置

2.k8s1.24之后的版本,创建service account时,不会自动创建secret,需要手工创建secret

 

二、步骤

1.创建service account(sa.yaml)

apiVersion: v1
kind: ServiceAccount
metadata:
  name: apiserver-sa
  namespace: kube-system
kubectl apply -f sa.yaml

 

2.创建高权限的cluster role(clusterrole.yaml)

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-admin
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["*"]
kubectl apply -f clusterrole.yaml

 

3.service account与cluster role进行绑定(clusterrolebinding.yaml)

复制代码
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: api-admin-bind
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: apiserver-sa
  namespace: kube-system
复制代码
kubectl apply -f clusterrolebinding.yaml

 

4.手工创建secret(secret.yaml,token会自动分配),k8s1.24之前的版本不需要此步骤

apiVersion: v1
kind: Secret
metadata:
  name: apiserver-token-secret  
  namespace: kube-system
  annotations:
    kubernetes.io/service-account.name: "apiserver-sa"  #对应步骤1中创建的service name
type: kubernetes.io/service-account-token
kubectl apply -f secret.yaml

 

5.secret中的token是base64编码的,解码后保存到token.txt,供后续使用

kubectl -n kube-system get secrets apiserver-token-secret -o jsonpath='{.data.token}'|base64 -d > token.txt

 

6.通过curl访问apiserver

curl -H "Authorization: Bearer $(cat token.txt)" -k https://127.0.0.1:6443/api/v1/namespaces/kube-system

 

7.其他

确定apiserver地址:
127.0.0.1:6443为apiserver地址。可以通过kubectl查看具体环境的apiserver地址
kubectl cluster-info

 

8.常用apiserver uri

复制代码
/apis/apps/v1/namespaces/danny/deployments       # namespace=danny下的deployment列表
/apis/apps/v1/namespaces/danny/statefulsets      # namespace=danny下的statefulset列表
/apis/apps/v1/namespaces/danny/daemonsets        # namespace=danny下的daemonsets列表
/api/v1/namespaces/danny/services                # namespace=danny下的service列表
/api/v1/namespaces/danny/pods                    # namespace=danny下的pod列表
/api/v1/namespaces/danny/configmaps              # namespace=danny下的configmap列表
/api/v1/namespaces/danny/secrets                 # namespace=danny下的secret列表
/api/v1/namespaces/danny/serviceaccounts         # namespace=danny下的serviceaccount列表
/api/v1/namespaces/danny/persistentvolumeclaims  # namespace=danny下的pvc列表
/api/v1/persistentvolumes                        # pv列表
/api/v1/nodes                                    # node列表
/apis/networking.k8s.io/v1/namespaces/danny/ingresses   #namespace=danny下的ingress列表
复制代码

更多uri可查看官网:

posted on   落烨无痕  阅读(86)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示