Deploy Nginx Service in Kubernetes

To deploy Nginx in Kubernetes and make it accessible from external sources, you need to follow a few steps:

Nginx service Deployment

Prerequisites

You have to set the Kubernetes cluster to follow these links.

Access the Kubernetes Master Node

To access the master node in a Kubernetes cluster, follow these LINK:

Open a terminal session.

Use the SSH protocol to connect:

ssh -i access_key user@master-node-address

Replace user with your username and master-node-address with the IP address or hostname of the master node.

Enter your password or authenticate with your SSH key when prompted.

Once connected, you can perform administrative tasks on the Kubernetes master node.

Create a Deployment: Define a Kubernetes Deployment manifest to specify the desired state for the Nginx deployment.

Expose the Deployment: Expose the Nginx Deployment through a Kubernetes Service of type NodePort or LoadBalancer.

Access from External Sources: Access the Nginx service using the assigned NodePort or the external IP if you're using a LoadBalancer.

Here is a step-by-step guide

First, check your nodes Check the status of your nodes

Create a Deployment YAML File (deployment.yaml)

vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80

Create a Service YAML File (service.yaml)

vi service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Apply the YAML file to create the Nginx Service

Once you have created this manifest (deployment.yaml , service.yaml for example ), you can apply it to your Kubernetes cluster using the following command:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
  • Check the status of your service using:

kubectl get services 
  • Once the external IP is assigned, you can use it to access your application.

  • For detailed information about the service, run:

kubectl describe services 

When you create a LoadBalancer Service in Kubernetes for your NGINX deployment, an external IP address is automatically allocated. This allows you to access NGINX from any location using the given public IP.

After deploying the application and service, find the public IP of a node in the Kubernetes cluster.

To verify the service is operational, access it through the load balancer by visiting: http://load_balancer_IP.

Last updated