Configure Nginx ingress controller for GCP(GKE)
Hi everyone. This is my first article for medium, don’t judge me strict. Will appreciate all your comments related to what should I improve in writing similar articles.

This article for peoples who already crated cluster, and who can create Deployment for some docker image and can expose it to some Load Balancer of ClusterIP service. Also, you already know how to configure DNS and you have some domain registered inside you Gloud DNS in some record set.
First of all, why do we need an Nginx ingress controller? The main reason why is because it gives you much more freedom. I realized that I need it when I’ve tried to find solving of problem to redirect my users from domain.com to www.domain.com. For this purpose, you can use annotation:
nginx.ingress.kubernetes.io/from-to-www-redirect: “true”
But as I realized soon, you can only use it when you will install Nginx ingress controller. Without it, it will not be working. How can you do it? Here is a guide https://kubernetes.github.io/ingress-nginx/deploy/.
1 . kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole cluster-admin \
--user $(gcloud config get-value account)2 . kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml3. kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/cloud-generic.yaml
After executing all of this commands step by step you can verify the installation
kubectl get pods --all-namespaces -l app.kubernetes.io/name=ingress-nginx --watch
Then you will see something like this in your terminal
POD_NAMESPACE=ingress-nginx
POD_NAME=$(kubectl get pods -n $POD_NAMESPACE -l app.kubernetes.io/name=ingress-nginx -o jsonpath='{.items[0].metadata.name}')
kubectl exec -it $POD_NAME -n $POD_NAMESPACE -- /nginx-ingress-controller --version
The last step is to check the installed version. You will see something the same the only difference will be in your release version. This means that all is configured correctly. Then the main part of all this process. How to check is it really configured correct and will be working for me. I can show you how is it looks like in my GCP console.
First of all, you have to go GKE -> Workloads and check it the is

Next step is to go GKE -> Services & Ingress and see this :

There will be 2 IP: ip:80, ip:443. This IP should be the same as you selected in Cloud DNS, for your host. In the last step, you have to configure your Ingress resource for directing traffic between your services.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
spec:
rules:
- host: www.domain.com
http:
paths:
- backend:
serviceName: front-end-service
servicePort: 80
path: /What you can see from this file. You should have front-end-service which exposed on port 80 , then you should have some domain which configured in Cloud DNS. Then the main thing for it to working correctly. You should check -> GKE -> Workloads ->nginx-ingress-controller -> YAML . There should be these lines in this YAML file. You can see — ingress-class=nginx , it should be the same with name of kubernetes.io/ingress.class: nginx . If it diferrent — it will not be working.
spec:
containers:
- args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --ingress-class=nginx
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/ingress-nginx
- --annotations-prefix=nginx.ingress.kubernetes.ioThe last step to check is it works. Go to domain.com and it will redirect you to www.domain.com. If it happened — it works properly. If you have additional questions, you can ask me in the comments . Will answer all your questions
