How to Setup Metal LB for Nginx Ingress Loadbalancer IP in Kubernetes 2021

 


At first create namespace

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/namespace.yaml

Then install metal lb

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/manifests/metallb.yaml

Check the available IP for loadbalancer of your virtual subnet

Then set the IP in the below configmap and apply

apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 172.16.1.230-172.16.1.230

kubectl apply -f config.yaml

Next set up the nginx ingress controller using helm

Create a internal-ingress.yaml file for configuration

controller:
config:
use-forwarded-headers: "true"
server-tokens: "False"
service:
loadBalancerIP: 172.16.1.230

Then install using helm

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install ingress-nginx ingress-nginx/ingress-nginx  -f  internal-ingress.yaml
 
Check the service and External IP of loadbalancer
 
kubectl get svc
 
Now create a simple deployment and ingress for nginx default page
 
deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: nginx
name: nginx-deploy
spec:
replicas: 1
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx
name: nginx



kubectl apply -f deployment.yaml

create a service

kubectl expose deploy nginx-deploy --port 80

create an updated ingress.yaml (kubernetes updated ingress)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "X-Frame-Options: Deny";
more_set_headers "X-Xss-Protection: 1; mode=block";
more_set_headers "X-Content-Type-Options: nosniff";
name: nginx-deploy
namespace: default
spec:
rules:
- host: nginx.devops.com
http:
paths:
- backend:
service:
name: nginx-deploy
port:
number: 80
path: /
pathType: ImplementationSpecific

 
 
Then apply the yaml files

kubectl apply -f ingress.yaml
 
Go to /etc/hosts and add the loadbalancer ip and site address

sudo nano /etc/hosts
 
172.16.1.230  nginx.devops.com 
 
Save it and check

curl http://nginx.devops.com

You can see the nginx welcome page




Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment