Skip to main content

Working with Pods

Simple Nginx Pod​

Let's create a simple pod with Nginx container. We can create this in two ways - imperative or declarative (manifests).

Imperative command

kubectl run nginx-pod --image=nginx

kubectl run nginx-pod --image=nginx pod/nginx-pod created

Manifest

apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx-container
image: nginx

kubectl apply -f 1-pod.yaml pod/nginx-pod created

Once the Pod is created, check the status:

kubectl get pods

kubectl get pods NAME READY STATUS RESTARTS AGE nginx-pod 1/1 Running 0 10m

Pod with Env​

Let's create a pod with an environment variable. We can create this in two ways - imperative or declarative (manifests).

Imperative command

kubectl run sample-pod --image=nginx --env="TEST_ENV=Hello!"

Manifest

apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx-container
image: nginx
env:
- name: TEST_ENV
value: "Hello!"

How do we check if this environment variable is set in the container?

Normally on a server, you set an environment variable and execute the command echo $TEST_ENV to verify. Below is a way to verify the same in kubernetes.

kubectl exec sample-pod -c nginx-container -- sh -c 'echo $TEST_ENV'

kubectl exec sample-pod -c nginx-container -- sh -c 'echo $TEST_ENV' Hello!

The -- in the command separates the kubectl command and the command to run inside the container. You may ask that we can directly run the command echo $TEST_ENV after the --.

If you run:

kubectl exec sample-pod -c nginx-container -- echo $TEST_ENV

we get an empty output. It will check for the TEST_ENV on the host. But the variable is set inside the container. So, we pass the actual command as arguments to the sh -c.