Kubectl Create Pods: How to Create Pods in Kubernetes

How to Create Pods in Kubernetes

A pod is a group of one or more containers with shared storage/network resources and a specification for how to run the containers. Pods allow you to run multiple containers that need to work together. This makes it easier to manage related processes, as they can share the same lifecycle and network namespace.

In this tutorial, we’ll cover different methods to create pods in the Kubernetes cluster.

Method 1: Creating a Pod Using kubectl create

The simplest way to get started is to create a pod using the kubectl create command. This method allows you to create a pod with minimal configuration.

1. Open your terminal and run the kubectl create command to create a new pod.

 # kubectl create pod mypod --image=nginx

This command creates a pod named mypod using the nginx image.

pod/mypod created

2. To verify that the pod was created successfully, run kubectl get pods command:

 # kubectl get pods

Output.

NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          10s

3. To get detailed information about the pod, use the kubectl describe command:

 # kubectl describe pod mypod

Output.

Name:         mypod
Namespace:    default
...
Containers:
  mypod:
    Container ID:   docker://1234567890abcdef
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:1234567890abcdef
    Port:           
    Host Port:      
    State:          Running
    Started:        Tue, 01 Jan 2024 00:00:00 +0000
    Ready:          True
    Restart Count:  0
...

Method 2: Creating a Pod with YAML Configuration

Using a YAML configuration file gives you more control over the pod’s specifications. You can define the pod’s properties in detail, including labels, container specifications, and resource limits.

1. Create a YAML file named mypod.yaml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
  labels:
    app: myapp
spec:
  containers:
  - name: mycontainer
    image: nginx
    ports:
    - containerPort: 80

This YAML file defines a pod named mypod with a single container named mycontainer using the nginx image. The container exposes port 80.

2. Apply the YAML file to create the pod:

 # kubectl apply -f mypod.yaml

Output.

pod/mypod created

3. To verify that the pod was created, run:

 # kubectl get pods

Output.

NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          10s

Method 3: Creating a Pod Interactively

Creating a pod interactively allows you to define the pod’s properties through a guided process. This method is useful if you prefer a step-by-step approach rather than writing a YAML file.

1. Run the kubectl run interactive command to generate a YAML configuration file:

 # kubectl run mypod --image=nginx --dry-run=client -o yaml > mypod-interactive.yaml

This command generates a YAML configuration for the pod and saves it to mypod-interactive.yaml. The –dry-run=client flag ensures that the command only simulates the creation of the pod and does not create it. Additionally, theĀ  -o yaml option outputs the configuration in YAML format.

2. Edit the generated YAML file if you want to customize the pod’s configuration. For example, you might want to add labels, specify resource limits, or configure environment variables.

3. Apply the YAML file to create the pod:

 # kubectl apply -f mypod-interactive.yaml

Output:

pod/mypod created

4. To verify that the pod was created, run:

 # kubectl get pods

Output.

NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          10s

Conclusion

In this article, we covered three methods to create pods in Kubernetes using kubectl. We started with the simplest approach using kubectl create, moved on to creating a pod with a YAML configuration, and finally explored the interactive method. Each method has its own use case and benefits, allowing you to choose the best approach based on your requirements.

FAQs

1. How do I create a pod in Kubernetes?

You can create a pod using a YAML file or directly via the command line.

2. Can I specify multiple containers in a single pod?

Yes, a pod can have multiple containers. All containers within a pod share the same network and storage.

3. What is the difference between a pod and a deployment in Kubernetes?

A pod is a single instance of an application, whereas a deployment manages a set of identical pods.

4. How can I create a pod in a specific namespace?

Use the -n flag with your command or specify the namespace in the YAML file.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva