Managing the Kubernetes Cluster¶
This article will outline methods and commands to manage our Kubernetes cluster. I'll walk you through the basics of SSHing into the server. Then an overview of some tools that you can use to manage the cluster.
SSHing into the Server.¶
Note
It is assumed that you are already aware of what SSH is and how to use it.
This is the most basic way to interact with the server. You can use it get a look at things like the mounted disks, running processes, trigger builds on the system etc.
The server has no password login method. Thus, to get access to the server, you need to ask someone who already has access to it to add you to its list of trusted hosts.
In order to add someone to the server, you need to first generate a SSH key-pair. To do that make sure that you have a ssh on your computer.
Figure out how to install that if you don't. Then you can run
- Follow the steps to generate the files correctly.
This should create a public-private key pair in your ~/.ssh folder. You then need to send your public key (the file ending with .pub) to
a server admin.
Warning
It is very bad practice to share your private ssh key. Make sure that you never do that!
When sharing your public key
SSH verification doesn't work if you have trailing \n or \t or even \0 characters
when you paste it. Make sure that you have correctly copied your public key when sharing it.
The server admin then has to add the public key in a newline in the ~/.ssh/authorized_keys file. The easiest way to do this is to
open the file in nano or vim on the server and paste it as a newline. I would not recommend directly piping it into the file using the >> operator
You can then connect to the server by running:
Make sure that you type
yeswhen prompted.
Basic Inspection Through SSH¶
There are some basic thing that you can do via SSH to interact with the server and the cluster on it. Some things that i've found usefull are:
- This lets you see free disk space. The
-hmakes the output human readable htopis a pretty way to see the usage of the server. It's a nicer formatted version of thetopcommand that is standard on LINUX systems.- This lets you see the Number of PROCesses on the machine
- This lets you see logs from the servers system. Letting you spot thing like compiler invocations and system calls that sometimes tell you why things might be crashing.
Use tmux for longer tasks
If you are doing any sort of monitoring, or longer tasks like building containers whilst SSH'd into the server. I would recommend using tmux
which is preinstalled on the server. Use it to have persistent 'sessions' that will run even if your ssh disconnects.
kubectl¶
This is perhaps the most important tool that you will have in order to interact with the cluster. It allows for you to send requests to the Kubernetes API through your command line. Follow the installation instructions to install it on your personal computer
Setting Up kubectl¶
There are then some steps that you have to do in order to get your local installation of kubectl to talk to the cluster.
- SSH into the cluster and copy the contents of the
/etc/rancher/k3s/k3s.yamlfile. You can do this with the following: - Run
mkdir ~/.kube && touch ~/.kube/configto make a file calledconfig. - Paste the content you copied into the
configfile.
You can then check that kubectl is properly installed by running:
Alias kubectl to something easier
You are going to be using kubectl a lot. It's best to alias it something easier to type. Here is what I have:
.bashrc or .zshrc to have it persistent.
Also make sure that your kubectl completions are properly setup. They are a lifesaver.
Using kubectl & Basic Patterns You'll See¶
Some of the basic commands you'll see yourself doing often are:
get¶
This is a command to list objects that you specify.
1. The-A is shorthand for --all-namespaces you will be using it a lot, or you will be specifying the namespace you want to look inside with -n <namespace>
2. List pods with more information, equivalent to the -v flag in most other commands.
describe¶
This is a command that lets you get the description of objects in your cluster.
k describe -n jenkins pods jenkins-0
k describe -n container-registry pod container-registry-zot-0
k describe -n ingress ingressroutes.traefik.io http-catch-all-redirect
log¶
This lets you see the logs of k8s object.
K9s¶
K9s is a cool little TUI to visually inspect the state of your cluster and perform actions such as viewing logs and descriptions more ergonomically. Read the installation instructions here