To manage a K3s Kubernetes cluster through kubectl is recommended not to do it directly from the server nodes, but to connect remotely from another computer. This way, you won't have to copy your .yaml files describing your deployments or configurations directly on any of your server nodes.
In this guide I'll assume that you want to access remotely your K3s cluster from a Debian-based Linux computer. This guide will use the curl command, which may not come installed in your Linux distribution. In a Debian system, install the curl package as follows:
$ sudo apt install -y curlThe first thing you must know is the version of the K3s cluster you're going to connect to. This is important because kubectl is guaranteed to be compatible only with its own correlative version or those that are at one minor version of difference from it. For instance, at the time of writing this guide, the latest kubectl minor version is 1.22, meaning that it's compatible with the 1.21, 1.22 and future 1.23 versions of the Kubernetes api. K3s follows the same versioning system, since it's "just" a particular distribution of Kubernetes.
To check the version of your K3s cluster, connect to your k3sserver01 serve node and execute the following k3s command.
$ sudo k3s --version
k3s version v1.22.3+k3s1 (61a2aab2)
go version go1.16.8The line k3s version is the one you have to pay attention to and, in particular, read the v1.22.3 part. This K3s server node is running Kubernetes version 1.22.3, and you can connect to it with the latest 1.22 version you can get of the kubectl command.
To know which is the latest stable release of Kubernetes, check this stable.txt file online. It just contains a version string which, at the time of writing this, is v1.22.4.
Now you know the version of kubectl you have to put in your client system, but first you must prepare it to install the command. First of all, don't install kubectl with a software manager like apt or yum. This is to avoid that a regular update changes your version of the command to an uncompatible one with your cluster, or not being able to upgrade kubectl because some reason of other. So, better make a manual, non system-wide installation of kubectl in your client computer.
Said that, let's proceed with this manual installation of kubectl.
-
Get into your client system as your preferred user and open a shell terminal in it. Then, remaining in the $HOME directory of your user, execute the following
mkdircommand.$ mkdir -p $HOME/bin $HOME/.kube
The folders I've created are the following.
$HOME/bin: in some Linux systems like Debian, this directory is already in the$PATHof any user, but has to be created.$HOME/.kube: where the kubectl command will look for the configuration file to connect to the K3s cluster.
-
Download the
kubectlcommand in your$HOME/bin/kubectl-binfolder withcurlas follows.$ curl -L "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o $HOME/bin/kubectl
Check with
lsthe file you've downloaded.$ ls bin kubectl
-
Adjust the permission of this file so only your current user can execute it.
$ chmod 700 $HOME/bin/kubectl
At this point, DON'T execute the kubectl command yet! You still need to get the configuration for connecting with the cluster, so keep reading.
The configuration file you need is inside the server nodes of your K3s cluster.
-
Get into your K3s cluster's server node, and open the
/etc/rancher/k3s/k3s.yamlfile in it. It should look like below.apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJlRENDQVIyZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQWpNU0V3SHdZRFZRUUREQmhyTTNNdGMyVnkKZG1WeUxXTmhRREUyTXpFMk9UZzNOamt3SGhjTk1qRXdPVEUxTURrek9USTVXaGNOTXpFd09URXpNRGt6T1RJNQpXakFqTVNFd0h3WURWUVFEREJock0zTXRjMlZ5ZG1WeUxXTmhRREUyTXpFMk9UZzNOamt3V1RBVEJnY3Foa2pPClBRSUJCZ2dxaGtqT1BRTUJCd05DQUFSZXNVc2ZwMzllVHZURkVYbE5xaDlNZUkraStuSTRDdzgrWkZZLzUyVDIKWVJkRkpQQy85TjlDRVpXVUpRYzFKeXdOOE00dGlLNDlPV3hOdE10aWdaUElvMEl3UURBT0JnTlZIUThCQWY4RQpCQU1DQXFRd0R3WURWUjBUQVFIL0JBVXdBd0VCL3pBZEJnTlZIUTRFRmdRVTYzZDVVTFUvRXNkRHZnQ1ZVMjc3CkplS1hzam93Q2dZSUtvWkl6ajBFQXdJRFNRQXdSZ0loQU5WUERDZ3k1MjdacGRMOWs5SVNpcnlIc2xVY21jK2YKcCtFeGxVcW9HZVhTQWlFQXFrb1c3RHpOR3dVVnNQTTlsWjlucm9wOXdwbjR4Ny9wczZZRUhYUDg1djQ9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K server: https://0.0.0.0:6443 name: default contexts: - context: cluster: default user: default name: default current-context: default kind: Config preferences: {} users: - name: default user: client-certificate-data: LS0tLS1CRUdJTiB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t3V1RBVEJnY3Foa2pPClBRSUJCZ2dxaGtqT1BRTUJCd05DQUFUUytJNk0ycEk3VllvRGxKUkdXTXZXSUZ4SXU4RUlCTFhpK1RZQVpyM3cKNFY1S052aUtoajBwWFdwZG8xWnJnNmpITmJHUzFINjZpUVdZMTZwWk96VnhvMEl3UURBT0JnTlZIUThCQWY4RQpCQU1DQXFRd0R3WURWUjBUQVFIL0JBVXdBd0VCL3pBZEJnTlZIUTRFRmdRVTUwWk4yZXczN1lTLzU4Z2F1V0JhCjRyZ004NEF3Q2dZSUtvWkl6ajBFQXdJRFNRQXdSZ0loQUpXMCtrN1lXUHJhamV2dGtzMmlHQjMyTG5tS2ZCcGMKbHhXbVBvOEVIeDdpQWlFQXQ3L1hIbVBNNlJzNDBsUUREZGEwTUpINmJ2bGl0MnVLNGpoMHVxbGNqZUU9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K client-key-data: LS0tLS1CRUdJTiBFQyBQUklWQVRFIEtFWS0tLS0tCk1IY0NBUUVFSUpCTkxJK0ZtNFNqZUlqUFBiSnFNRWlDWmtuU1dJL0JOYnNWWVM1VkhydTZvQW9HQ0NxR1NNNDkKQXdFSG9VUURRZ0FFMm1mQ1FYeDFaOWNUMTZmcUMweVRLQVhZdmxXNEVGME1GVTVtSjRHV1llbkFaOVhFclNMSApCZk5sNEVzbDk4b2FTa3VnVUREMGg5SHFCb0VPeVVVWnVRPT0KLS0tLS1FTkQgRUMgUFJJVkFURSBLRVktLS0tLQo=
-
Copy the
k3s.yamlfile in your client system, then move it to the.kubefolder in your user's$HOMEpath but renamed asconfig.$ mv k3s.yaml $HOME/.kube/configThen adjust the permissions and ownership of the
configfile as follows.$ chmod 640 $HOME/.kube/config $ chown youruser:yourusergroup $HOME/.kube/config
-
Alternatively, you could just create the
configfile, then paste the contents of thek3s.yamlfile in it.$ touch $HOME/.kube/config ; chmod 640 $HOME/.kube/config
-
-
Edit the
configfile and edit theserver:line present there. You'll have to replace the url with the external IP and port of the K3s server node from which you got the configuration file. For instance, it could behttps://192.168.1.21:6443(thek3sserver01node in this guide series).... server: https://192.168.1.21:6443 ...Save the change to the file.
Now is the moment to open the 6443 port on the external IPs of your K3s server node, but only for the IP of your kubectl client. I'll list you briefly the steps to do so.
-
Go to your Proxmox VE web console, then go to the
Datacenter > Firewall > Aliasview. There, add your client's IP as a new alias, but giving it a significant name. For instance, you could use the following schema.ext_sys_[client's hostname]_net[network interface number]If the client happened to be called
kubectl_client, the alias would be namedext_sys_kubectl_client_net0. -
Browse to the
Datacenter > Firewall > IPSetpage and create a new IP set calledkubectl_ext_clients. Add to it the alias created in the previous step. -
Go to the
Datacenter > Firewall > Security Grouppage. Add to thek3s_srvrs_net0_insecurity group the following rule.- Type
in, ActionACCEPT, Protocoltcp, Sourcekubectl_ext_clients, Dest. port6443, CommentK3s api server port open externally for kubectl external clients.
The security group should end looking like in the next screenshot.
- Type
-
To verify that you can connect to the cluster, try in your client the
kubectl cluster-infocommand.$ kubectl cluster-info Kubernetes control plane is running at https://192.168.1.21:6443 CoreDNS is running at https://192.168.1.21:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy Metrics-server is running at https://192.168.1.21:6443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
And this is it! From now on, you can execute any kubectl command you want on your cluster from this particular client.
If you are using bash in your client system, you can enable the bash autocompletion for kubectl.
-
Open a terminal in your client system and do the following.
$ sudo touch /etc/bash_completion.d/kubectl $ kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl -
Then, execute the following
sourcecommand to enable the new bash autocompletion rules.source ~/.bashrc
Since from now on you're going to deal with Kubernetes configuration files, you'd like to know if they're valid before you apply them in your K3s cluster. To help you with this task, there's a command line tool called Kubeval that you can install in your kubectl client system as follows.
-
Download the compressed package containing the executable in your
$HOME/bindirectory (which you created already during thekubectlsetup).$ cd $HOME/bin ; wget https://github.com/instrumenta/kubeval/releases/latest/download/kubeval-linux-amd64.tar.gz
-
Unpackage the
tar.gz's contents.$ tar xvf kubeval-linux-amd64.tar.gz
-
The
tarcommand will extract three files. One is thekubevalcommand, the other two are aLICENSEand aREADME.mdfile that you can delete together with thetar.gz.$ rm kubeval-linux-amd64.tar.gz LICENSE README.md
-
The kubeval command already comes enabled for execution, but you might like to restrict its permission mode so only your user can execute it.
$ chmod 700 kubeval
-
Test the command by getting its version.
$ kubeval --version Version: 0.16.1 Commit: f5dba6b486fa18b9179b91e15eb6f2b0f7a5a69e Date: 2021-03-30T15:17:06Z
BEWARE!
Theres's an important issue with this command, as of its0.16.1version at least. It cannot validate the Kubernetes kustomizekustomization.yamlfiles (which you'll use a lot in upcoming guides) properly and returns errors where it shouldn't while checking them.
$HOME$HOME/.kube$HOME/bin
$HOME/.kube/config$HOME/bin/kubectl$HOME/bin/kubeval
/etc/rancher/k3s
/etc/rancher/k3s/k3s.yaml
<< Previous (G025. K3s cluster setup 08) | +Table Of Contents+ | Next (G027. K3s cluster setup 10) >>
