# How to create Kubernetes pods

Kubernetes pods are configured **imperatively, declaratively or using API interfaces**. You can also specify and customize additional resources for the containers included.

## Requirements for creating a Kubernetes pod

To run a [Kubernetes pod](https://www.ionos.com/digitalguide/server/configuration/kubernetes-pods/), you need a running **Kubernetes cluster**, either locally on a development system, in a cloud or onsite. For installation, we recommend the [Kubernetes tutorial](https://www.ionos.com/digitalguide/server/configuration/kubernetes-tutorial/). In addition, the kubectl command line tool should be available and set up on the computer to enable interaction with the Kubernetes cluster. You’ll also need to check whether the kubeconfig file is configured correctly in order to establish a successful connection to the cluster.

Tip [Managed Kubernetes](https://cloud.ionos.com/managed/kubernetes "Managed Kubernetes from IONOS") from IONOS ensures the high availability of your K8s deployments while optimizing costs. Integration into the IONOS cloud ecosystem also provides an advanced, fully integrated persistent data store.

## How to create a Kubernetes pod step by step

There are three ways to create pods in Kubernetes:

1. Imperative configuration
2. Declarative configuration
3. Using an API interface

### Imperative configuration

With the imperative method, you give the system explicit instructions via the `kubectl` command line on how to create a Kubernetes pod without having prepared a detailed configuration file beforehand.

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

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

Since the imperative method makes changes directly without a clear definition of the intended state, the declarative approach is generally recommended.

### Declarative configuration

The declarative approach in Kubernetes requires the desired state of the resources to be defined using **YAML configuration files**.

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

```yaml
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
```

Use the `kubectl apply` command to activate the Kubernetes pod based on the declarative configuration.

```shell
kubectl apply -f nginx-pod.yaml
```

The declarative configurations 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 transparent management and monitoring of resources in your Kubernetes cluster.

### Using an API interface

Kubernetes provides a **RESTful API** that you can use to interact with the Kubernetes cluster. Before using the API, you must authenticate and authorize yourself. This is usually done by providing access tokens or certificates, depending on the configuration of the Kubernetes cluster.

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

```shell
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
```

To activate the Kubernetes pod, use `curl` to communicate with the REST API of the Kubernetes cluster:

```shell
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
```

This `curl` command sends a **HTTPS-POST request** to a Kubernetes cluster endpoint. The options `-k` and `-v` stand for ignoring the SSL certificate check and verbose mode. A POST request is defined with `-X` POST. The **Authorization** 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:

```shell
kubectl get pods
```

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

For more detailed information, enter the command below:

```shell
kubectl describe pod my-pod
```

This command returns detailed information about the pod, including configuration, events and state transitions.

To view the **logs** of the main container in the Kubernetes pod:

```shell
kubectl logs my-pod
```

If there are several containers, you can mark the container with the `-c` option.

## Deleting a pod

Deleting pods is straightforward and can be done with a simple command.

```shell
kubectl delete pod nginx
```

This stops the selected Kubernetes pod, and the associated container is also shut down. In this example, we have successfully removed the Nginx pod from the cluster.


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/create-kubernetes-pods/](https://www.ionos.com/digitalguide/server/configuration/create-kubernetes-pods/) for AI/LLM consumption.