K3s Notes - Part 3

In Parte 2 of K3s, a description of various concepts that are handled in Kubernetes was made, the kubectl client was installed and connectivity tests to the Kubernetes API were done.

Now we will execute basic tasks in our small cluster, but before we continue describing more details of Kubernetes.

Kubernetes objects

  1. Pod: It is a group of one or more containers (such as Docker containers), with shared storage/network, and some specifications of how to run the containers.
  2. Service: An abstract way to expose an application running on a set of Pods as a network service.
  3. Volume: It has a functionality similar to that of Docker Volume, where data persistence is searched after restarting one container.
  4. Namespace: It allows us to isolate resources for the use of the different users of the cluster.
  5. Manifest: It is a yaml manifest file that contains instructions that specify how to deploy an application to the node or nodes in a Kubernetes cluster.

Kubernetes Drivers

Kubernetes contains top-level abstractions called Controllers. The Controllers are based on the basic objects and provide additional functionality on top of them.

  1. Replicaset: The purpose of a ReplicaSet is to keep a stable set of replicas of Pods running at all times. Thus, it is used numerous times to ensure the availability of a specific number of identical Pods.
  2. Deployment: A Deployment controller provides declarative updates for Pods and ReplicaSets. When you describe the desired state on a Deployment object, the Deployment controller takes care of changing the current state to the desired state in a controlled way. You can define Deployments to create new ReplicaSets, or remove existing Deployments and adopt all their resources with new Deployments.
  3. StatefulSet: A StatefulSet is the workload API object used to manage stateful applications.
  4. DaemonSet: A DaemonSet ensures that all (or some) of the nodes run a copy of a Pod. As more nodes are added to the cluster, new Pods are added to them. As nodes are removed from the cluster, those Pods are destroyed. Deleting a DaemonSet clears all the Pods that have been created.
  5. Job: A Job creates one or more Pods and ensures that a specific number of them finish successfully. As pods complete successfully, the Job tracks successful executions. When a specified number of successful executions is reached, the task (that is, the Job) is completed. Deleting a Job removes the Pods you have created.

Commands Kubernetes

Some common commands:

1# List the available namespaces
2$ kubectl get ns
3# List Pods available in a Namespace
4$ kubectl -n kube-system get pods
5# List Pods available in a Namespace with more details
6$ kubectl -n kube-system get pods -o wide
7# Delete a pod
8$ kubectl -n kube-system delete pod NAME_RUN_POD

As an extra detail if there is a problem with the configuration file to the node we can execute:

1$ export KUBECONFIG=PATH_CONFIG_FILE

Yaml File Examples

1. For our first example we will create and apply a basic file:

1apiVersion: v1
2kind: Pod
3metadata:
4  name: fcch-nginx
5spec:
6  containers:
7  - name: fcch-nginx
8    image: nginx:alpine

To execute the content of the yaml files we must know some commands:

1$ kubectl apply -f basic_pod.yaml

To enter the container inside the pod we can execute:

1$ kubectl exec -it fcch-nginx -- sh

To remove the pod created:

1$ kubectl delete pod fcch-nginx
2# Verificate pods 
3$ kubectl get pods

2. For this example we will use environment variables and allocate resources.

 1apiVersion: v1
 2kind: Pod
 3metadata:
 4  name: fcch-nginx
 5spec:
 6  containers:
 7  - name: fcch-nginx
 8    image: nginx:alpine
 9    env:
10    - name: NAME_VARIABLE
11      value: "fcch"
12    - name: NAME_VARIABLE_OTHER
13      value: "fcch-blog"
14    - name: DD_AGENT_HOST
15      valueFrom:
16        fieldRef:
17          fieldPath: status.hostIP
18    resources:
19      requests:
20        memory: "64Mi"
21        cpu: "200m"
22      limits:
23        memory: "128Mi"
24        cpu: "500m"
25    readinessProbe:
26      httpGet:
27        path: /
28        port: 80
29      initialDelaySeconds: 5
30      periodSeconds: 10
31    livenessProbe:
32      tcpSocket:
33        port: 80
34      initialDelaySeconds: 15
35      periodSeconds: 20
36    ports:
37    - containerPort: 80

If we want to verify the yaml file that was executed we can execute:

1$ kubectl get pod fcch-nginx -o yaml

If you need to see the details of a pod:

1$ kubectl describe pod fcch-nginx

3. In this example we will use volumes, the type will be StatefulSet and we will use replicas.

A detail before continuing, PVC: Persistent Volume Claim.

 1apiVersion: apps/v1
 2kind: StatefulSet
 3metadata:
 4  name: app-test
 5spec:
 6  selector:
 7    matchLabels:
 8      app: apppod
 9  serviceName: "first-frontend"
10  replicas: 2
11  template:
12    metadata:
13      labels:
14        app: apppod
15    spec:
16      containers:
17      - name: first-frontend
18        image: busybox
19        args:
20        - sleep
21        - infinity
22        volumeMounts:
23        - mountPath: "/data"
24          name: app-pvc
25  volumeClaimTemplates:
26  - metadata:
27      name: app-pvc
28    spec:
29      accessModes:
30      - ReadWriteOnce
31      resources:
32        requests:
33          storage: 5Gi
34      storageClassName: do-block-storage

To work with Volumes we can execute the following commands:

 1# List volumes
 2$ kubectl get pvc
 3# Show details PVC
 4$ kubectl describe pvc app-pvc-app-test-0
 5# List Pods of type StatefulSet
 6$ kubectl get statefulsets
 7# List Pods of type StatefulSet
 8$ kubectl get stss
 9# Delete Pod of type StatefulSet
10$ kubectl delete sts app-test
11# Delete PVC
12$ kubectl delete pvc app-pvc-app-test-0

Some commands that can help us:

1# List all
2$ kubectl get all
3# List services
4$ kubectl get svc
5# Service detail
6$ kubectl describe svc kubernetes

Artículos K3s

  1. K3s - Part 1
  2. K3s - Part 2
  3. K3s - Part 3
  4. K3s - Part 4

References

Translations: