Install Ingress on Kubernetes and Minikube
Published: 2024-01-11 23:23:00

Kubernetes `ingress` was introduced to overcome short comings of services. Another version of Kubernetes such as `Openshift` created similar ingress such as `Routes`.

### Kubernetes Service - Drawbacks
1. Missing Enterprise Grade Load Bancing Capabilities.
2. Driving Cloud Cost - for Public IP Address

_Kubernetes service [svc] of type=[LoadBalancer] does not support enterprise grade Loadbancing capabilities but rather simple round robin fashion._ 

_To Solve this problem Kubernetes have asked Enterprise Load Balancers such as ([F5, NGINX, Ambassador, HA Proxy](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/) to name a few) to create [Ingress Controllers](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/) and user to create a Ingress resources. Its user's responsibilities_ 

### Enterprise Load Balancer Offers Advanced Capabilities
- Ratio Based Routing
- Sticky Session Routing
- Host Based
- Path Based
- Domain Based
- White listing IP Addresses
- Black Listing IP Addresses

## 1. Install NGINX controller in Kubernetes.

- `minikube addons enable ingress`to install on [Minikube](https://kubernetes.github.io/ingress-nginx/deploy/#minikube)
- Verify `Ingress Controller` installed or not
```
$minikube addons enable ingress
* ingress is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS
  - Using image registry.k8s.io/ingress-nginx/controller:v1.8.1
  - Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20230407
  - Using image registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20230407
* Verifying ingress addon...
* The 'ingress' addon is enabled
```
- verify by following command `kubectl get pods -A | grep nginx`

```
PS C:\Users\Jasper\OneDrive\Documents\CodeRepo\kubernetes\d38_ingress_ctrl> kubectl get pods -A
NAMESPACE       NAME                                        READY   STATUS      RESTARTS       AGE
default         sample-python-deployment-5787bd6b9f-656fn   1/1     Running     1 (16h ago)    20h
default         sample-python-deployment-5787bd6b9f-t9ttj   1/1     Running     1 (16h ago)    20h
ingress-nginx   ingress-nginx-admission-create-vcq7w        0/1     Completed   0              132m
ingress-nginx   ingress-nginx-admission-patch-g6szm         0/1     Completed   1              132m
ingress-nginx   ingress-nginx-controller-7799c6795f-qc9w5   1/1     Running     0              132m
kube-system     coredns-5d78c9869d-nscfc                    1/1     Running     3 (16h ago)    12d
kube-system     etcd-minikube                               1/1     Running     3 (16h ago)    12d
kube-system     kube-apiserver-minikube                     1/1     Running     3 (16h ago)    12d
kube-system     kube-controller-manager-minikube            1/1     Running     3 (16h ago)    12d
kube-system     kube-proxy-gzk52                            1/1     Running     3 (16h ago)    12d
kube-system     kube-scheduler-minikube                     1/1     Running     3 (16h ago)    12d
kube-system     storage-provisioner                         1/1     Running     12 (69m ago)   12d
```

## 2. Create Ingress Resource in Kubernetes.
- Route `foo.bar.com` to IP Addresses 
-- _(To mock it in local, in real time - we do not need to mock is as IP address is mapped to Domain Name by DNS) _
- Below [ingress.yml](https://kubernetes.io/docs/concepts/services-networking/ingress/) file is to route `foo.bar.com` to Service `sample-python-service`
```
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sample-python-service-ingress
spec:
  rules:
  - host: "foo.bar.com"
    http:
      paths:
      - pathType: Prefix
        path: "/bar"
        backend:
          service:
            name: sample-python-service
            port:
              number: 80
```
- Create `Ingress` resource `kubectl apply -f ingress.yml`
- Notice: Nothing happens if you don't install kubernetes controller. Controller will watch for this resource to be created and they applies the logic. 
```
$ kubectl apply -f ingress.yml
ingress.networking.k8s.io/sample-python-service-ingress created
```
## 3. Verify Ingress Resource in Kubernetes.

- Run command `kubectl get ingress` to view `ingress` resource
```
kubectl get ingress
NAME                            CLASS   HOSTS         ADDRESS          PORTS   AGE
sample-python-service-ingress   nginx   foo.bar.com   192.168.59.105   80      6m35s
```
## 3. Route domain to `Ingress` 
- Locate `Hosts File` and update `config` to point domain name `foo.bar.com` to created `ingress-service` --> which points to `service`
- Where is the Hosts File [Located](https://www.liquidweb.com/kb/edit-host-file-windows-10/#:~:text=Where%20is%20the%20Hosts%20File%20Located%3F%201%20Windows,-%20%22%2Fetc%2Fhosts%22%203%20Mac%20OS%20X%20-%20%22%2Fprivate%2Fetc%2Fhosts%22)?

> The location of the hosts file will differ by operating system. The typical locations are noted below.
```
Windows 10 - "C:\Windows\System32\drivers\etc\hosts"
Linux - "/etc/hosts"
Mac OS X - "/private/etc/hosts"
```
## 4. Verify domain is routed to Application (Ingress-> Service-> App) 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co8k76qj5onicti33bhr.png)

**Credits:-**
Thanks to [Abhishek Veeramalla](https://www.youtube.com/@AbhishekVeeramalla)