Ingress
This guide covers setting up ingresson a kind cluster.
Setting Up An Ingress Controller
We can leverage KIND's extraPortMapping
config option whencreating a cluster to forward ports from the hostto an ingress controller running on a node.
We can also setup a custom node label by using node-labels
in the kubeadm InitConfiguration
, to be usedby the ingress controller nodeSelector
.
- Create a cluster
- Deploy an Ingress controller, the following ingress controllers are known to work:
Create Cluster
Create a kind cluster with extraPortMappings
and node-labels
.
- extraPortMappings allow the local host to make requests to the Ingress controller over ports 80/443
- node-labels only allow the ingress controller to run on a specific node(s) matching the label selector
cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
authorization-mode: "AlwaysAllow"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
EOF
Contour
Deploy Contour components.
kubectl apply -f https://projectcontour.io/quickstart/contour.yaml
Apply kind specific patches to forward the hostPorts to theingress controller, set taint tolerations andschedule it to the custom labelled node.
{
"spec": {
"template": {
"spec": {
"nodeSelector": {
"ingress-ready": "true"
},
"tolerations": [
{
"key": "node-role.kubernetes.io/master",
"operator": "Equal",
"effect": "NoSchedule"
}
]
}
}
}
}
Apply it by running:
kubectl patch daemonsets -n projectcontour envoy -p '{"spec":{"template":{"spec":{"nodeSelector":{"ingress-ready":"true"},"tolerations":[{"key":"node-role.kubernetes.io/master","operator":"Equal","effect":"NoSchedule"}]}}}}'
Now the Contour is all setup to be used.Refer to Using Ingress for a basic example usage.
Additional information about Contour can be found at: projectcontour.io
Ingress NGINX
Apply the mandatory ingress-nginx components
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.27.0/deploy/static/mandatory.yaml
and expose the nginx service using NodePort.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.27.0/deploy/static/provider/baremetal/service-nodeport.yaml
Apply kind specific patches to forward the hostPorts to theingress controller, set taint tolerations andschedule it to the custom labelled node.
{
"spec": {
"template": {
"spec": {
"containers": [
{
"name": "nginx-ingress-controller",
"ports": [
{
"containerPort": 80,
"hostPort": 80
},
{
"containerPort": 443,
"hostPort": 443
}
]
}
],
"nodeSelector": {
"ingress-ready": "true"
},
"tolerations": [
{
"key": "node-role.kubernetes.io/master",
"operator": "Equal",
"effect": "NoSchedule"
}
]
}
}
}
}
Apply it by running:
kubectl patch deployments -n ingress-nginx nginx-ingress-controller -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx-ingress-controller","ports":[{"containerPort":80,"hostPort":80},{"containerPort":443,"hostPort":443}]}],"nodeSelector":{"ingress-ready":"true"},"tolerations":[{"key":"node-role.kubernetes.io/master","operator":"Equal","effect":"NoSchedule"}]}}}}'
Now the Ingress is all setup to be used.Refer Using Ingress for a basic example usage.
Using Ingress
The following example creates simple http-echo servicesand an Ingress object to route to these services.
kind: Pod
apiVersion: v1
metadata:
name: foo-app
labels:
app: foo
spec:
containers:
- name: foo-app
image: hashicorp/http-echo
args:
- "-text=foo"
---
kind: Service
apiVersion: v1
metadata:
name: foo-service
spec:
selector:
app: foo
ports:
# Default port used by the image
- port: 5678
---
kind: Pod
apiVersion: v1
metadata:
name: bar-app
labels:
app: bar
spec:
containers:
- name: bar-app
image: hashicorp/http-echo
args:
- "-text=bar"
---
kind: Service
apiVersion: v1
metadata:
name: bar-service
spec:
selector:
app: bar
ports:
# Default port used by the image
- port: 5678
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
annotations:
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /foo
backend:
serviceName: foo-service
servicePort: 5678
- path: /bar
backend:
serviceName: bar-service
servicePort: 5678
---
Apply the contents
kubectl apply -f https://kind.sigs.k8s.io/examples/ingress/usage.yaml
Now verify that the ingress works
# should output "foo"
curl localhost/foo
# should output "bar"
curl localhost/bar