Kubernetes Build
Tags: kubernetes, docker, probe, test
Published: 2023-09-29 15:47:34

This post sums up the steps to build a Kubernetes application.


## CRI - Container Runtime Interface
Kubernetes is designed to work with many different container runtimes like Docker, CRI-O, containerd, rkt and others. The CRI allows easy integration of various container runtimes with kubelet.


## Containerizing an application
- The more stateless and transient the better an app is suited for containerization
- Environmental configuration needs to be provided via configMaps and secrets

### What it is
A dockerfile is a list of commands from whit an image can be build.
An image is a binary file that includes everything needed to be run as a container. The images are usually stored in the container registry.
A container is a running instance of an Image.

### Sample with Docker
After you wrote your docker file do:

```
sudo docker build -t yourapp # build the container
sudo docker images # verify the image 
sudo docker run yourapp #execute the image
sudo docker push # push to the repository
```

you can keep your docker images local, on a repo or on a container registry of a cloud provider, like azure container registry, google, aws.
Every container in a POD shares a single IP address and namespace. Every container has equal potential storage given to the pod.


## Probes
Three different types of probes help to ensure that applications are ready for traffic and healthy within Kubernetes.

### readinessProbe
If your application needs to be initizalized or configured in order to accept traffic, you can use the readinessProbe. 
The container will not accept traffic until the probe returns a healthy state.

### livenessProbe
It checks if the container is in a healthy state, while running. If it fails, the container is terminated and a replacement would be spawned.

### startupProbe
This probe is used to test an application that takes a long time to start. The duration until a container is considered to have failed is determined by the `failureThreshold x periodSeconds`. If the periodSeconds is set to 5 seconds and the failureThreshold set, it would check every 5 seconds and fail after a total of 50 Seconds.

### Probes samples
```
apiVersion: v1
kind: Pod
metadata:
  name: your-app
spec:
  containers:
    - name: your-container
      image: your-image:latest
      ports:
        - containerPort: 8080
      startupProbe:
        httpGet:
          path: /areyouready
          port: 8080
        initialDelaySeconds: 10
        periodSeconds: 5
      livenessProbe:
        httpGet:
          path: /health
          port: 8080
        initialDelaySeconds: 15
        periodSeconds: 10
      # Define a custom configuration probe
      # readinessProbe:
      #  exec:
      #    command:
      #     - cat
      #     - /app/config/config.yaml
      #  initialDelaySeconds: 20
      #  periodSeconds: 30
```


## Create a POD
With the following command, you can create a pod as defined in a file called `your-pod.yaml`

```
kubectl apply -f your-pod.yaml
```


## Testing
To see if everything is working as expected, you can use the describe functionality or get the logs of a pod.

```
kubectl describe pod <pod-name>
```
```
kubectl logs <pod-name> -c <container-name>
```

In the next post I will show you how to write a declarative configuration to define the desired state of an containerized piece of code - in Kubernetes it is called DEPLOYMENT. See you there.

Further reading:
Container runtimes: https://github.com/containers
Helm: https://helm.sh/
ArtifactHub: https://artifacthub.io/
Resource: https://training.linuxfoundation.org/training/kubernetes-for-developers/