How to use kubeconfig to access to remote Kubernetes cluster

Install kubectl binary

The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
[root@localhost ~]# sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
[root@localhost ~]# ls -la /usr/local/bin/kubectl
[root@localhost ~]# kubectl version --output=yaml
clientVersion:
buildDate: "2022-09-21T14:33:49Z"
compiler: gc
gitCommit: 5835544ca568b757a8ecae5c153f317e5736700e
gitTreeState: clean
gitVersion: v1.25.2
goVersion: go1.19.1
major: "1"
minor: "25"
platform: linux/amd64
kustomizeVersion: v4.5.7

Use kubeconfig file to access remote Kubernetes cluster

A kubeconfig is a YAML file with all the Kubernetes cluster details, certificate, and secret token to authenticate the cluster. You might get this config file directly from the cluster administrator or from a cloud platform if you are using managed Kubernetes cluster. A kubeconfig file is a file to configure access to Kubernetes when to use with kubectl cli tool.

When to deploy Kubernetes cluster, a kubeconfig is automatically generated. It’s saved in ~/.kube/config drectory.

1
2
[root@host1 ~]#  ls -la ~/.kube/config
-rw------- 1 root root 5577 May 9 02:25 /root/.kube/config

You can access the Kubernetes cluster by providing the kubeconfig file remotely.

1
2
3
4
5
6
7
[root@localhost ~]# scp host1:/root/.kube/config ./kubeconfig-remote
[root@localhost ~]# kubectl --kubeconfig=./kubeconfig-remote get nodes
NAME STATUS ROLES AGE VERSION
host0 Ready <none> 135d v1.19.2
host1 Ready <none> 135d v1.19.2
host2 Ready <none> 135d v1.19.2
host3 Ready master 135d v1.19.2