This section uses a CCE cluster as an example to describe how to connect to a CCE cluster using kubectl.
When you access a cluster using kubectl, CCE uses the kubeconfig.json file generated on the cluster for authentication. This file contains user information, based on which CCE determines which Kubernetes resources can be accessed by kubectl. The permissions recorded in a kubeconfig.json file vary from user to user.
For details about user permissions, see Cluster Permissions (IAM-based) and Namespace Permissions (Kubernetes RBAC-based).
To connect to a Kubernetes cluster from a PC, you can use kubectl, a Kubernetes command line tool. You can log in to the CCE console, click the name of the cluster to be connected, and view the access address and kubectl connection procedure on the cluster details page.
To bind a public IP (EIP) to the cluster, go to the cluster details page and click Bind next to EIP in the Connection Information pane. In a cluster with an EIP bound, kube-apiserver will be exposed to public networks and may be attacked. You are advised to configure Advanced Anti-DDoS (AAD) for the EIP of the node where kube-apiserver resides.
Download kubectl and the configuration file. Copy the file to your client, and configure kubectl. After the configuration is complete, you can access your Kubernetes clusters. Procedure:
Prepare a computer that can access the public network and install kubectl in CLI mode. You can run the kubectl version command to check whether kubectl has been installed. If kubectl has been installed, skip this step.
This section uses the Linux environment as an example to describe how to install and configure kubectl. For details, see Installing kubectl.
cd /home curl -LO https://dl.k8s.io/release/{v1.25.0}/bin/linux/amd64/kubectl
{v1.25.0} specifies the version number. Replace it as required.
chmod +x kubectl mv -f kubectl /usr/local/bin
On the Connection Information pane on the cluster details page, click Learn more next to kubectl. On the window displayed, download the configuration file.
cd /home mkdir -p $HOME/.kube mv -f kubeconfig.json $HOME/.kube/config
kubectl config use-context internal
kubectl config use-context external
kubectl config use-context externalTLSVerify
For details about the cluster two-way authentication, see Two-Way Authentication for Domain Names.
Currently, CCE supports two-way authentication for domain names.
When you use kubectl to create or query Kubernetes resources, the following output is returned:
# kubectl get deploy Error from server (Forbidden): deployments.apps is forbidden: User "0c97ac3cb280f4d91fa7c0096739e1f8" cannot list resource "deployments" in API group "apps" in the namespace "default"
The cause is that the user does not have the permissions to operate the Kubernetes resources. For details about how to assign permissions, see Namespace Permissions (Kubernetes RBAC-based).
When you use kubectl to create or query Kubernetes resources, the following output is returned:
The connection to the server localhost:8080 was refused - did you specify the right host or port?
The cause is that cluster authentication is not configured for the kubectl client. For details, see 3.