NGINX Ingress Controller for Kubernetes Tutorial¶
- You can find the source code for this video in my GitHub Repo.
- If you want to create EKS cluster using terraform, you can follow this tutorial.
Prerequisites¶
Deploy Prometheus on Kubernetes Cluster¶
- Create following
CRDs
and apply them.prometheus/0-crd/0-alertmanagerconfigs.yaml
prometheus/0-crd/1-alertmanagers.yaml
prometheus/0-crd/2-podmonitors.yaml
prometheus/0-crd/3-probes.yaml
prometheus/0-crd/4-prometheuses.yaml
prometheus/0-crd/5-prometheusrules.yaml
prometheus/0-crd/6-servicemonitors.yaml
prometheus/0-crd/7-thanosrulers.yaml
- Deploy Prometheus Operator.
prometheus/1-prometheus-operator/0-monitoring-ns.yaml
prometheus/1-prometheus-operator/1-crd-cluster-roles.yaml
prometheus/1-prometheus-operator/2-service-account.yaml
prometheus/1-prometheus-operator/3-cluster-role.yaml
prometheus/1-prometheus-operator/4-cluster-role-binding.yaml
prometheus/1-prometheus-operator/5-deployment.yaml
prometheus/1-prometheus-operator/5-deployment.yaml
prometheus/1-prometheus-operator/6-service.yaml
prometheus/1-prometheus-operator/7-service-monitor.yaml
- Deploy Prometheus.
prometheus/2-prometheus/0-service-account.yaml
prometheus/2-prometheus/1-cluster-role.yaml
prometheus/2-prometheus/2-cluster-role-binding.yaml
prometheus/2-prometheus/3-prometheus.yaml
- Check Prometheus pods.
Deploy Nginx Ingress Controller (YAML & HELM)¶
- Add Nginx ingress Helm repo.
- Update Helm repository.
- Search for
nginx
Helm Chart.
-
Create
values.yaml
to override default parameters -
Generate YAML from the Helm chart.
helm template my-ing ingress-nginx/ingress-nginx \
--namespace ingress \
--version 3.35.0 \
--values values.yaml \
--output-dir my-ing
- Deploy Nginx ingress with Helm.
helm install my-ing ingress-nginx/ingress-nginx \
--namespace ingress \
--version 3.35.0 \
--values values.yaml \
--create-namespace
- List Helm releases.
- Get
nginx
pods.
- Get Kubernetes services.
Monitor Nginx Ingress with Prometheus¶
- Port forward Prometheus to localhost for now.
- Add monitoring label to ingress namespace.
Deploy Grafana on Kubernetes Cluster¶
- Generate admin password for Grafana.
- Decode the password to base64 string.
- Deploy grafana.
- Get Kubernetes pods.
Import Nginx Ingress Controller Grafana Dashboard¶
- Port forward Grafana to localhost for now.
-
Login to Grafana, user:
admin
, password:devops123
. -
Add Prometheus datasource.
-
To find Grafana dashboard google:
nginx ingress grafana dashboard
. -
Import
9614
dashboard
Create Ingress for Prometheus¶
- Get services.
- Create ingress.
- Get ingresses.
-
Create CNAME record for
prometheus.devopsbyexample.io
-
Go to
http://prometheus.devopsbyexample.io
Test Nginx Ingress Admission Webhook¶
- Add valid ngin directive annotation to Prometheus ingress.
prometheus.yaml | |
---|---|
- Make a mistake in the directive and apply.
- Render the full nginx config.
- Search for
more_set_headers
Create Ingress for Grafana¶
- Get services.
- Create ingress.
- Get ingresses.
-
Create CNAME record for
grafana.devopsbyexample.io
-
Go to
http://grafana.devopsbyexample.io
Simple Fanout Ingress Example¶
-
Create
app
directory. -
Create
example-3
directory. -
Apply
example-3
folder.
- Get ingresses in staging namespace.
-
Create CNAME record for
api
. -
Test with ingresses with
cURL
.
Virtual Hosting Ingress Example¶
- Create
example-4
folder. - Apply
example-4
.
- Get ingress.
-
Create CNAME record for
foo
andbar
. -
Test with
cURL
.
Nginx Ingress TLS Example¶
- Install
cfssl
using homebrew if you are on mac.
- Create config
certs/0-config.json
. - Create CA certificate request
certs/1-ca-csr.json
. - Change directory to
certs
and generate CA.
- Create certificate request for
foo-api.devopsbyexample.io
domaincerts/2-foo-api-csr.json
. - Generate certificate.
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=0-config.json \
-profile=demo \
2-foo-api-csr.json | cfssljson -bare foo-api
- Open with OpenSSL.
- Create Kubernetes secret
example-5/7-tls-secret.yaml
.
7-tls-secret.yaml | |
---|---|
- Encode tls certificate
certs/foo-api.pem
to base64.
- Encode private key
certs/foo-api.pem
to base64.
- Create ingress
example-5/8-tls-ingress.yaml
. - Switch directory and apply.
- Get ingress
- Create CNAME record for
foo-api
. - Go to
https://foo-api.devopsbyexample.io
- Add CA to KeyChain.
Nginx Ingress Different Namespaces Example¶
- Create
example-6
folder. - Apply
example-6
.
- Use local service.
- Get pods in both namespaces.
- Get ingresses in
foo
namespace.
- Create CNAME for
api-ns
. - Test using
cURL
.
- Create
example-6/6-bar-external.yaml
. - Update
example-6/5-ingress.yaml
. - Apply
example-6
.
- Test with
cURL
.
Nginx Ingress TCP Example¶
-
Create following files.
example-7/0-namespace.yaml
example-7/1-secrets.yaml
example-7/2-statefulset.yaml
example-7/3-service.yaml
-
Apply
example-7
folder and get pods indatabase
namespace.
- Create
example-7/4-configmap.yaml
.
kubectl get svc my-ing-ingress-nginx-controller -n ingress
kubectl get deployment -n ingress
kubectl edit deployment -n ingress my-ing-ingress-nginx-controller
kubectl edit svc my-ing-ingress-nginx-controller -n ingress
- Go to AWS open LB and security group.
- Create CNAME for postgres.
Monitor Nginx Ingress with Grafana¶
- Open
http://grafana.devopsbyexample.io
. - Create some traffic.
curl http://api.devopsbyexample.io/bar/123asd
curl http://bar.devopsbyexample.io/bar
curl http://foo-ns.devopsbyexample.io/bar
curl http://api-ns.devopsbyexample.io/foo/asdad