How to create Kubernetes pods
Kubernetes pods are configured imperatively, declaratively or using API interfaces. You can also specify and customise additional resources for the containers included.
Requirements for creating a Kubernetes pod
To run a Kubernetes pod, you need a running Kubernetes cluster, either locally on a development system, in a cloud or onsite. For installation, we recommend the 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.
Managed Kubernetes from IONOS ensures the high availability of your K8s deployments while optimising 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:
- Imperative configuration
- Declarative configuration
- 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.
kubectl run nginx --image=nginx --restart=Never
shellThis 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.
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
yamlUse the kubectl apply
command to activate the Kubernetes pod based on the declarative configuration.
kubectl apply -f nginx-pod.yaml
shellThe 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 authorise 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.
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
shellTo activate the Kubernetes pod, use curl
to communicate with the REST API of the Kubernetes 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
shellThis 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:
kubectl get pods
shellThe result is a list of existing pods and their status, start time and other details.
For more detailed information, enter the command below:
kubectl describe pod my-pod
shellThis 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:
kubectl logs my-pod
shellIf 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.
kubectl delete pod nginx
shellThis 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.
The ideal platform for demanding, highly scalable container applications. Managed Kubernetes works with many cloud-native solutions and includes 24/7 expert support.