> For the complete documentation index, see [llms.txt](https://bagus-cahyono.gitbook.io/programming-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bagus-cahyono.gitbook.io/programming-notes/cka/03_pod.md).

# Pod

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

A Pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

Think of a Pod as a wrapper around containers, ensuring they work as a single unit.

## Single Container Pod

We can start or run a particular image in a pod using `kubectl run` command.

```bash
➜ kubectl run nginx --image=nginx --port=80
pod/nginx created
```

This command will create a pod named `nginx` will single container named `nginx` using image `nginx` and open port `80`. We can see the details using command `kubectl describe`.

```bash
➜ kubectl describe pod nginx 
Name:             nginx
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Tue, 04 Feb 2025 12:48:15 +0700
Labels:           run=nginx
Annotations:      <none>
Status:           Running
IP:               10.244.0.94
IPs:
  IP:  10.244.0.94
Containers:
  nginx:
    Container ID:   docker://6f804d723c05e62267a7b70896ec07dac53991f4e152874a8e713144c57c02d2
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0a399eb16751829e1af26fea27b20c3ec28d7ab1fb72182879dcae1cca21206a
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 04 Feb 2025 12:48:19 +0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6bc8f (ro)
...
```

We can also use `yaml` file to define pod configuration. The command `kubectl run` we previously use is more or less equal with this configuration below.

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
```

Explanation:

* `metadata.name: nginx`: The name of the Pod (must be unique in the namespace).
* `spec`: The specification of the Pod.
* `containers`: List of containers that will run inside this Pod.
* `- name: nginx`: The name of the container inside the Pod.
* `image: nginx`: The Docker image used to run the container (pulled from Docker Hub by default).
* `ports`: Defines which ports are opened on the container.
* `containerPort: 80`: The container listens on port 80.

To apply it as usual we can use `kubectl apply -f <file>` command.

## Multiple Container Pod

To create a pod that have multiple containers we just need to add new entries in `containers` section. Create new file `pod.yaml` and put example configuration below.

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-with-logger
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
  - name: logger
    image: busybox
    command: ["sh", "-c", "while true; do echo 'Logging...'; sleep 10; done"]
```

Apply the configuration file using `kubectl apply` and let see the details using `kubectl describe` command.

```bash
➜ kubectl apply -f pod.yaml 
pod/nginx-with-logger created
```

```bash
➜ kubectl describe pod nginx-with-logger 
Name:             nginx-with-logger
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Tue, 04 Feb 2025 13:49:17 +0700
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               10.244.0.95
IPs:
  IP:  10.244.0.95
Containers:
  nginx:
    Container ID:   docker://7ff16ce361c2acba92bc4e7a5ad4591c2f8943dc9fc766d8c89870a66e35d012
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0a399eb16751829e1af26fea27b20c3ec28d7ab1fb72182879dcae1cca21206a
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 04 Feb 2025 13:49:21 +0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-dmx2c (ro)
  logger:
    Container ID:  docker://a2bf248b0f9cad05d9e9af0e99fc273e479bda47018c43b406d3469ed93a88e8
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:a5d0ce49aa801d475da48f8cb163c354ab95cab073cd3c138bd458fc8257fbf1
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      while true; do echo 'Logging...'; sleep 10; done
    State:          Running
      Started:      Tue, 04 Feb 2025 13:49:26 +0700
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-dmx2c (ro)
...
```

As you can see above we have 2 containers with name `nginx` and `logger`.

We can get the container logs using this command below.

```bash
➜ kubectl logs pods/nginx-with-logger logger 
Logging...
Logging...
Logging...
Logging...
Logging...
Logging...
```

The command above will print logs from pod `nginx-with-logger` specifically in container `logger`. To print logs from all container we can add option `--all-containers=true`.

## Access the Pod

To access the pod we can use `kubectl port-forward` command.

```bash
➜ kubectl port-forward nginx-with-logger 8080:80  
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
```

This command will forward traffic from port `8080` in our local machine to pod named `nginx-with-logger` port `80`.

Lets try to access it using `curl` and we should see default response from nginx server.

```bash
➜ curl http://localhost:8080
```

```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
```

## Delete Pod

To delete pod we can use `kubectl delete pods <pod-name>` command.

```bash
➜ kubectl delete pods nginx
pod "nginx" deleted
```

## References

* <https://kubernetes.io/docs/concepts/workloads/pods/>
* <https://kubernetes.io/docs/reference/kubectl/generated/kubectl_run/>
* <https://kubernetes.io/docs/reference/kubectl/generated/kubectl_port-forward/>
* <https://kubernetes.io/docs/reference/kubectl/generated/kubectl_logs/>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bagus-cahyono.gitbook.io/programming-notes/cka/03_pod.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
