Development Kubernetes cluster with Kind (4) - Nginx ingress
Other posts in this series:
The following commands install the nginx ingress controller using the official Helm chart:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm show chart ingress-nginx/ingress-nginx
helm install ingress-nginx ingress-nginx/ingress-nginx --wait --namespace ingress-nginx --create-namespace --version ^4.1.0 --set-string "controller.nodeSelector.ingress-ready=true" --set controller.service.type=NodePort --set controller.service.nodePorts.http=32080 --set controller.service.nodePorts.https=32443 --set controller.metrics.enabled=true --set-string controller.podAnnotations."prometheus\.io/scrape"="true" --set-string controller.podAnnotations."prometheus\.io/port"="10254"
kubectl -n ingress-nginx wait --for=condition=ready --timeout=600s pod --selector=app.kubernetes.io/component=controller
Create example resources:
cat <<'EOF' | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: echo
labels:
app: echo
spec:
replicas: 2
selector:
matchLabels:
app: echo
template:
metadata:
labels:
app: echo
spec:
containers:
- name: echo
image: mendhak/http-https-echo:19
ports:
- containerPort: 8080
resources:
requests:
cpu: 256m
memory: 128Mi
limits:
cpu: 256m
memory: 128Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: whoami
labels:
app: whoami
spec:
replicas: 2
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: containous/whoami:v1.5.0
ports:
- containerPort: 80
resources:
requests:
cpu: 256m
memory: 128Mi
limits:
cpu: 256m
memory: 128Mi
---
kind: Service
apiVersion: v1
metadata:
name: echo
spec:
type: ClusterIP
selector:
app: echo
ports:
- protocol: "TCP"
port: 8080
targetPort: 8080
---
kind: Service
apiVersion: v1
metadata:
name: whoami
spec:
type: ClusterIP
selector:
app: whoami
ports:
- protocol: "TCP"
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: echo
annotations:
ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /echo(/|$)(.*)
pathType: Prefix
backend:
service:
name: echo
port:
number: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami
annotations:
ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /whoami(/|$)(.*)
pathType: Prefix
backend:
service:
name: whoami
port:
number: 80
---
EOF
Access to http://127.0.0.1:32080/echo/anything or http://127.0.0.1:32080/whoami (see containous/whoami documentation)to test.
Remember: set ingressClassName: nginx
in the ingress spec, or set controller.watchIngressWithoutClass
to true
so all the ingress would be managed by nginx.
With the previous configuration, the nginx ingress has also been configured to expose metrics to Prometheus, that we will install in a later post; for now, to check that the metrics are being generated, we can forward the port to the metrics service:
kubectl -n ingress-nginx port-forward service/ingress-nginx-controller-metrics 10254
And explore the metrics in the URL http://127.0.0.1:10254/metrics:
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 2.39e-05
go_gc_duration_seconds{quantile="0.25"} 3.61e-05
go_gc_duration_seconds{quantile="0.5"} 4.96e-05
go_gc_duration_seconds{quantile="0.75"} 9.7e-05
go_gc_duration_seconds{quantile="1"} 0.0015301
go_gc_duration_seconds_sum 0.004726
go_gc_duration_seconds_count 51
...