Guides - Manage a Cluster with kubectl
A managed Kubernetes service that enables you to easily control and scale your application’s infrastructure.
Install kubectl
You need to install the kubectl client to your computer before proceeding. Follow the steps corresponding to your computer’s operating system.
macOS:
Install via Homebrew:
brew install kubectl
If you don’t have Homebrew installed, visit the Homebrew home page for instructions. Alternatively, you can manually install the binary; visit the Kubernetes documentation for instructions.
Linux:
Download the latest kubectl release:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Make the downloaded file executable:
chmod +x ./kubectl
Move the command into your PATH:
sudo mv ./kubectl /usr/local/bin/kubectl
Windows:
Visit the Kubernetes documentation for a link to the most recent Windows release.
Connect to a Cluster with kubectl
After you’ve created your LKE cluster using the Cloud Manager, you can begin interacting with and managing your cluster. You connect to it using the kubectl client on your computer. To configure kubectl, download your cluster’s kubeconfig file.
Access and Download your kubeconfig
Anytime after your cluster is created you can download its kubeconfig. The kubeconfig is a YAML file that will allow you to use kubectl to communicate with your cluster. Here is an example kubeconfig file:
- File: example-cluster-kubeconfig.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
apiVersion: v1 kind: Config preferences: {} clusters: - cluster: certificate-authority-data: LS0tLS1CRUd... server: https://example.us-central.linodelke.net:443 name: lke1234 users: - name: lke1234-admin user: as-user-extra: {} token: LS0tLS1CRUd... contexts: - context: cluster: lke1234 namespace: default user: lke1234-admin name: lke1234-ctx current-context: lke1234-ctx
This configuration file defines your cluster, users, and contexts.
To access your cluster’s kubeconfig, log in to your Cloud Manager account and navigate to the Kubernetes section.
From the Kubernetes listing page, click on your cluster’s more options ellipsis and select Download kubeconfig. The file will be saved to your computer’s
Downloads
folder.Download and view your Kubeconfig from the cluster's details page You can also download the kubeconfig from the Kubernetes cluster’s details page.
When viewing the Kubernetes listing page, click on the cluster for which you’d like to download a kubeconfig file.
On the cluster’s details page, under the kubeconfig section, click the Download icon. The file will be saved to your
Downloads
folder.To view the contents of your kubeconfig file, click on the View icon. A pane will appear with the contents of your cluster’s kubeconfig file.
To improve security, change the
kubeconfig.yaml
file permissions to be only accessible by the current user:chmod go-r /Downloads/kubeconfig.yaml
Open a terminal shell and save your kubeconfig file’s path to the
$KUBECONFIG
environment variable. In the example command, the kubeconfig file is located in theDownloads
folder, but you should alter this line with this folder’s location on your computer:export KUBECONFIG=~/Downloads/kubeconfig.yaml
Note It is common practice to store your kubeconfig files in~/.kube
directory. By default, kubectl will search for a kubeconfig file namedconfig
that is located in the~/.kube
directory. You can specify other kubeconfig files by setting the$KUBECONFIG
environment variable, as done in the step above.View your cluster’s nodes using kubectl.
kubectl get nodes
Note If your kubectl commands are not returning the resources and information you expect, then your client may be assigned to the wrong cluster context. Visit our Troubleshooting Kubernetes guide to learn how to switch cluster contexts.
You are now ready to manage your cluster using kubectl. For more information about using kubectl, see Kubernetes’ Overview of kubectl guide.
Persist the Kubeconfig Context
If you create a new terminal window, it does not have access to the context that you specified using the previous instructions. This context information can be made persistent between new terminals by setting the KUBECONFIG
environment variable in your shell’s configuration file.
These instructions persist the context for users of the Bash terminal. They are similar for users of other terminals:
Navigate to the
$HOME/.kube
directory:cd $HOME/.kube
Create a directory called
configs
within$HOME/.kube
. You can use this directory to store your kubeconfig files.mkdir configs
Copy your
kubeconfig.yaml
file to the$HOME/.kube/configs
directory.cp ~/Downloads/kubeconfig.yaml $HOME/.kube/configs/kubeconfig.yaml
Note Alter the above line with the location of the Downloads folder on your computer.
Optionally, you can give the copied file a different name to help distinguish it from other files in the
configs
directory.Open up your Bash profile (e.g.
~/.bash_profile
) in the text editor of your choice and add your configuration file to the$KUBECONFIG
PATH variable.If an
export KUBECONFIG
line is already present in the file, append to the end of this line as follows; if it is not present, add this line to the end of your file:export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config:$HOME/.kube/configs/kubeconfig.yaml
Close your terminal window and open a new window to receive the changes to the
$KUBECONFIG
variable.Use the
config get-contexts
command forkubectl
to view the available cluster contexts:kubectl config get-contexts
You should see output similar to the following:
CURRENT NAME CLUSTER AUTHINFO NAMESPACE * lke1234-ctx lke1234 lke1234-admin default
If your context is not already selected, (denoted by an asterisk in the
current
column), switch to this context using theconfig use-context
command. Supply the full name of the cluster (including the authorized user and the cluster):kubectl config use-context lke1234-ctx
You should see output like the following:
Switched to context "lke1234-ctx".
You are now ready to interact with your cluster using
kubectl
. You can test the ability to interact with the cluster by retrieving a list of Pods. Use theget pods
command with the-A
flag to see all pods running across all namespaces:kubectl get pods -A
You should see output like the following:
NAMESPACE NAME READY STATUS RESTARTS AGE kube-system calico-kube-controllers-dc6cb64cb-4gqf4 1/1 Running 0 11d kube-system calico-node-bx2bj 1/1 Running 0 11d kube-system calico-node-fg29m 1/1 Running 0 11d kube-system calico-node-qvvxj 1/1 Running 0 11d kube-system calico-node-xzvpr 1/1 Running 0 11d kube-system coredns-6955765f44-r8b79 1/1 Running 0 11d kube-system coredns-6955765f44-xr5wb 1/1 Running 0 11d kube-system csi-linode-controller-0 3/3 Running 0 11d kube-system csi-linode-node-75lts 2/2 Running 0 11d kube-system csi-linode-node-9qbbh 2/2 Running 0 11d kube-system csi-linode-node-d7bvc 2/2 Running 0 11d kube-system csi-linode-node-h4r6b 2/2 Running 0 11d kube-system kube-proxy-7nk8t 1/1 Running 0 11d kube-system kube-proxy-cq6jk 1/1 Running 0 11d kube-system kube-proxy-gz4dc 1/1 Running 0 11d kube-system kube-proxy-qcjg9 1/1 Running 0 11d
This page was originally published on