Creating A Pod (Kubernetes)

A Kubernetes pod manifest to create nginx

I am currently learning more about Kubernetes and need to make sure I’m understanding it correctly, which means more blogs about Kubernetes in my wording for future reference.

MiniKube

I’m running Kubernetes locally on my MacBook via MiniKube. If you want to follow along please install MiniKube as per the notes at: https://kubernetes.io/docs/tasks/tools/install-minikube/

geektechstuff_k8-creating-a-pod2

Once installed Minikube can be started with the command minikube start , and the minikube dashboard can be launched using the command minikube dashboard , with minikube running I will be using kubectl to give commands to my Kubernetes cluster.

Pods

A pod in Kubernetes is a collection of containers and volumes running in the same execution environment. So if you have multiple applications in containers that rely on each other, think pods. If applications are running in the same pod, they share environment variables and IP address. If applications are running in different pods then they don’t share environment variables and IP addresses.

Pods Commands

  • kubectl get pods

Lists all the pods running.

  • kubectl run PODNAME –generator=run-pod/v1 –image=CONTAINER_IMAGE_NAME

Creates a pod named PODNAME using the container image named CONTAINER_IMAGE_NAME

  • kubectl delete pod PODNAME

Deletes the pod named PODNAME

  • kubectl describe pod PODNAME

Gives a detailed description about the pod named PODNAME

  • kubectl port-forward PODNAME HOST_PORT:CONTAINER_PORT

Forwards a port from the host into the container.

Pod Manifest

Rather than launching a pod via the command line with the various options/flags, the same details can be entered into a pod manifest. A pod manifest is written in YAML format and contains all the details needed to create the pod.

I am going to use the nginx container image to launch a pod running nginx.

A Kubernetes pod manifest to create nginx
A Kubernetes pod manifest to create nginx

apiVersion: v1

kind: Pod

metadata:

    name: nginx

spec:

    containers:

        - image: nginx:latest

          name: nginx

          ports:

            - containerPort: 80

              name: http

              protocol: TCP

With the pod manifest created, the pod can be launched via the command:
kubectl apply -f test_pod.yml
kubectl apply -f test_pod.yml
kubectl apply -f test_pod.yml

kubectl get pods will then show the pod and its status. The status may show as starting for a few minutes as the pod is starting up.

kubectl get pods
kubectl get pods

Further details about the pod can be viewed using the kubectl describe pods nginx command.

kubectl describe pods nginx
kubectl describe pods nginx
kubectl describe pods nginx
kubectl descrive pods nginx

As the pod contains nginx we can it a quick test by forwarding a host port into the container, in this case:

kubectl port-forward nginx 8080:80

kubectl port-forward nginx 8080:80
kubectl port-forward nginx 8080:80

And opening localhost:8080 (or 127.0.0.1:8080) in a web browser then brings up the default nginx webpage:

default nginx page
default nginx page

If you prefer a web interface then status of the pod can also be viewed via the Kubernetes / Minikube dashboard:

The Kuberenetes web interface
The Kuberenetes web interface

 

3 thoughts on “Creating A Pod (Kubernetes)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.