Kuber­netes pods are con­figured im­per­at­ively, de­clar­at­ively or using API in­ter­faces. You can also specify and customise ad­di­tion­al resources for the con­tain­ers included.

Re­quire­ments for creating a Kuber­netes pod

To run a Kuber­netes pod, you need a running Kuber­netes cluster, either locally on a de­vel­op­ment system, in a cloud or onsite. For in­stall­a­tion, we recommend the Kuber­netes tutorial. In addition, the kubectl command line tool should be available and set up on the computer to enable in­ter­ac­tion with the Kuber­netes cluster. You’ll also need to check whether the kube­con­fig file is con­figured correctly in order to establish a suc­cess­ful con­nec­tion to the cluster.

Tip

Managed Kuber­netes from IONOS ensures the high avail­ab­il­ity of your K8s de­ploy­ments while op­tim­ising costs. In­teg­ra­tion into the IONOS cloud ecosystem also provides an advanced, fully in­teg­rated per­sist­ent data store.

How to create a Kuber­netes pod step by step

There are three ways to create pods in Kuber­netes:

  1. Im­per­at­ive con­fig­ur­a­tion
  2. De­clar­at­ive con­fig­ur­a­tion
  3. Using an API interface

Im­per­at­ive con­fig­ur­a­tion

With the im­per­at­ive method, you give the system explicit in­struc­tions via the kubectl command line on how to create a Kuber­netes pod without having prepared a detailed con­fig­ur­a­tion file be­fore­hand.

kubectl run nginx --image=nginx --restart=Never
shell

This command creates a single pod named nginx that contains the Nginx web server.

Since the im­per­at­ive method makes changes directly without a clear defin­i­tion of the intended state, the de­clar­at­ive approach is generally re­com­men­ded.

De­clar­at­ive con­fig­ur­a­tion

The de­clar­at­ive approach in Kuber­netes requires the desired state of the resources to be defined using YAML con­fig­ur­a­tion files.

Open a text editor and create a YAML file, for example nginx-pod.yaml, which describes the desired state of the Nginx pod.

apiVersion: apps/v1
kind: Deployment
metadata:
    name: nginx-deployment
spec:
    selector:
        matchLabels:
            app: nginx
    minReadySeconds: 5
    template:
        metadata:
            labels:
                app: nginx
        spec:
            containers:
            - name: nginx-container
                image: nginx:latest
                ports:
                - containerPort: 80
yaml

Use the kubectl apply command to activate the Kuber­netes pod based on the de­clar­at­ive con­fig­ur­a­tion.

kubectl apply -f nginx-pod.yaml
shell

The de­clar­at­ive con­fig­ur­a­tions you specify in the YAML files are a concrete record of the intended state, including the version of the pod to be created. This leads to trans­par­ent man­age­ment and mon­it­or­ing of resources in your Kuber­netes cluster.

Using an API interface

Kuber­netes provides a RESTful API that you can use to interact with the Kuber­netes cluster. Before using the API, you must au­then­tic­ate and authorise yourself. This is usually done by providing access tokens or cer­ti­fic­ates, depending on the con­fig­ur­a­tion of the Kuber­netes cluster.

Here’s an example of how to create a JSON file for an Nginx pod using the REST API.

cat > nginx-pod.json <<EOF
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "name": "nginx-deployment"
    },
    "spec": {
        "selector": {
            "matchLabels": {
                "app": "nginx"
            }
        },
        "minReadySeconds": 5,
        "template": {
            "metadata": {
                "labels": {
                    "app": "nginx"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "nginx-container",
                        "image": "nginx:latest",
                        "ports": [
                            {
                                "containerPort": 80
                            }
                        ]
                    }
                ]
            }
        }
    }
}
EOF
shell

To activate the Kuber­netes pod, use curl to com­mu­nic­ate with the REST API of the Kuber­netes cluster:

curl -k -v -X POST -H "Authorization: Bearer <JWT_TOKEN>" -H "Content-Type: application/json" https://cluster-ip:6443/api/v1/namespaces/default/pods -d@nginx-pod.json
shell

This curl command sends a HTTPS-POST request to a Kuber­netes cluster endpoint. The options -k and -v stand for ignoring the SSL cer­ti­fic­ate check and verbose mode. A POST request is defined with -X POST. The Au­thor­iz­a­tion header contains a bearer token (replace <JWT_TOKEN> with the actual token), and Content-Type specifies the data format type as JSON. The option -d@nginx-pod.json sends the data to the server.

View pods

To check the current status of all pods in the namespace, enter the following command:

kubectl get pods
shell

The result is a list of existing pods and their status, start time and other details.

For more detailed in­form­a­tion, enter the command below:

kubectl describe pod my-pod
shell

This command returns detailed in­form­a­tion about the pod, including con­fig­ur­a­tion, events and state trans­itions.

To view the logs of the main container in the Kuber­netes pod:

kubectl logs my-pod
shell

If there are several con­tain­ers, you can mark the container with the -c option.

Deleting a pod

Deleting pods is straight­for­ward and can be done with a simple command.

kubectl delete pod nginx
shell

This stops the selected Kuber­netes pod, and the as­so­ci­ated container is also shut down. In this example, we have suc­cess­fully removed the Nginx pod from the cluster.

IONOS Cloud Managed Kuber­netes
Container workloads in expert hands

The ideal platform for demanding, highly scalable container ap­plic­a­tions. Managed Kuber­netes works with many cloud-native solutions and includes 24/7 expert support.

Go to Main Menu