Get Permanent Token When Accessing External Clusters¶
You can quickly integrate an external cluster into cluster management using kubeconfig. To ensure the stability of the integration, it is recommended to use a long-lasting token (preferably a permanent token) in the kubeconfig. However, different cluster service providers, such as AWS EKS and GKE, have different methods for getting a permanent token, typically only providing tokens with a validity of 24 hours.
Note
For specific product operation interface reference: Integrate Cluster
Create a ServiceAccount with Cluster Administrator Permissions¶
To address the above issue, you can create a ServiceAccount with cluster administrator permissions and use the kubeconfig of this ServiceAccount to access the cluster.
Warning
When executing the following steps, ensure that you have configured the AWS or GCP CLI and have permissions to access the cluster, otherwise an error will occur.
-
Create the YAML configuration for the ServiceAccount and ClusterRoleBinding:
cat >eks-admin-service-account.yaml <<EOF apiVersion: v1 kind: ServiceAccount metadata: name: eks-admin namespace: kube-system --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: eks-admin roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: eks-admin namespace: kube-system EOF
-
Apply the configuration using the following command:
Generate Secret for ServiceAccount¶
In Kubernetes versions 1.24 and above, creating a ServiceAccount does not automatically create a secret containing the CA certificate and user token; you need to associate it manually.
-
Create the YAML configuration for the Secret:
-
Find the secret corresponding to the ServiceAccount named eks-admin:
Retrieve ServiceAccount Token¶
-
View the details of the eks-admin-secret:
Now, you can see the token information. Check if there is an
exp
field; the value of this field is the expiration time of the token. If there is no such field, it is a permanent token.
Configure kubeconfig¶
-
Using the token, set up the kubeconfig:
-
Test locally to see if you can connect to the cluster:
Export and Use kubeconfig¶
Export the kubeconfig information:
Copy the exported content and add it to the cluster management to complete the integration of the cluster.
Note
- In cluster versions 1.24 and above, creating a ServiceAccount does not automatically create a secret containing the CA certificate and user token; you need to associate it manually. Refer to the official K8s documentation: Kubernetes ServiceAccount.
- You can use JWT tools to parse the token and check its expiration time; refer to jwt.io.