Skip to content

K8s Howto

Managing the cluster using the GUI

The cluster can be fully managed via the GUI. The URL for the management interface has been communicated to you. The SuSE documentation covers everything you need to know about rancher. Here is the link to the documentation: SuSE Documentation (external)

Command Line Access to the Cluster

Setting up the Basic Tools

Note that the following assumes use of a Unix like system - these have been tested with Linux but should also work on WSL2 and OSX.

To connect to the cluster via the command line, you need to install some basic tools. We suggest installing the following:

  • arkade
    • Arkade source
    • this is a handy utility for managing k8s command line tools
    • One-liner for installing arkade
      • curl -sLS https://get.arkade.dev | sudo sh
      • Run arkade --help to ensure it has been installed correctly
  • kubectl
    • this is the primary command line tool for interacting with k8s clusters
    • it can be installed via arkade using arkade get kubectl
    • you may have to modify your path or mv the binary to a different directory to provide systemwide access - check the arkade output for pointers on this.
  • helm (optional, recommended)
    • this tool is used for deploying and managing more complex applications on a k8s cluster
    • it can be installed via arkade using arkade get helm
    • you may have to modify your path or mv the binary to a different directory to provide systemwide access - check the arkade output for pointers on this.
  • k9s (optional, recommended)
    • this tool is used for viewing the state of your resources on the cluster
    • it can be installed via arkade using arkade get k9s
    • you may have to modify your path or mv the binary to a different directory to provide systemwide access - check the arkade output for pointers on this.

With the basic tools set up, you should be able to run kubectl but you will not be able to access the cluster as you have not provided any credentials to the kubectl command.

Configuring your Credentials

Your credentials can be downloaded from the Rancher UI in the cluster view.
rancher-download-credentials.png

Download these credentials and put them into the ~/.kube directory; ensure that they are in a file called ~/.kube/config.

You should now be able to access the cluster using one of the namespaces to which you have access:

$ kubectl -n <namespace> get pods
No resources found in <namespace> namespace.

Note

If you don't provide a namespace, the default namespace will be used.

Deploying your first Applications

Once you have access to the cluster, you can deploy your first application using kubectl. To deploy your application with kubectl you will need to provide a yaml input which describes the application.

Many such yaml files are available on the internet. We provide a git repository with two test applications which can be deployed.
The repository is here: https://github.zhaw.ch/InIT-Operations/K8s-examples.git. You can clone the repository to your local machine.

Deploying an Ubutu Container

In the ubuntu directory (repository you just cloned) run the following (substituting <namespace> as appropriate) to deploy an ubuntu container.

$ kubectl -n <namespace> apply -f ubuntu-deployment.yaml
deployment.apps/ubuntu-deployment created

This is a very simple deployment comprising just a single pod; as you can see, the yaml file specifies the resources required by the pod.

You can confirm that the pod has been created using:

$ kubectl -n <namespace> get pods
NAME                                   READY   STATUS    RESTARTS   AGE
ubuntu-deployment-dbc9ccdc8-4rvb7      1/1     Running   0          28s

As you can see, kubernetes assigns a unique idenitifier to your new ubuntu pod.

You can use kubectl to obtain a shell in the container.

$ kubectl -n <namespace> exec -ti ubuntu-deployment-dbc9ccdc8-4rvb7 -- bash
root@ubuntu-deployment-dbc9ccdc8-4rvb7:/#

Remove the Container as Follows

$ kubectl -n <namespace> delete -f ubuntu-deployment.yaml
deployment.apps "ubuntu-deployment" deleted

Deploying a Wordpress Application

Next, we consider a more complex deployment: a wordpress deployment comprising the wordpress application, a mysql database, some persistent volumes and secrets.

Warning

The following example is for educational purposes only. An older version of Wordpress will be installed, which is not safe at all. Please delete your Deployment right after testing.

Enter the wordpress directory and deploy the application as before:

$ kubectl -n <namespace> apply -f wordpress-deployment.yaml
secret/mysql-pass created
service/wordpress created
persistentvolumeclaim/wp-pv-claim created
deployment.apps/wordpress created
service/wordpress-mysql created
persistentvolumeclaim/mysql-pv-claim created
deployment.apps/wordpress-mysql created

This time a larger set of resources are created for your application - more specifically:

  • A Secret for the MySql password
  • A PersistentVolumeClaim for MySql (which automatically creates a PersistentVolume)
  • A PersistentVolumeClaim for Wordpress (which automatically creates a PersistentVolume)
  • A Deployment for MySql
  • A Deployment for Wordpress
  • A Service for MySql
  • A Service for Wordpress

You can check the status of the deployment using:

$ kubectl -n <namespace> get deployments
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
wordpress         1/1     1            1           32s
wordpress-mysql   1/1     1            1           31s

You can see that there are two specific Deployments - wordpress and the database. Also, you can see that the Deployments are READY comprising 1 pod of 1 required pods.

To get information on the specific pods which comprise the Deployments:

$ kubectl -n <-namespace> get pods
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-5994d99f46-ncq6r        1/1     Running   0          67s
wordpress-mysql-6c479567b-tq8xc   1/1     Running   0          66s

Please be aware that it might take several seconds, up to a minute, for your pods to transition to the Running state.

You can use the command k9s -n <namespace> to monitor the status of various resources within the cluster. Alternatively, the same information can be accessed through the Rancher UI. It's important to note that with k9s, you will only be able to view or access resources within the namespaces to which you have permissions.

If everything has worked properly, you now have a running Wordpress instance; however, it is not accessible from outside the cluster. To provide such access, it is necessary to create an Ingress. To do this, you first need to modify the wordpress-ingress.yaml file to specify a unique hostname. This can be done by modifying line 8 in the file wordpress-ingress.yaml it must be changed, for example to:

- host: wp-test.xxx.init-lab.ch

Note that you will need to provide a hostname of the form <application>.xxx.init-lab.ch. All names of this form are configured to resolve to the k8s cluster and the cluster performs proper SSL termination for such hostnames. Use of inappropriate or offensive names is prohibited and will be removed immediately.

When you have edited the file providing a unique hostname, you can apply:

$ kubectl -n <namespace> apply -f wordpress-ingress.yaml
ingress.networking.k8s.io/wp-ingress created

This will deploy your ingress and you should be able to connect to your new Wordpress instance by connecting to the host you specified above from your browser.

Use the following to remove the deployment:

  • kubectl -n <namespace> delete -f wordpress-deployment.yaml
  • kubectl -n <namespace> delete -f wordpress-ingress.yaml
$ kubectl -n <namespace> delete -f wordpress-deployment.yaml
secret "mysql-password" deleted
service "wordpress-service" deleted
persistentvolumeclaim "wp-pv-claim" deleted
deployment.apps "wordpress-deployment" deleted
service "wordpress-mysql" deleted
persistentvolumeclaim "mysql-pv-claim" deleted
deployment.apps "wordpress-mysql-deployment" deleted
$ kubectl -n <namespace> delete -f wordpress-ingress.yaml
ingress.networking.k8s.io "wordpress-ingress" deleted

Persistent Volumes

The cluster supports two different types of Persistent Volumes, ones which are deleted when the associated claim is removed and ones which are kept when the associated claim is removed. These two different types are represented as StorageClasses - they are called cinder-csi-hdd (spinning disk storage) and cinder-csi-ssd (SSD storage) respectively. If no StorageClass is specified, the Local Storage class will be used. In this case your data will be stored locally on the server, your pod is running.

Using container images hosted on a private registry

In some cases, there is a need to use container images which are not public. To do this, it is necessary to create a Secret on the cluster and to use this when deploying the pod - it is necessary to specify imagePullSecrets in the pod specification.

See here for more info.

It's important to be aware that secrets in Kubernetes are not truly encrypted; they are merely base64 encoded. This means that anyone with access to the namespace can retrieve and decode the secret. To enhance security, especially when working with private container registries, it's advisable to create tokens that are limited in scope and/or duration.

Helm Charts and Operators

helm is a tool for deploying and updating applications which can be quite arbitrarily complex, often comprising secrets, volumes, services, pods, deployments etc. Many well known applications provide helm charts to support easy deployment on kubernetes clusters.

In general, helm charts can be deployed on the kubernetes cluster. Typically, you need to specify the namespace to which the application will be deployed, using, for example:

$ helm install -n <NAMESPACE> <DEPLOYMENT-NAME> <HELM-CHART>

It is often necessary to specify particular values for the application which relate to the deployment context; it may, for example, be necessary to specify an ingress for the application which would be specific to our kubernetes cluster. (If you specify values, you will need to use the -f switch with helm)

Operators are a more sophisticated approach which provide dynamic management of applications; as such, they continuously monitor application state and can adapt by, for example, autoscaling components.

Currently, we have very little experience with operators so we cannot say how well they work on the cluster in general. One important point, however, is that many operators require Custom Resource Definitions (CRDs) - we currently do not support CRDs and hence it is not possible to deploy operators which require CRDs on the cluster.

Troubleshooting

General troubleshooting advice

Troubleshooting applications on kubernetes typically involves a mix of interacting with the platform to see if there are any configuration problems, viewing the logs of running containers to see what has happened in the past and logging into running containers to perform some ad hoc testing (eg ensure that an application container can access a database).

To examine the configuration of your application, use the kubectl describe command or you can use k9s to more rapidly navigate around the different resources which have been created for your application.

To view logs of a pod, you can use

$ kubectl -n <namespace> logs <pod-name>

You can access the same information by pressing l (lowercase L) on the pod name in k9s.

To log into a container directly from your command line, you can exec a shell on the container as follows:

$ kubectl -n <namespace> exec <pod-name> -- /bin/bash

Note that this assumes that bash exists in the container (which is not true for alpine images, for example - in that case, you would use ash instead of bash). It is also possible to obtain shell access via k9s (click s on the pod name or via the Rancher UI via the Execute Shell option associated with the pod.

Deployment succeeds, pod starts but no service is accessible

One common reason for this to occur is that the process running in the pod binds to localhost (127.0.0.1) and not the external interface of the pod. In this case, the process may appear to run properly, being able to bind to a network stack successfully, but as localhost inside the pod is only accessible inside the pod, it is not possible to connect a Service to this running process.

Typically, when exposing services inside a pod, you should bind it to 0.0.0.0 which will bind to all IP addresses inside the pod. (We don’t have support for IPv6 in the cluster as yet).

Deployment upgrade is accepted but Pod is not updated and Deployment shows errors

The default behaviour of a Deployment is to use the RollingUpdate upgrade strategy. This means that when updating a Deployment, a new instance is created before the old one is removed. This can cause problems if there is not sufficient quota for two simultaneous deployments. The solution to this is to use the Recreate strategy: this removes the old version of the Deployment before deploying the updated version.

The Recreate strategy can be specified as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ubuntu-deployment
  labels:
    app: ubuntu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ubuntu
  strategy:
    type: Recreate
  template:
    <INSERT TEMPLATE DATA>

Problems with tokens generated by Rancher

There's a known issue when generating tokens in Rancher: if you restrict the token's scope to a specific cluster, the token may not function correctly. Conversely, tokens that are not limited in scope do not experience this issue.

Conclusion: do not apply scope limits when generating tokens in Rancher at this time.

Next Steps

You can modify the Wordpress example to deploy other applications using different container images, you can write your own applications and/or APIs which can be deployed or you can experiment with using helm to install prepackaged applications.