Ingress on Kubernetes Cluster
On Local Cluster Created by using Kubeadm
Why Ingress need on Kubernetes?
Ingress is a key component in Kubernetes that provides HTTP and HTTPS routing to services based on rules defined by the user.
Ingress may provide load balancing, SSL termination and name-based virtual hosting.
In this blog, am going to demonstrate Host Based and Path Based ingress how to as simple way by installing Nginx ingress controller on cluster, for LoadBalancer solution we are going to use metallb because its local cluster created using kubeadm with 3 virtual machines.
General types of Ingress we use on Kubernetes
1. Single Service Ingress:
Ingress by specifying a default backend with no rules
2. Name Based Virtual Hosting ingress:
Supports routing HTTP traffic to multiple hostnames at the same IP address
3. Simple Fanout / Path Based ingress:
Route traffic from a single IP address to more than one service, based on the HTTP URI.
Ingress rule
Pre-Requisites
You may need to deploy an Ingress controller such as ingress-nginx.
Create Namespace
kubectl create namespace ingress-nginx
Install Nginx Ingress
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/cloud/deploy.yaml
Output
Pre-flight check
kubectl get pods -n ingress-nginx
Ensure ingress controller pod to be up, running, and ready.
Output
List Service
kubectl get svc --namespace=ingress-nginx
Output
LoadBalancer is in pending state, because it's local cluster, not in the cloud or no cloud controller has installed.
LoadBalancer Solution ??? for cluster created using kubeadm😀
metallb
But we have solution for create LoadBalancer in local cluster by using metallb
Install metallb
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.14.3/config/manifests/metallb-native.yaml
List pods
kubectl get pods -n metallb-system
Output
Defining the IPs to assign to the Load Balancer Services
vi ip-pool.yaml
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: cheap
namespace: metallb-system
spec:
addresses:
- 192.168.10.0/24
Apply the ip pool
kubectl apply -f ip-pool.yaml
Verify the Nginx ingress service for LoadBalancer IP Status
kubectl get svc -n ingress-nginx
Output
IP Address got allotted by metallb.
Single Ingress - Demo
Deploy a sample application.
- Create deployment
vi kubewebserver.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubewebserver
labels:
app: kubewebserver
spec:
replicas: 1
selector:
matchLabels:
app: kubewebserver
template:
metadata:
labels:
app: kubewebserver
spec:
containers:
- name: kubewebserver
image: nginx:alpine
ports:
- containerPort: 80
- Apply the deployment
kubectl apply -f kubewebserver.yaml
- Create Service
vi kubewebserver-service.yaml
apiVersion: v1
kind: Service
metadata:
name: kubewebserver-service
labels:
app: kubewebserver-service
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: kubewebserver
- Apply the service
kubectl apply -f kubewebserver-service.yaml
To get ingress Class
First get the ingress class, we need to define on ingress yaml
kubectl get ingressClass -A
Create minimal ingress without host
vi minimal-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: kubewebserver-service
port:
number: 80
- Apply the ingress
kubectl apply -f minimal-ingress.yaml
- List ingress
kubectl get ingress
Output
Validate
curl 192.168.10.0
Output
Create ingress with host
vi ingress-with-host.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-with-host
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: app1.kubelancer.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: kubewebserver-service
port:
number: 80
- Apply the ingress
kubectl apply -f ingress-with-host.yaml
- Get ingress
kubectl get ingress
Output
Validate
Try with by using IP Address
curl 192.168.10.0
Using IP Address will return error
Use Hostname
curl --resolve app1.kubelancer.com:80:192.168.10.0 http://app1.kubelancer.com:80
Output