DEV Community

Jution Candra Kirana
Jution Candra Kirana

Posted on

Kubernetes: Pod

Before continuing, make sure you have installed the kubernetesextension shown below:

Kuberneted Extension

Once you have it installed, create a new file. Here, we will create a file named pod.yaml. Since you have installed the Kubernetes extension, you don't need to write everything out manually. Just type pod and then press tab, and a pod template will appear as follows:

apiVersion: v1
kind: Pod
metadata:
  name: myapp
  labels:
    name: myapp
spec:
  containers:
    - name: myapp
      image: <Image>
      resources:
        limits:
          memory: '128Mi'
          cpu: '500m'
      ports:
        - containerPort: <Port>
Enter fullscreen mode Exit fullscreen mode

There's no need to memorize the above structure. If you've learned docker, this will be familiar.

In this practice, we will create a pod with the nginx container:

apiVersion: v1
kind: Pod
metadata:
  name: sample-nginx
  labels:
    name: sample-nginx
spec:
  containers:
    - name: sample-nginx
      image: nginx:alpine
      resources:
        limits:
          memory: '10Mi'
          cpu: '10m'
      ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Next, to create the pod defined above, use the commands explained earlier in the POD section.

So, the command will be like this (the pod.yaml file in this practice is located in the sample folder):

kubectl apply -f sample/pod.yaml
Enter fullscreen mode Exit fullscreen mode

If successful, you will see the following information:

pod/sample-nginx created
Enter fullscreen mode Exit fullscreen mode

Then, check the pod using the following command:

kubectl get pod

NAME           READY   STATUS               RESTARTS   AGE
sample-nginx   0/1     ContainerCreating    0          89s
Enter fullscreen mode Exit fullscreen mode

Focus on the STATUS. Here are some common statuses that may appear:

  • ContainerCreating: Kubernetes is preparing the container for the pod. This process includes pulling the required image (like nginx:alpine), creating the container, and connecting it to the cluster's network. If the pod remains in this status for too long, there may be issues downloading the image or starting the container.
  • Running: The pod is running, and the container inside it is active. This status indicates that everything is functioning normally.
  • Pending: The pod is not yet ready to run because one of the preparation steps (like scheduling or image pulling) has not been completed. This usually happens if there are issues scheduling the pod to a node.
  • Succeeded: The pod has completed successfully, usually used for pods with jobs or cron jobs that have finished executing their tasks.
  • Failed: The pod has failed to run. The reasons can vary, such as configuration errors or issues with the container itself.
  • CrashLoopBackOff: The pod continuously fails to start, and Kubernetes is trying to restart the container. This indicates that the container is encountering problems each time it attempts to run.

Next, you can check the details of the created container by running:

kubectl describe pod sample-nginx
Enter fullscreen mode Exit fullscreen mode

After hitting [enter], you will see detailed information about the container.

Next, we will try port-forwarding, which should have been covered in the docker material.

kubectl port-forward pod/sample-nginx 8080:80

Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Enter fullscreen mode Exit fullscreen mode

Please open your browser to access nginx. Since we have done port-forward, the access will no longer be to port :80 but to port :8080. The complete URL will be localhost:8080, and the result will be as follows:

Nginx Run on Web

Top comments (0)