Saturday 25 June 2022

Deploying Nginx Ingress to AKS Using a Makefile in a Custom Namespace

 Nginx is a popular ingress controller used in Kubernetes. We can use the yaml file here to deploy Nginx ingress controller to Azure Kubernetes Services. By default the namespace ingress-nginx will be used to deploy the Nginx controller. Let's see the usage of a Makefile to deploy Nginx ingress controller to AKS in a custom namespace utilizing the yaml file here.

To run make files you need to have make installed. In Ubuntu WSL you can isntal make with sudo apt-get install build-essential command. It isntalls other essntial tools as well. In windows you can nstall with chocolatey using choco install make.

Create a file named as Makefile with no extension with below content.

.PHONY: install-nginx delete-nginx

install-nginx:
    
	@curl -LsS https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml \
        | sed 's/namespace: ingress-nginx/namespace: yournamespace/' \
        | sed '/^ *kind: Namespace/,/^ *labels:/s/name: ingress-nginx/name: yournamespace/' \
| kubectl apply -f - delete-nginx: @curl -LsS https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml \ | sed 's/namespace: ingress-nginx/namespace: yournamespace/' \
| sed '/^ *kind: Namespace/,/^ *labels:/s/name: ingress-nginx/name: yournamespace/' \
| kubectl delete -f -

For example if you want to create your Nginx ingress controller in namespace platform you can use the make file below.

.PHONY: install-nginx delete-nginx

install-nginx:
    
	@curl -LsS https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml \
        | sed 's/namespace: ingress-nginx/namespace: platform/' \
        | sed '/^ *kind: Namespace/,/^ *labels:/s/name: ingress-nginx/name: platform/' \
        | kubectl apply -f -
    
delete-nginx:
    
	@curl -LsS https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml \
        | sed 's/namespace: ingress-nginx/namespace: platform/' \
        | sed '/^ *kind: Namespace/,/^ *labels:/s/name: ingress-nginx/name: platform/' \
        | kubectl delete -f -



A make file would look like above.

What we do is download the yaml file here and replace ingress-nginx with the namespace name we need.

sed 's/namespace: ingress-nginx/namespace: platform/'

Replces any occurance of  namespace: ingress-nginx

sed '/^ *kind: Namespace/,/^ *labels:/s/name: ingress-nginx/name: platform/'

Replaces the name: ingress-nginx  below as name: yournamespace in the yaml file here.

apiVersion: v1
kind: Namespace
metadata:
  labels:
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  name: ingress-nginx

You need to have the kubectl installed in your machine to run this. To install in kubectl you can use instructions here. You need to login using az login and have aks credentials merged to your kubeconfig. When we run the makefile with make targeting your AKS cluster you will get the ingress controller deployed to your desired namespace.


The ingress controller is created in the custom namespace as specified.





No comments:

Popular Posts