A pod is the lowest and most basic container deployment and management unit in Kubernetes. Pods are a logical host for one or more containers that are connected through the localhost interface and share the same network namespace. When using Kubernetes, you will inevitably be creating pods when getting your applications up and running, and then managing your pods to make sure that your hosted applications are running as expected.
In this tutorial, we will learn how to create and manage a pod in Kubernetes on a Linux system. There are many actions within Kubernetes that will spawn pods, so we will look at a basic way to create one, and then see various
kubectl commands that can be used to interact with the pod and manage it accordingly.
In this tutorial you will learn:
- How to create and manage a pod in Kubernetes
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Create and Manage a Pod in Kubernetes step by step instructions
Before getting started, this tutorial assumes that you have already installed Kubernetes and began creating a Kubernetes cluster, whether you have a production ready configuration or just a single node test system.
Creating a Pod
- One of the easiest ways to quickly create a pod is imperatively, which will download the required image from Docker hub. The
kubectl runsyntax will do that for us. In this example, we will create a pod for running Nginx:
$ kubectl run --image=nginx nginx-server --port=80
- Another method is called declaratively, which requires us to create a YAML file with all of the deployment settings. Here is an example:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
Then, we create the pod from this configuration file by executing:
$ kubectl create -f nginx-deployment.yaml
- Another option is to use the Helm package manager to download an image and create a pod for us. For example:
$ helm repo add bitnami https://charts.bitnami.com/bitnami $ helm install my-nginx bitnami/nginx
All of these methods will spawn one or more pods (depending on your configuration) to accommodate the image you are trying to run. From there, you can also create a service if you want your applications to be accessible for clients over the network.
Managing a Pod
A pod can be managed via the
kubectl command. Here are some examples:
- Retrieve information about all of the Kubernetes pods in your network with:
$ kubectl get pods
Or the more verbose version, which will show pods in all name spaces:
$ kubectl get pods --all-namespaces
- Get details about a specific pod that has been deployed (in this example, one named ‘apache’):
$ kubectl get pods/apache
And more detailed information with the
$ kubectl describe pods/apache
- See the logs for a specific pod:
$ kubectl logs my-pod
- To delete a pod:
$ kubectl delete pod nginx
In this tutorial, we saw how to create and manage a pod in Kubernetes on a Linux system. As we learned, there are several ways to create pods in Kubernetes, which are the smallest part of a cluster. We also saw some basic kubectl commands that can be used to interact with and manage our Kubernetes pods. Many more tools and commands exist, such as the ability to check CPU and RAM usage of a Kubernetes pod.