Create a Kubernetes POD based on Custom Specifications

Table of contents

No heading

No headings in the article.

This blog post is based on Real-time scenarios

Scenario

In this Scenario, you will assume the role of a DevOps engineer at an e-commerce startup. As part of the organization's annual technology audit, it has been advised to adopt Kubernetes.

Currently, the developer team manually provisions the application whenever a new server is launched, resulting in a sluggish deployment and update process. Your assignment is to create a proof-of-concept by building a POD that adheres to the provided specifications.

Objectives

  • Create Kubernetes Namespace

  • Generate Manifest of Pod

  • Fetch Pod Logs for Troubleshooting

  • Create Pod based on custom specifications

Requirement

  • Basic understanding of Pods

  • Basic Understanding of Linux

What you'll learn

  • Namespaces

  • DNS Configuration for Pods

  • Exposing Ports in Pods

  • Running Commands in Pod

Your project assignment:

Your Manager:

Hello Ganesh! Thank you for taking on this project for our team. The application team has encountered an issue where they have to manually configure the application environment and deploy it whenever a new server is launched. This process is time-consuming for both the developers and the overall software release cycle. To address this, our organization's technology consultant has suggested using Kubernetes. Your task is to create a POD that represents the application and develop a proof-of-concept to validate its feasibility.

The application team has provided a set of specifications that can be used to simulate the application running in the POD:

  • The container should be based on the busybox image.

  • The hostname of the container should be "app-pod".

  • Port 80 should be exposed.

  • All containers/PODs should be hosted in a separate and isolated namespace called "prod-env".

  • The DNS associated with the container/POD should always be set to "8.8.8.8".

  • To simplify the demonstration, the container should run the command "ping localhost" to mimic a sample application.

  • Once the application is running, the initial set of logs should be stored in a file named "app-logs.txt".

Since the application team prefers versioning all changes and uses Git extensively, it is recommended to create the POD using a YAML-based Manifest that can be versioned.

Project Tasks:

Task 1: Create a Production Namespace

  • Create a new namespace called "prod-env" to host production Pods.

Task 2: Generate a Base POD Manifest

  • Create a Pod Manifest that includes the basic Pod configuration according to the project overview requirements.

Task 3: Modify Pod Manifest to Include DNS Configuration

  • Update the Pod Manifest to include the necessary DNS configuration to ensure that the nameserver in the Pod always remains as "8.8.8.8".

Task 4: Create Pod from Pod Manifest

  • Create a Pod using the Pod Manifest you have created, ensuring that it runs in the "prod-env" namespace.

Task 5: Store Pod Logs to File

  • Verify if the Application Pod is generating the required logs and store them in a static file named "pod-logs.txt".

Solutions:

Task 1: Create a Production Namespace

$ mkdir kubernetes

$ cd kubernetes

1. Create a new namespace named prod-env which will be hosting all the production-related Pods.

Note: We can create Kubernetes resources with Imperatively and Declaratively I will show both of them.

$ kubectl create namespace prod-env

$ vi namespace.yml

apiVersion: v1
kind: Namespace
metadata:
  name: prod-env

$ kubectl create -f namespace.yml

$ kubectl get ns

Task 2: Generate a Base POD Manifest

$ kubectl run app-pod --image=busybox --command "ping" "localhost" --namespace prod-env --port=80 --dry-run=client -o yaml > app.yml

2. Verify the Base Manifest that has been created.

$ cat app.yml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: app-pod
  name: app-pod
  namespace: prod-env
spec:
  containers:
  - command:
    - ping
    - localhost
    image: busybox
    name: app-pod
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

3. Ensure whether the following field and values are set within the generated manifest.

name: app-pod
  namespace: prod-env
spec:
  containers:
  - command:
    - ping
    - localhost
    image: busybox

Note: At this stage, the DNS Specific Configuration is still not present in the Pod Spec. We will be adding it in the next step.

Task 3: Modify Pod Manifest to Include DNS Configuration

1. Open the manifest file using text editor

$ vi app.yml

2. Change the DNS Policy from ClusterFirst to None

3. Add dnsConfig to ensure nameserver is 8.8.8.8

  dnsConfig:
    nameservers:
      - 8.8.8.8

  1. Save the file and Exit

  2. Verify the final contents using the cat command

    $ cat app.yaml

     apiVersion: v1
     kind: Pod
     metadata:
       creationTimestamp: null
       labels:
         run: app-pod
       name: app-pod
       namespace: prod-env
     spec:
       containers:
       - command:
         - ping
         - localhost
         image: busybox
         name: app-pod
         ports:
         - containerPort: 80
         resources: {}
       dnsPolicy: "None"
       dnsConfig:
         nameservers:
           - 8.8.8.8
       restartPolicy: Always
     status: {}
    

Task 4: Create Pod from Pod Manifest

1. Create a Pod from the manifest.

$ kubectl apply -f app.yaml

  1. Verify if the Pod is created in the prod-env namespace.

$ kubectl get pods -n prod-env

Task 5: Store Pod Logs to File

1. Verify if logs associated with the app-pod are being generated.

$ kubectl logs app-pod -n prod-env

2. Store the current generated Logs to a Static file named pods-logs.txt

$ kubectl logs app-pod -n prod-env > pod-logs.txt

3. Verify the contents of the pod-logs.txt and ensure that the log entries are present.

$ cat pod-logs.txt

Your First Assignment is Completed Successfully.

Did you find this article valuable?

Support Ganesh Balimidi by becoming a sponsor. Any amount is appreciated!