Deploying Nginx Ingress Controller Using Helm in Kubernetes.

rd17

RD17🧑🏽‍💻

Posted on July 28, 2024

Deploying Nginx Ingress Controller Using Helm in Kubernetes.

Image description

Introduction

In Kubernetes, an Ingress Controller is essential for managing external access to services within your cluster, typically HTTP and HTTPS. The Nginx Ingress Controller is one of the most popular Ingress Controllers available. This guide will walk you through deploying the Nginx Ingress Controller using Helm, a powerful package manager for Kubernetes.

Prerequisites

Before we begin, ensure you have the following:

  • A running Kubernetes cluster. (Minikube, AKS, EKS, GKE, or any other)
  • kubectl command-line tool configured to interact with your cluster.
  • Helm installed on your local machine.

Step 1: Install Helm

If you haven't installed Helm yet, you can do so using the following commands:

On MacOS

 brew install helm
Enter fullscreen mode Exit fullscreen mode

On Linux

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
Enter fullscreen mode Exit fullscreen mode

On Windows(From Chocolatey/Scoop/Winget)

choco install kubernetes-helm
scoop install helm
winget install Helm.Helm
Enter fullscreen mode Exit fullscreen mode

Verify your Helm installation:

helm version
Enter fullscreen mode Exit fullscreen mode

Step 2: Add the Nginx Ingress Helm Repository

Helm uses repositories to distribute charts. We need to add the official Nginx Ingress repository:

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
Enter fullscreen mode Exit fullscreen mode

This command adds the ingress-nginx repository and updates your Helm repository list to ensure you have the latest charts.

Step 3: Install the Nginx Ingress Controller

Now, let's install the Nginx Ingress Controller using the Helm chart. We'll name our release k8s-nginx-ingress and install it into a new namespace called ingress-nginx.

helm install k8s-nginx-ingress ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace
Enter fullscreen mode Exit fullscreen mode

Breakdown of the Command:

  • helm install: Command to install a Helm chart.
  • k8s-nginx-ingress: Name of the Helm release.
  • ingress-nginx/ingress-nginx: Specifies the chart to install from the ingress-nginx repository.
  • --namespace ingress-nginx: Namespace in which to install the Ingress Controller.
  • --create-namespace: Creates the namespace if it doesn’t already exist.

Step 4: Verify the Installation

After the installation is complete, verify that the Nginx Ingress Controller is running:

Check Pods

kubectl get pods -n ingress-nginx
Enter fullscreen mode Exit fullscreen mode

Check Services

kubectl get svc -n ingress-nginx
Enter fullscreen mode Exit fullscreen mode

You should see an output indicating that the Nginx Ingress Controller pods are running and that a service of type LoadBalancer (or NodePort in some environments) is available.

Step 5: Deploy an Nginx Pod

To demonstrate the functionality of the Nginx Ingress Controller, we'll deploy a simple Nginx pod and expose it using a service.

Deploy an NGINX Pod
Deploying a pod is straightforward with the kubectl run command. This command creates a deployment with a single NGINX pod.

kubectl run nginx-pod --image=nginx
Enter fullscreen mode Exit fullscreen mode

Expose the NGINX Pod
To access the NGINX pod, you need to expose it. Let’s create a Service of type ClusterIP:

kubectl expose pod nginx-pod --port=80 --type=ClusterIP --name=nginx-service
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a Secret for the SSL Certificate

If you plan to use TLS with your Ingress resource, you need to create a Kubernetes Secret to store your SSL certificate and private key.

Convert SSL Certificate to Base64
First, convert your SSL certificate (tls.crt) and private key (tls.key) to base64:

cat tls.crt | base64
cat tls.key | base64
Enter fullscreen mode Exit fullscreen mode

Create the Secret File
Create a file named nginx-tls-secret.yaml with the following content, replacing and with the actual base64 encoded values:

apiVersion: v1
kind: Secret
metadata:
  name: example-tls
  namespace: default
data:
  tls.crt: <base64 encoded cert>
  tls.key: <base64 encoded key>
type: kubernetes.io/tls

Enter fullscreen mode Exit fullscreen mode

Deploy the Secret

kubectl create -f nginx-tls-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Step 7: Create an Ingress Resource

With the Nginx Ingress Controller and the Nginx pod running, you can now create an Ingress resource to route traffic to the Nginx service.

Example Ingress Resource
Create a file named nginx-ingress.yaml with the following content:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-example
  namespace: default
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: 
                name: nginx-service
                port:
                  number: 80
  # This section is only required if TLS is to be enabled for the Ingress
  tls:
    - hosts:
      - example.com
      secretName: example-tls
Enter fullscreen mode Exit fullscreen mode

Deploy the Ingress Resource

kubectl create -f nginx-ingress.yaml
Enter fullscreen mode Exit fullscreen mode

Step 8: Configure DNS Entry

After deploying the Nginx Ingress Controller and the Ingress resource, you need to map your domain name (example.com) to the external IP address of the Nginx Ingress Controller's load balancer.

Get the Load Balancer IP
Run the following command to get the external IP address assigned to the Nginx Ingress Controller's load balancer:

kubectl get svc -n ingress-nginx
Enter fullscreen mode Exit fullscreen mode

Image description

Update DNS Record
Once you have the external IP address, update the DNS record for your domain to point to this IP address. This process will vary depending on your DNS provider, but generally, you will need to create an A record for example.com with the external IP address.

Image description

Image description

Conclusion

By following these steps, the Nginx Ingress Controller is now deployed in the Kubernetes cluster using Helm.

If you have any questions or encounter issues, feel free to comment below. Keep exploring Kubernetes!

💖 💪 🙅 🚩
rd17
RD17🧑🏽‍💻

Posted on July 28, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related