阿里云k8s如何创建可用的api token

阿里云的 Kubernetes 配置文件(如您所提供的 YAML 格式文件)通常不会直接包含用于连接 Kubernetes 集群的令牌。而是包含了客户端证书和私钥数据,这些是用于通过证书验证而不是令牌验证的方式来与 Kubernetes API 服务器进行安全交互的。

1.创建一个 ServiceAccount:

kubectl create serviceaccount [service-account-name]

2.通过 YAML 文件创建 ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: example-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: example-serviceaccount
  namespace: example-namespace
roleRef:
  kind: ClusterRole
  name: example-clusterrole
  apiGroup: rbac.authorization.k8s.io

3.手动创建服务账号令牌

apiVersion: v1
kind: Secret
metadata:
  name: [secret-name]
  namespace: [your-namespace]
  annotations:
    kubernetes.io/service-account.name: [service-account-name]
type: kubernetes.io/service-account-token

4.定义pod-exec-role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: your-namespace
  name: pod-exec-role
rules:
- apiGroups: [""]
  resources: ["pods/exec", "pods/log"]
  verbs: ["create", "get", "list"]

5.创建ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: pod-exec-binding
  namespace: your-namespace
subjects:
- kind: User
  name: "your-username"
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-exec-role
  apiGroup: rbac.authorization.k8s.io

6.创建view的roleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cao-crb
subjects:
- kind: User
  name: "your-username"  # 替换为实际的用户名
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: view  # 确保这是正确的 ClusterRole 名称
  apiGroup: rbac.authorization.k8s.io

阿里云Kubernetes集群默认不开启Kubernetes Dashboard,您需要手动开启,以下是在阿里云Kubernetes v1.23.0中使用Web界面的步骤: 1. 安装Kubernetes Dashboard。 ```bash kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.1/aio/deploy/recommended.yaml ``` 2. 创建Kubernetes Dashboard Service Account和ClusterRoleBinding。 ```bash kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.1/aio/deploy/recommended.yaml ``` 3. 修改Kubernetes Dashboard Service Type为NodePort。 ```bash kubectl -n kubernetes-dashboard edit service kubernetes-dashboard ``` 将Service Type修改为NodePort,并添加NodePort端口号,如: ```yaml apiVersion: v1 kind: Service metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"k8s-app":"kubernetes-dashboard"},"name":"kubernetes-dashboard","namespace":"kubernetes-dashboard"},"spec":{"ports":[{"port":443,"targetPort":8443}],"selector":{"k8s-app":"kubernetes-dashboard"}}} creationTimestamp: "2022-01-04T05:20:19Z" labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kubernetes-dashboard resourceVersion: "14272" selfLink: /api/v1/namespaces/kubernetes-dashboard/services/kubernetes-dashboard uid: 6d4d7a0d-e270-4a98-9b0f-4f0b4d6f2eb0 spec: clusterIP: 172.20.33.97 externalTrafficPolicy: Cluster ports: - nodePort: 32000 # 添加NodePort端口号 port: 443 protocol: TCP targetPort: 8443 selector: k8s-app: kubernetes-dashboard sessionAffinity: None type: NodePort # 修改Service Type为NodePort status: loadBalancer: {} ``` 4. 获取Kubernetes Dashboard访问链接。 ```bash kubectl -n kubernetes-dashboard get service kubernetes-dashboard ``` 将输出信息中的NodePort端口号记录下来,然后使用以下URL访问Kubernetes Web界面: ```bash https://<master-ip>:<nodeport> ``` 其中,`<master-ip>`为您的Kubernetes集群Master节点IP,`<nodeport>`为上一步记录的NodePort端口号。 您将看到一个登录页面,选择“Token”身份验证并输入以下命令以获取令牌: ```bash kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep service-controller-token | awk '{print $1}') ``` 将令牌复制到登录页面中并点击“登录”按钮即可访问Kubernetes Web界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值