An Ingress is a Kubernetes API object that is used to access the services running inside the Kubernetes cluster from external world. Ingress is not enabled by default in Kubernetes cluster you have to enable Kubernetes cluster using third party ingress controller like Nginx, Istio, HAProxy, etc.,
Enabling Ingress Controller in Kubernetes Cluster
As a prerequisite, we must have a running Kubernetes cluster .You can setup running Kubernetes cluster using minikube or any other cloud services like AWS,GCP.,
Run the following kubectl command to install ingress nginx controller.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/baremetal/deploy.yaml
Verify that ingress controller is running successfully in cluster using the below command
kubectl get all -n ingress-nginx
The above command confirms that ingress is running successfully in our Kubernetes cluster. Next we will deploy a application and try to access that application from external world.
Deploying a Demo Application
For testing ingress, we will deploy a demo application of Nginx.
Create a namespace
Create a namespace called dev to deploy a sample application.
Deploy a Nginx Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Copy the above code and place it in nginx.yaml file .Run the above code using the following command
All the nginx pod is up and running.
Expose the application using service
Expose the running application using clusterIP service. First create a file called nginx-svc.yaml and copy the below code and run the file using the below command.
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: dev
labels:
app: hello
spec:
type: ClusterIP
selector:
app: nginx
ports:
- port: 80
targetPort: 8080
protocol: TCP
Copy the above code and place it in nginx-svc.yaml file. Run the above code using the below command and check whether the service is pointing the application using the command kubectl get ep -n dev. It is showing three endpoints.
Create an ingress object for Application
Now let’s create an ingress object to access our hello world application. An ingress object is nothing but a set of routing rules.
Create a file called ingress-hello.yaml and copy the below code and run it.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
namespace: dev
spec:
ingressClassName: nginx
rules:
- host: "dev.alphabsolutions.com"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx-service
port:
number: 80
When you type dev.alphabsolutions.com in browser it displays nginx webpage as follows