Kubernetes for dummies

This article is a quick start HowTo for kubernetes. It doesn’t include some k8s stuff like namespaces, jobs, cronjobs, history, ReplicaSet, auto service discovery, supervision and other. Just quick description as simple as possible to be able to start working with the Kubernetes.

k8s

What is the Kubernetes?

For simplicity: this is a container management system.

When I say a container – I mean isolated environment for an application. It’s a Docker.

Pod

One application running in k8s is called a pod. A Docker container – is one pea inside a pod. You can start many Docker containers inside one pod. For example it can be a pod called web with two docker containers: php and db.

Docker container – is one pea inside a pod.

Usually you can find only one Docker container inside a pod. It’s a normal. Because second container can be a metrics exporter, an adapter or other system-specific container.

Peas Pod Pea Pod Green Fresh
Magnascan / Pixabay

All containers has the same TCP/IP address inside a pod. It can communicate using a localhost hostname.

Deployment и StatefulSet

A (Pod) can has other resources like: name, label, number of replicas. We can connect external volumes or init-containers to a pod. And many other resources.

So one pod with resources is called Deployment. To describe Deployment we need to use yaml file like this:

---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: web-site
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: php
          image: php-fpm:latest
          ports:
            - containerPort: 80
          volumeMounts:
            - name: data
              mountPath: /var/www
              subPath: data
        - name: db
          image: mysql:latest
          volumeMounts:
            - name: db
              mountPath: /var/lib/mysql
              subPath: db

Here you can see an image variable with the Docker image name. For example: image: osixia/openldap:1.2.5 or image: localhost:5000/openldap:latest. You can also find a TCP ports and volumeMounts definition.

NOTE: A StatefulSet – is another kind of deployment. Each pod in a StatefulSet has a static hostname and personal volume. It can be used for example for a PostgreSQL application with a master-slave replication where each instance has a personal data set.

Service

Ok. Now we have an application with some of the resources described in Deployment. But we want to open an external port for this application.

We need to create another kind of resource: The Service:

apiVersion: v1
kind: Service
metadata: 
  name: web-page
spec:
  selector:   
    app: web
  type: NodePort
  ports: 
  - name: http
    port: 80
    targetPort: 80
    nodePort: 9000
    protocol: TCP

Here you can see a new (Service) called web-page. It has a selector, that can choose our application by label: app: web. You can find this label above in the Deployment description.

We need to use labels to select resources in the k8s

You can also find a: type: NodePort. There are several types of services available:

  • NodePort – external TCP/IP port on the kubernetes node. It will open a new port (nodePort: 9000) right on the kubernetes server (node).
  • ClusterIPinternal IP address on the kubernetes cluster network.
  • LoadBalancerIP – external (white) IP address for service.

For example you can install your http reverse-proxy server with the LoadBalancerIP type and external IP address. But describe other services as the ClusterIP type. In k8s such proxy-server called Ingress. You can read about it below.

Done! Deployment + Service – that’s all for a first time.

How it works

Kubernetes

Here is your application: app: web running with 1 replica. And other (app: other) – with many replicas. It has a different labels, so Ingress proxy server can select differen services depending on some rules.

Ingress

Ingress – it’s a http reverse proxy server. It can select destination end point by some rules:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web-ingress
spec:
  rules:
    - host: www.burlutsky.su
      http:
        paths:
          - path: /
            backend:
              serviceName: web-page
              servicePort: http

Here you can see the rule with a host name and a path. You can also find a ServiceName: serviceName: web-page that will be used for this rule.

NOTE: It is better to use the ClusterIP type for the web-page Service in this example. You need also remove a nodePort parameter to avoid opening an external port on the k8s node.

Helm

Good! But how can we start? I suppose to use the Helm.

Helm k8s

With the Helm we don’t need to create an yaml files. We will use a templates!

It’s not a good practice to work with a many-many k8s yaml-files directly. Moreover we can’t use it with a different values. We need to change yaml-files every time to setup a same application but with a different name or login / password / domain name /etc .

So one of the most popular templating solution for the Kubernetes for today is – Helm!

Here you can find an example of the Helm template (yaml-file described above):


{{- $fullName := include "fullname" . -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}-ingress
spec:
  rules:
    - host: {{ .Values.hostname }}
      http:
        paths:
          - path: /
            backend:
              serviceName: {{ $fullName }}
              servicePort: http

You can find varibales:

  • .Values.hostname – it’s a domain name variable from a special values.yaml-file:
hostname: www.burlutsky.su

And this variable now available as .Values.hostname. So we don’t need to change our ymal-files. We can only change one values.yaml file.

  • $fullName – other template variable, with a service name. You can find it in a special: _helpers.tpl -file.

You can find all the yalm-files in the templates directory. So, grab any example that you want from this url : https://github.com/helm/charts/tree/master/stable

Try to find Deployment (or StatefulSet), Service, Ingress. Usually you can find it in the templates directory: deployment.yaml, services.yaml

To install application in k8s with your values you can change existing values.yaml-file or create a new my-values.yaml file with values that you want to change:

helm install -f my-values.yaml stable/postgresql

This command will get values from the my-values.yaml file, merge it with the values.yaml from distribution, generate output yaml-files and apply it in the k8s.

Useful commands

  • kubectl get pods – list all pods
  • kubectl describe pod <name> – get additional information about a pod
  • kubectl get services – list all services
  • kubectl get deployments – list all deployments
  • helm list – list applications (releases) installed by helm

Minikube

To try kubernetes in your own computer or a virtual machine you can use the Minikube application: https://github.com/kubernetes/minikube

Good luck with the k8s!

Tagged with:

Leave a Reply

Your email address will not be published.