DEV Community

chauhoangminhnguyen
chauhoangminhnguyen

Posted on • Updated on • Originally published at howtodevez.blogspot.com

Kubernetes Pod Cheatsheet

Introduction

This article is here to guide you on using Pod in Kubernetes. I'll give you some concepts and commands that Kubernetes often uses to handle Pod.

K8s Pod

Pod Introduction

A Pod is the smallest deployable unit in Kubernetes. Here are some key points about Pods:

  • Multiple Containers: A Pod can contain more than one container, and there's no limit to how many containers you can run inside a Pod. These containers are relatively tightly coupled and share resources such as disk.
  • Shared Resources: All the containers inside a Pod are connected via localhost and share the same memory space. They also share storage (volumes), IP address, and configuration information.
  • Unique IP Address: Each Pod gets a unique IP address.
  • Ephemeral Nature: Pods are ephemeral in nature; they can be created, deleted, and updated.

Pod detail

Prerequisites

Before we begin, make sure you have the following:

  • For Kubernetes systems, you can use minikube or have permissions to provision resources on cloud providers like Google, Amazon, Azure, etc.
  • You need permission to create a cluster or access an existing one.
  • In this guide, I'll be using Google Cloud Provider. If you're using the same provider, it'll be easier to follow along.
  • If you haven't installed gcloud and kubectl yet, refer to this guide for installation instructions, project setup, and cluster creation

Kubernetes commands

First, create a file named deployment.yml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

In the kind section, I'm defining a Pod named nginx. The spec section specifies the container to run, which is the nginx image, and this container will use port 80.

To create this Pod, execute the following command:

# only create, throw error if existed
kubectl create -f deployment.yaml

# recommand, create if not exist, update for existed
kubectl apply -f deployment.yml 
Enter fullscreen mode Exit fullscreen mode

To list the existing Pods, use the following command:

Get pod

To run a shell inside the Pod, use this command:
Exec shell

Inside the nginx container, you can use nginx features as needed.
Nginx
To exit shell mode, use the exit command.

To view the logs of the Pod, use the following command:
Log nginx

To get detailed information about the Pod, use this command:
Describe nginx

To forward a port from Kubernetes to localhost, use the following command:

kubectl port-forward po/{po name} {localhost port}:{container port}
Enter fullscreen mode Exit fullscreen mode

Forward port

Nginx homepage

To delete a Pod, use the following command:

kubectl delete po nginx

# clear all resource of k8s manifest
kubectl delete -f deployment.yml
Enter fullscreen mode Exit fullscreen mode

Delete pod

Conclusion

In this article, I've introduced some basic concepts and provided a few simple commands commonly used for managing Pods in Kubernetes. If you have any suggestions or questions regarding the content of the article, please don't hesitate to leave a comment below!


If you found this content helpful, please visit the original article on my blog to support the author and explore more interesting content.

BlogspotBlogspotDev.toFacebookX


Some series you might find interesting:

Top comments (0)