Here, we'll quickly go over two ways to set up Kubernetes:

  • A development cluster with minikube
  • A managed cluster rented from a provider (Scaleway, DigitalOcean, Azure, or Google Cloud)

We'll start with the first option.

Discovering Kubernetes

Install the kubectl CLI client

kubectl is the universal entry point for controlling all types of Kubernetes clusters. It's a command-line client that communicates with the API of a cluster over REST.

We'll explore kubectl gradually throughout the TPs. However, note that:

  • kubectl can manage multiple clusters/configurations and switch between them
  • kubectl is necessary for the Lens graphical client that we'll use later.

The installation method doesn't matter much. To install kubectl on Ubuntu, we'll simply do: `sudo snap install kubectl –classic`.

  • Run `kubectl version` to display the kubectl client version.
Install Minikube

Minikube is the most popular development version of Kubernetes (locally). It's maintained by the Cloud Native Foundation and very close to upstream Kubernetes. It allows simulating one or more cluster nodes in the form of Docker containers or virtual machines.

We will typically use Docker as the runtime for minikube (the k8s nodes will be containers simulating servers). This is, of course, a development configuration. However, it behaves very similarly to a real cluster.

  • If Docker is not installed, install Docker with the one-liner command: `curl -fsSL https://get.docker.com | sh`, then add yourself to the Docker group with `sudo usermod -a -G docker <yourname>`, and `sudo reboot` to make it take effect.
  • To start the cluster, simply do: `minikube start` (it's also possible to specify the number of CPU cores, memory, and other parameters to adapt the cluster to our needs.)

Minikube automatically configures kubectl (in the `~/.kube/config` file) so that we can connect to the development cluster.

  • Test the connection with `kubectl get nodes`.

Display the kubectl version again. This time the version of Kubernetes running on the active cluster is also displayed. Ideally, the client and the cluster should be in the same minor version, for example, 1.20.x.

Bash completion

To allow kubectl to complete command and resource names with <Tab>, it's useful to install Bash autocompletion:

sudo apt install bash-completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ${HOME}/.bashrc

Now you can press <Tab> to complete your kubectl commands, which is very useful!

Explore our k8s cluster
  • Our k8s cluster is full of various objects, organized dynamically to describe applications, compute tasks, services, and access rights. The first step is to explore the cluster a bit:
  • List the nodes to retrieve the name of the unique node (`kubectl get nodes`) then display its characteristics with `kubectl describe node/minikube`.
  • The `get` command is generic and can be used to retrieve the list of all types of resources.
  • Similarly, the `describe` command can be applied to any k8s object. However, we must prefix the object name with its type (e.g., `node/minikube` or `nodes minikube`) because k8s cannot guess what we are looking for when multiple resources have the same name.
  • To display all resource types that we use: `kubectl get all`

It seems that there is only one resource in our cluster. This is the Kubernetes API service, so that we can communicate with the cluster.

In reality, there are usually others hidden in other namespaces. In fact, the internal elements of Kubernetes themselves run as Kubernetes services and daemons. Namespaces are groups that serve to logically isolate resources and in terms of rights (with Kubernetes' Role-Based Access Control (RBAC)).

To verify this we can:

  • Display the namespaces: `kubectl get namespaces`

A Kubernetes cluster typically has a namespace called `default` in which commands are run and resources created if nothing is specified. It also has a namespace `kube-system` where k8s system processes and resources reside. To specify the namespace, we can add the `-n` argument to most k8s commands.

  • To list resources related to the kube-system, `kubectl get all -n kube-system`.
  • Or: `kubectl get all –all-namespaces` (can be abbreviated to `kubectl get all -A`) which allows displaying the contents of all namespaces at once.
  • To get information about a namespace: `kubectl describe namespace/kube-system`
Deploy an application

We will now deploy a first containerized application. Deployment is more complex than with Docker (and Swarm), especially because it's separated into several objects and more configurable.

  • To create a deployment from the command line (as opposed to the declarative mode we'll see later), we can run for example: `kubectl create deployment microbot –image=monachus/rancher-demo`.

This command creates a deployment object. We can study this deployment with the `kubectl describe deployment/microbot` command.

  • Let's scale up this deployment with `kubectl scale deployment microbot –replicas=5`.
  • `kubectl describe deployment/microbot` allows us to see that the service has been scaled to 5 replicas.

At this stage, the deployment is not yet accessible from outside the cluster, so we need to expose it as a service:

  • `kubectl expose deployment microbot –type=NodePort –port=8080 –name=microbot-service`
  • Let's list the services to see the result: `kubectl get services`

A service allows exposing a deployment either by port or using a load balancer.

To expose this application on the port of our choice, we should use a LoadBalancer.

We won't see it here (we would need to use the Minikube MetalLB addon).

But we can still run a command in our dev environment: `kubectl port-forward svc/microbot-service 8080:8080 –address 0.0.0.0`

You can now access your app via: [http://localhost:8080](http://localhost:8080) Minikube also integrates a way to access our service: it's the `minikube service microbot-service` command.

Can you explain what the app does?

Simplify Kubernetes command lines

To save time in Kubernetes commands, we generally define an alias: `alias kc='kubectl'` (to be put in your .bash_profile by doing `echo “alias kc='kubectl'” » ~/.bash_profile`, then `source ~/.bash_profile`). You can then replace `kubectl` with `kc` in the commands. Also, to save time on the command line, most Kubernetes keywords can be abbreviated: - `services` becomes `svc` - `deployments` becomes `deploy` - etc. The complete list: [https://blog.heptio.com/k

ubectl-resource-short-names-heptioprotip-c8eff9fb7202](https://blog.heptio.com/kubectl-resource-short-names-heptioprotip-c8eff9fb7202)

Try displaying the service accounts (users) and namespaces with a short command.

Install Lens

Lens is a nice graphical interface for Kubernetes. It connects using the default `~/.kube/config` configuration and will allow us to access a much nicer dashboard. You can install it by running these commands:

sudo apt-get update; sudo apt-get install -y libxss-dev
curl -fSL https://github.com/lensapp/lens/releases/download/v4.0.6/Lens-4.0.6.AppImage -o ~/Lens.AppImage
chmod +x ~/Lens.AppImage
~/Lens.AppImage &

Setting up a K8s cluster in the cloud with a provider like DigitalOcean or Scaleway

  • Create an account (or get access) on DigitalOcean or Scaleway
  • Create a Kubernetes cluster using the DigitalOcean interface or the Scaleway interface
  • The creation takes about 5 minutes.
  • On DigitalOcean, you are offered in step 3 or on the page of your Kubernetes cluster to download the kubeconfig file. (download the cluster configuration file, or Download Config File).
  • Similarly, on Scaleway, on the page describing your cluster, a large button at the bottom of the page encourages you to download this same kubeconfig file (Download Kubeconfig).

This file contains the kubectl configuration suitable for connecting to our cluster.

Merge the kubectl configuration

  • Open the kubeconfig and `~/.kube/config` files with gedit.
  • Merge in `~/.kube/config` the elements of the YAML lists of:
    • clusters
    • contexts
    • users
  • Set the `current-context:` key to `<cluster_name>` (replace with your value)
  • Test the connection with `kubectl get nodes`.
Deploy the application
  • Run `kubectl cluster-info`, the cluster API is accessible from a domain name generated by the provider.
  • Deploy the microbot application as in the previous section with minikube
  • To visit the application, you need to find the public IP of one of the cluster nodes by listing objects of type Service, or on the cloud provider's page.
teaching_assistant/workflow/installation_and_configuration_of_kubernetes.txt · Last modified: 2024/05/15 12:09 by Ralph
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0