Skip to content

Working with Pods

It's time to learn how to work with pods, the KubeCTL command-line utility, and the Deployment Flow. But that's not all, because you also have an overview of such exciting topics as liveness, readiness, and startup probes used in Kubernetes for pod verification.

Creating and Interacting with Pods

There are two ways to create objects in Kubernetes:

  1. Imperative (object descriptions are added directly in the terminal).
  2. Declarative (creating special manifest files from which the resource is created).

Let's look at each of them ⬇️

Imperative Method of Creating Objects

First, you need to check if there are any pods in the namespace that was created earlier. The following command is used for this:

kubectl get pods -n mateapp

Here flag -n sets the namespace in which the command will be executed.

Now look at the inline command that creates a pod:

cat << EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
    name: nginx
    namespace: mateapp
spec:
    containers:
    - name: nginx
      image: nginx
EOF

Using the cat << EOF command, a file is created whose content is everything between the two EOF markers. The pipe | is a pipe operator in Unix that passes the result of the command to the left of | as input to the command on the right — kubectl create -f -.

💡 The kubectl -f command creates resources, and the -f flag allows you to specify the file in which the resource is described. If a dash follows the -f flag, it means that instead of a standard file in the filesystem, the file should be read from stdout.

File contents:

  • apiVersion: v1 — the API version of the resource, i.e., the pod;
  • kind: Pod — the type of resource you want to create;
  • metadata — a mandatory resource description attribute that includes its name; you can also specify the namespace where the resource should be created; if you don't explicitly specify a namespace here or in the kubectl command, the resource will be created in the default namespace default;
  • specification, which describes the pod itself, includes:
  • containers — a mandatory field with a list of containers for this pod;
  • name (nginx);
  • image (nginx) — the official nginx container.

To check pods, use the kubectl get pods -o wide command, which will display information about all pods and their IP addresses.

Declarative Method of Creating Objects

First, let's create a file pod.yml (for better understanding we named it this way, but in fact it can be named anything). This file will contain the pod description from the previous section about the imperative method:

apiVersion: v1
kind: Pod
metadata:
 name: nginx
 namespace: mateapp
spec:
 containers:
 - name: nginx
   image: nginx

Creating this resource using kubectl is done in the terminal:

kubectl apply -f pod.yml

(apply - creates the pod, and if the pod already exists, it applies changes to the existing one; create - creates, but if it already exists, it returns an error)

Checking pods from the mateapp namespace is done as follows:

kubectl get pods -n mateapp

The command for displaying detailed information about a pod:

kubectl describe pod nginx -n mateapp

The result of executing this command will be detailed information about the resource:

  • status;
  • IP;
  • the node on which the resource is running;
  • container information.

💡 The kubectl describe {resourceKind} command also outputs information about various resources.

Pod Interaction

To test communication between pods, you will need another pod with a busybox container inside. This container will also have the curl tool installed to make HTTP requests. The manifest of this pod will look like this:

apiVersion: v1
kind: Pod
metadata:
    name: busybox
    namespace: mateapp
spec:
    containers:
    - name: busybox
      image: curlimages/curl:8.11.1
      args:
      - sleep
      - "1000"

As you can see, this file is similar to the one you already created — only the container specification has changed. The following command will be used to create the pod:

kubectl apply -f busybox.yml

Like in Docker, you can connect to a container and execute Shell commands directly from it. Before connecting to the busybox container, we need to find out the IP address of the other pod — the one we will call with the curl command. We find the IP address using the command:

kubectl get pods -n mateapp -o wide

Next, you need to connect to busybox using the command:

kubectl -n mateapp exec -it busybox -- sh

You will find yourself inside the busybox container. All that's left is to execute the curl 10.1.0.7 command. In response, you will receive HTML markup. By default, nginx listens on port 80, and curl also makes requests to port 80 by default: