Sub topic 16
Ingress and addons for local development
This chapter assumes you have the web Deployment and Service from the earlier chapters running, and that you can create a kind cluster from a configuration file.
Why ingress
A NodePort Service exposes one port per application. An Ingress is a Kubernetes resource that routes HTTP requests to Services by host name or path, so many apps can share ports 80 and 443. An Ingress only describes the rules. An ingress controller, such as ingress-nginx, is a pod that reads the rules and does the routing.
Ingress on kind
kind has no addon system, so you install the controller yourself. The control-plane node needs an ingress-ready label and host ports 80 and 443 mapped to it. Save this as kind-ingress.yaml:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
- role: workerCreate the cluster, install the controller, and wait until it is ready:
kind create cluster --name dev --config kind-ingress.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=120sIngress on minikube
minikube ships optional features as addons. Enabling one deploys its manifests into the cluster:
minikube addons list -p dev
minikube addons enable ingress -p dev
minikube addons enable metrics-server -p devmetrics-server makes kubectl top pods work. With the Docker driver on macOS or Windows, the node IP is not reachable from the host. Run minikube tunnel -p dev in a separate terminal to reach the ingress controller on 127.0.0.1.
Route to the web Service
This Ingress sends all requests for web.localtest.me to the web Service. The public domain localtest.me resolves to 127.0.0.1, so you need no /etc/hosts edit.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web
spec:
ingressClassName: nginx
rules:
- host: web.localtest.me
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80kubectl apply -f web-ingress.yaml
kubectl get ingress web
curl http://web.localtest.me/Common errors
curlreturns 404 from nginx. The request reached the controller, but no rule matched. Check the host name andingressClassName.curlhangs or is refused on kind. The cluster was probably created without the port mappings, and they cannot be added later. Delete and recreate it.- The Ingress has no ADDRESS on minikube. The addon pod may still be starting. Check
kubectl get pods -n ingress-nginx.
Exercise
On kind, your Ingress exists and the controller pod is Ready, but curl http://web.localtest.me/ is refused. The cluster config had no extraPortMappings. How do you fix it, and why can't you edit the running cluster?
Show answer
Delete the cluster with kind delete cluster --name dev and recreate it with a config that maps host ports 80 and 443 to the control-plane node container. Port mappings are set on the node containers when they are created, and kind cannot change them afterwards.