How to Create and Manage a Pod in Kubernetes

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
How to Create and Manage a Pod in Kubernetes
How to Create and Manage a Pod in Kubernetes
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Kubernetes
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – 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

  1. One of the easiest ways to quickly create a pod is imperatively, which will download the required image from Docker hub. The kubectl run syntax will do that for us. In this example, we will create a pod for running Nginx:
    $ kubectl run --image=nginx nginx-server --port=80
    
  2. 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
    
  3. 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:

  1. 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
    
  2. 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 describe argument:

    $ kubectl describe pods/apache
    
  3. See the logs for a specific pod:
    $ kubectl logs my-pod
    
  4. To delete a pod:
    $ kubectl delete pod nginx
    

Closing Thoughts




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.



Comments and Discussions
Linux Forum