Day-36: Exploring Service Discovery Options for Nginx in Kubernetes: DNS and Environment Variables:

Day-36: Exploring Service Discovery Options for Nginx in Kubernetes: DNS and Environment Variables:

Introduction:

Kubernetes is a powerful container orchestration platform that allows you to easily deploy, scale, and manage containerized applications. In this tutorial, we'll walk through the steps required to deploy a simple Nginx web server on Kubernetes, and then show you how to access the web server using DNS.

Step 1: Create a Pod:

The first step is to create a Pod that will run the Nginx container. Here's an example YAML file that defines a Pod called nginx-pod:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-nginx
  labels:
    app: ubuntu-nginx
spec:
  containers:
  - name: ubunginx
    image: ubuntu
    command: ["sh", "-c", "apt-get update && apt-get install -y nginx && nginx -g 'daemon off;'"]
    ports:
    - containerPort: 80

This YAML file creates a Pod called nginx-pod that runs a single container based on the nginx image. The container listens on port 80.

To create the Pod, you can use the following command:

DevSecOps Series

$ kubectl create -f pod1.yml

Step 2: Create a Service:

apiVersion: v1
kind: Service
metadata:
  name: nodepod
spec:
  selector:
    app: ubuntu-nginx
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP

This YAML file creates a Service called nginx-service that selects Pods with the app=nginx label. The Service exposes port 80 and maps it to the target port 80 of the selected Pods. The type of the Service is ClusterIP, which means that it's only accessible within the cluster.

To create the Service, you can use the following command:

Step 3: Create Another Pod:

Create a pod here but not in default namespace i create namespace 'Abhijeet' here

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu
  namespace: abhijeet
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    command: ["sleep", "infinity"]

Now Test this with Cluster IP:

curl http://<clusterIP>

Step 4: Use DNS to Access the Nginx Service:

curl <servicename>.default.svc.cluster.local

Using Environment Variables for Service Discovery in Kubernetes:

Introduction:

Kubernetes is an open-source container orchestration platform that allows you to automate the deployment, scaling, and management of containerized applications. Service discovery is an essential aspect of managing applications in Kubernetes, as it enables the dynamic discovery of services and their endpoints. In this article, we will explore how to use environment variables for service discovery in Kubernetes.

Step 1: Create a Pod Let's create a Pod named "ubuntu-nginx" that runs an Nginx server:

apiVersion: v1
kind: Pod
metadata:
  name: ubuntu-nginx
  labels:
    app: ubuntu-nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80

Save this configuration file as "ubuntu-nginx.yaml" and create the Pod using the following command:

kubectl apply -f ubuntu-nginx.yaml

Step 2: Create a Service Now let's create a Service to expose the Nginx Pod:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: ubuntu-nginx
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80

Save this configuration file as "nginx-service.yaml" and create the Service using the following command:

The Shipyard Salaries | Glassdoor

kubectl apply -f nginx-service.yaml

Step 3: Use Environment Variables for Service Discovery To use environment variables for service discovery in Kubernetes, we need to set the environment variables in the Pod's container. In our case, we need to set the environment variable "NGINX_SERVICE_HOST" to the Service's ClusterIP and "NGINX_SERVICE_PORT" to the Service's port.

We can set these environment variables in the Pod's container by updating the Pod's configuration file as follows:so final file will be this , use this one to create pod and run it with kubectl apply -f ubuntu-nginx-env.yaml , Ignore Step-1

 apiVersion: apps/v1
kind: Deployment
metadata:
  name: ubuntu-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ubuntu-nginx
  template:
    metadata:
      labels:
        app: ubuntu-nginx
    spec:
      containers:
      - name: ubunginx
        image: ubuntu
        command: ["sh", "-c", "apt-get update && apt-get install -y nginx && nginx -g 'daemon off;'"]
        ports:
        - containerPort: 80
        env:
        - name: NGINX_SERVICE_HOST
          value: nginx-service
        - name: NGINX_SERVICE_PORT
          value: "80"

Save this configuration file as "ubuntu-nginx-env.yaml" and apply the changes using the following command:

kubectl apply -f ubuntu-nginx-env.yaml

Step 4: Test Environment Variables To test if the environment variables are set correctly, we can use the "env" command in the Pod's container:

kubectl exec -it ubuntu-nginx -- env

Step 5: Access Nginx Server Now that we have set the environment variables for service discovery, we can access the Nginx server from within the Pod's container using the environment variables:

curl $NGINX_SERVICE_HOST:$NGINX_SERVICE_PORT