2017-08-29 00:19:25 +03:00
|
|
|
# Configuring kubectl for Remote Access
|
|
|
|
|
2025-06-03 18:40:47 +03:00
|
|
|
In this lab you will generate a kubeconfig file for the `kubectl` command line
|
|
|
|
utility based on the `admin` user credentials.
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
> Run the commands in this lab from the `jumpbox` machine.
|
2017-08-29 00:19:25 +03:00
|
|
|
|
|
|
|
## The Admin Kubernetes Configuration File
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
Each kubeconfig requires a Kubernetes API Server to connect to.
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2025-06-03 18:40:47 +03:00
|
|
|
You should be able to ping `controlplane.kubernetes.local` based on the
|
|
|
|
`/etc/hosts` DNS entry from a previous lab.
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2025-04-08 17:30:28 +03:00
|
|
|
curl --cacert ca.crt \
|
2025-06-02 05:59:11 +03:00
|
|
|
https://controlplane.kubernetes.local:6443/version
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
2023-11-01 09:16:49 +03:00
|
|
|
|
|
|
|
```text
|
2018-05-12 19:54:18 +03:00
|
|
|
{
|
2023-11-01 09:16:49 +03:00
|
|
|
"major": "1",
|
2025-04-07 04:32:30 +03:00
|
|
|
"minor": "32",
|
2025-06-03 01:15:47 +03:00
|
|
|
"gitVersion": "v1.33.1",
|
2025-04-07 04:32:30 +03:00
|
|
|
"gitCommit": "32cc146f75aad04beaaa245a7157eb35063a9f99",
|
2023-11-01 09:16:49 +03:00
|
|
|
"gitTreeState": "clean",
|
2025-04-07 04:32:30 +03:00
|
|
|
"buildDate": "2025-03-11T19:52:21Z",
|
|
|
|
"goVersion": "go1.23.6",
|
2023-11-01 09:16:49 +03:00
|
|
|
"compiler": "gc",
|
|
|
|
"platform": "linux/arm64"
|
|
|
|
}
|
|
|
|
```
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
Generate a kubeconfig file suitable for authenticating as the `admin` user:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
{
|
2018-05-12 19:54:18 +03:00
|
|
|
kubectl config set-cluster kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--certificate-authority=ca.crt \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
2025-06-02 05:59:11 +03:00
|
|
|
--server=https://controlplane.kubernetes.local:6443
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2018-05-12 19:54:18 +03:00
|
|
|
kubectl config set-credentials admin \
|
2023-11-01 09:16:49 +03:00
|
|
|
--client-certificate=admin.crt \
|
|
|
|
--client-key=admin.key
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2018-05-12 19:54:18 +03:00
|
|
|
kubectl config set-context kubernetes-the-hard-way \
|
|
|
|
--cluster=kubernetes-the-hard-way \
|
|
|
|
--user=admin
|
|
|
|
|
|
|
|
kubectl config use-context kubernetes-the-hard-way
|
|
|
|
}
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
2025-06-03 18:40:47 +03:00
|
|
|
The results of running the command above should create a kubeconfig file in
|
|
|
|
the default location `~/.kube/config` used by the `kubectl` commandline tool.
|
|
|
|
This also means you can run the `kubectl` command without specifying a config.
|
2023-11-01 09:16:49 +03:00
|
|
|
|
2017-08-29 00:19:25 +03:00
|
|
|
|
|
|
|
## Verification
|
|
|
|
|
2021-05-02 08:33:46 +03:00
|
|
|
Check the version of the remote Kubernetes cluster:
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2021-05-02 08:33:46 +03:00
|
|
|
kubectl version
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```text
|
2025-06-03 01:15:47 +03:00
|
|
|
Client Version: v1.33.1
|
2025-06-03 18:40:47 +03:00
|
|
|
Kustomize Version: v5.6.0
|
2025-06-03 01:15:47 +03:00
|
|
|
Server Version: v1.33.1
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
List the nodes in the remote Kubernetes cluster:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2017-08-29 00:19:25 +03:00
|
|
|
kubectl get nodes
|
|
|
|
```
|
|
|
|
|
|
|
|
```
|
2025-06-03 18:40:47 +03:00
|
|
|
NAME STATUS ROLES AGE VERSION
|
|
|
|
node01 Ready <none> 15m v1.33.1
|
|
|
|
node02 Ready <none> 15m v1.33.1
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
Next: [Provisioning Pod Network Routes](11-pod-network-routes.md)
|