3 min read

Setup Nginx Ingress Controller for Kubernetes

Setup Nginx Ingress Controller for Kubernetes

This post is more like a note to myself, but this may help if you are looking at How to Setup Nginx Ingress Controller for Kubernetes.

From the official Kubernetes documentation, an Ingress is defined like as

An API object that manages external access to the services in a cluster, typically HTTP. Ingress may provide load balancing, SSL termination and name-based virtual hosting.

We can now plunge into the actual installation of NGINX Ingress Controller Kubernetes.

Install Nginx Ingress Controller - Helm Way

  1. Install Helm
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh

helm version

2.  Add Nginx Ingress Helm Repo

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx

3. Deploy Nginx Ingress with Helm

kubectl create namespace ingress-nginx
helm install rj-ingress ingress-nginx/ingress-nginx -n ingress-nginx

4. Verify the Installation

helm list -n ingress-nginx
kubectl get all -n ingress-nginx

NAME                                                       READY   STATUS    RESTARTS   AGE
pod/rj-ingress-ingress-nginx-controller-59c85d897b-j96wg   1/1     Running   0          13m

NAME                                                    TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)                      AGE
service/rj-ingress-ingress-nginx-controller             LoadBalancer   10.99.217.200   10.20.13.155   80:30072/TCP,443:32550/TCP   13m
service/rj-ingress-ingress-nginx-controller-admission   ClusterIP      10.96.196.7     <none>         443/TCP                      13m

NAME                                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/rj-ingress-ingress-nginx-controller   1/1     1            1           13m

NAME                                                             DESIRED   CURRENT   READY   AGE
replicaset.apps/rj-ingress-ingress-nginx-controller-59c85d897b   1         1         1       13m

Install Nginx Ingress Controller - Manifests Way

If you don't have Helm or if you prefer to use a YAML manifest, you can run the following command instead:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.0/deploy/static/provider/cloud/deploy.yaml

Operational Test

  1. Download Manifest
git clone https://github.com/rjhaikal/dotnet-websample.git
cd dotnet-websample
cat manifests/deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: webproject
  name: webproject
spec:
  replicas: 3
  revisionHistoryLimit: 1
  selector:
    matchLabels:
      app: webproject
  template:
    metadata:
      labels:
        app: webproject
    spec:
      containers:
      - image: rjhaikal/dotnet-websample:v1.0.0
        imagePullPolicy: Always
        name: webproject
        ports:
        - containerPort: 80
          name: webproject
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: webproject
  name: webproject
spec:
  ports:
  - name: "webproject-service"
    port: 80
  selector:
    app: webproject
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: webproject-ingress
spec:
  ingressClassName: nginx
  rules:
    - host: websample.rjhaikal.local
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: webproject
                port:
                  number: 80

2.  Apply Manifest

kubectl create namespace websample
kubectl apply -f manifests/deployment.yaml -n websample

3. Verify

kubectl get all -n websample
NAME                            READY   STATUS    RESTARTS   AGE
pod/webproject-fbdc4cc7-276v9   1/1     Running   0          15m
pod/webproject-fbdc4cc7-gxbh9   1/1     Running   0          15m
pod/webproject-fbdc4cc7-kq4fw   1/1     Running   0          15m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/webproject   ClusterIP   10.98.205.115   <none>        80/TCP    15m

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/webproject   3/3     3            3           15m

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/webproject-fbdc4cc7   3         3         3       15m


kubectl get ingress -n websample
NAME                 CLASS   HOSTS                      ADDRESS        PORTS   AGE
webproject-ingress   nginx   websample.rjhaikal.local   10.20.13.155   80      16m

4. Add Ingress to /etc/hosts

vim /etc/hosts
---
10.20.13.155 websample.rjhaikal.local
---

Result!

Reference

GitHub - kubernetes/ingress-nginx: Ingress-NGINX Controller for Kubernetes
Ingress-NGINX Controller for Kubernetes. Contribute to kubernetes/ingress-nginx development by creating an account on GitHub.