# Kubernetes - Pods

Kubernetes Pods are the smallest and most fundamental units in the Kubernetes object model that you can create or deploy. A pod represents a single instance of a running process in your cluster. In this guide, we'll explore the different aspects of creating and managing Kubernetes pods, including single and multi-container pods, namespaced pods, troubleshooting, and setting resource requests and limits.

**<mark>1. Creating a Pod with a Single Container</mark>**

Creating a pod with a single container is the most basic operation in Kubernetes. Here’s an example configuration to create a pod named `single-container-pod` that runs an NGINX container.

**YAML Configuration:**

```plaintext
apiVersion: v1
kind: Pod
metadata:
  name: single-container-pod
spec:
  containers:
  - name: cont1
    image: nginx:latest
    ports:
    - containerPort: 80
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274249553/86baa9dc-acc1-41e0-a536-3a79fb40d8da.png align="center")

Command to Apply the Configuration:

```plaintext
kubectl apply -f single-container-pod.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274533676/65cdf9ff-aff1-4cbb-9460-fbe3d58259d7.png align="center")

**Verifying the Pod Creation:**

After applying the configuration, you can verify the pod creation with the following commands:

```plaintext
kubectl get pods
kubectl get pods -n default
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274645443/c2f22d38-e364-4af8-b49c-5e8d1ccd53fc.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274604160/ad12a80d-119c-4179-b516-709d74a77649.png align="center")

#### **Checking the Node of the Pod:**

To check which worker node a specific pod is running on, use:

```plaintext
kubectl get pods -o wide
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724274684342/28603a0a-27d7-48b6-8474-ca4681608707.png align="center")

**<mark>2. Creating a Pod in a Specific Namespace</mark>**

Namespaces in Kubernetes help separate resources and manage environments more effectively. Here's how you can create a pod in a specific namespace:

**YAML Configuration:**

```plaintext
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: dev
spec:
  containers:
  - name: cont1
    image: httpd
    ports:
    - containerPort: 80
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275216622/41622717-0ae1-4b14-9a72-d5a67f0ba905.png align="center")

Command to Apply the Configuration:

```plaintext
kubectl apply -f  dev-namespaced-pod.yaml
```

This will create a pod named `my-pod` in the `dev` namespace, running an HTTPD container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275283164/9576e95c-b35a-4c68-8df7-184b6c3c8857.png align="center")

Checking the Pod Status within the Namespace:

```plaintext
kubectl get pods -n dev
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275338439/fd2aadfa-074f-4c1b-a66e-f9b8a922ebe3.png align="center")

#### **Checking the Node of the Pod in the Namespace:**

To see which worker node the pod is running on, use:

```plaintext
kubectl get pods -n dev -o wide
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275390465/89037ab7-c42b-42fd-88a9-e241098479c7.png align="center")

**<mark>3. Creating a Pod with Multiple Containers</mark>**

Kubernetes pods can contain multiple containers that share the same network namespace and storage. Below is an example configuration for creating a pod with two containers:

**YAML Configuration:**

```plaintext
apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: cont1
    image: httpd
    ports:
    - containerPort: 80
  - name: cont2
    image: nginx
    ports:
    - containerPort: 80
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275503842/fbe91daf-aaca-4a94-a9ed-387e65757be3.png align="center")

**Command to Apply the Configuration:**

Save the configuration as `multi-container-pod.yaml` and run:

```plaintext
kubectl apply -f multi-container-pod.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275557544/3b504153-c3bb-43ab-8df8-ec475fec3129.png align="center")

This pod (`multi-container-pod.yaml`) will run both HTTPD and NGINX containers, each exposing port 80.

**<mark>4. Troubleshooting Pod Containers</mark>**

When issues arise, Kubernetes offers tools to help troubleshoot pods. Here's how to gather detailed information:

**Detailed Pod Information:**

To get detailed information about a pod, use:

```plaintext
kubectl describe pod <pod-name>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275664374/4932c8f9-2d37-4c39-8d8c-b08eaaabd763.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275878590/77e62c52-45cb-4499-9a2e-ad4c3089e42e.png align="center")

**Access Container Logs:**

To view the logs of a specific container within a pod:

```plaintext
kubectl logs <pod-name> <container-name>
```

These commands provide valuable insights into the pod’s status and the container's logs, helping you diagnose issues effectively.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724275983400/4ef4807e-8982-480f-a420-3c1c421da655.png align="center")

**<mark>5. Setting Resource Requests and Limits</mark>**

To ensure efficient resource utilization, it's essential to specify resource requests and limits for your containers. Below is an example configuration:

**YAML Configuration:**

```plaintext
apiVersion: v1
kind: Pod
metadata:
  name: resource-limits-pod
spec:
  containers:
  - name: cont1
    image: nginx:latest
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
```

**Command to Apply the Configuration:**

Apply the configuration by running:

```plaintext
kubectl apply -f resource-limits-pod.yaml
```

This configuration ensures that the NGINX container in `resource-limits-pod.yaml` requests 64Mi of memory and 250m of CPU, with limits set at 128Mi of memory and 500m of CPU
