From Docker to KubernetesSchritt 5 von 10: Create a simple Pod
Alle Labs

Create a simple Pod

During the last sections, we touched ground on some of the basic concepts of Kubernetes, such as nodes, namespaces and contexts. Now, we will create our first Pod in the learning-tracker Namespace.

What is a Pod?

A Pod is the smallest deployable unit in Kubernetes. A Pod typically consists of one or more containers that share the same network namespace and can communicate over localhost. Pods are used to run applications and other workloads in Kubernetes.

In our last lab, we created a simple container using Docker. Now, we will put that container into a Pod and run it in our Kubernetes Cluster. Therefore, we will write our first Kubernetes manifest that describes the Pod we want to create.

💡A good starting point

At this point, it's a good idea to open your favorite text editor or IDE, which most likely has a plugin for Kubernetes manifests. Create a project folder for this lab to store the files we will create and change to that folder in your terminal.

Creating a Pod imperatively

You can create a Pod imperatively using the kubectl run command. Therefore, open your terminal and run the following command:

kubectl run learning-tracker --image=ghcr.io/tsclabs-eu/learning-tracker:v1.0.5

This command will create a Pod named learning-tracker in the learning-tracker Namespace using the image ghcr.io/tsclabs-eu/learning-tracker:v1.0.5. You can verify that the Pod was created by running:

kubectl get pods -n learning-tracker

You should see the newly created Pod in the list of Pods in the learning-tracker Namespace. The output should look similar to this:

NAME              READY   STATUS    RESTARTS   AGE
learning-tracker   1/1     Running   0          5s

You can also describe the Pod to see more details about it:

kubectl describe pod learning-tracker -n learning-tracker

You should see detailed information about the Pod, such as its status, containers, and events. This is a good way to check if the Pod is running correctly and if there are any issues. The 1/1 in the READY column indicates that the Pod has one container, and it is ready to serve requests.

Now that we created our first Pod, let's remove it again and create a manifest for it. As you might remember from the container basics lab, our app is able to log messages to the console, so we will also add this environment variable to the Pod manifest.

Creating a manifest for the Pod

Until now, we didn't work with manifests and only created objects imperatively. As mentioned earlier, it's a good practice to create manifests for your objects and store them at a safe place, such as a version control system. This way, you can easily recreate the object or share it with others

The good news is that you can simply extend the command we used to create the Pod to generate a manifest for it. You can do this by adding the --dry-run=client -o yaml flags to the command and redirecting the output to a file. Run the following command in your terminal:

kubectl run learning-tracker --image=ghcr.io/tsclabs-eu/learning-tracker:v1.0.5 --dry-run=client -o yaml > learning-tracker-pod.yaml

The --dry-run=client flag tells kubectl to only simulate the creation of the Pod and not actually create it, while the -o yaml flag tells it to output the manifest in YAML format. The output is then redirected to a file named learning-tracker-pod.yaml in your current directory. This file contains the manifest for the Pod we just created. You can open it in your text editor or IDE to inspect its contents. Let's take a look at the contents of the learning-tracker-pod.yaml file:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: learning-tracker
  name: learning-tracker
spec:
  containers:
  - image: ghcr.io/tsclabs-eu/learning-tracker:v1.0.5
    name: learning-tracker
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Basically, you see some sections that are common to all Kubernetes manifests, such as apiVersion, kind, and metadata. The spec section contains the specification of the Pod, including the containers that should be run in it. You can see that the Pod has a single container named learning-tracker that uses the image ghcr.io/tsclabs-eu/learning-tracker:v1.0.5. Some fields like creationTimestamp appear with null values, Kubernetes will fill these automatically when the Pod is created.

Now, let's make our first change to the manifest and add an environment variable to the container that enables logging to the console. Open the learning-tracker-pod.yaml file in your text editor or IDE and add the following section under the containers section:

[...]
  containers: 
  - name: learning-tracker
    env:
    - name: LOG_OUTPUT
      value: "console"  
    [...]

Add only the env section; leave the rest of the manifest unchanged. This will add an environment variable named LOG_OUTPUT with the value console to the container. This way, our application will log messages to the console, which we can later inspect.

Before we apply the manifest, let's remove the Pod we created earlier. You can do this by running the following command:

kubectl delete pod learning-tracker

Take a moment to find out if the Pod was deleted successfully by running:

kubectl get pods

If you see no Pods in the output, you have successfully deleted it. Now, we can apply the manifest we created earlier to create the Pod with the environment variable again.

You can do this by running the following command:

kubectl apply -f learning-tracker-pod.yaml

This command will create the Pod defined in the learning-tracker-pod.yaml file. You can verify that the Pod was created by running:

kubectl get pods -n learning-tracker

You should see the newly created Pod in the list of Pods in the learning-tracker Namespace again.

🎉First Pod

Congratulations! You have successfully created your first Pod in Kubernetes and learned how to create a manifest for it. You also added an environment variable to the container that enables logging to the console. Now, let's inspect the Pod and see how we can interact with it.