14.2. Ingress#
An ingress controller is an explicit load balancer. It’s more complex and more flexible than services of type LoadBalancer
because an Ingress
can sent requests to multiple services based on the URL of the request. The downside of this is that an Ingress only works for HTTP(S) traffic and not other things.
To convert your application from a LoadBalancer
service to an Ingress
based service you have to do the following:
Change the service to type
NodePort
. This is required by GCP.Create an
Ingress
resource with the URL routes you want.
Convert to NodePort
#
The first step is simple. Edit your deployment/service.yaml
file and change the following line:
type: NodePort
Now apply the changes:
$ kubectl apply -f service.yaml
Verify that the change happened:
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/mysite-deployment-69b9b76c87-krw42 1/1 Running 0 33m
pod/postgres-db-0 1/1 Running 0 6h1m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 34.118.224.1 <none> 443/TCP 20h
service/mysite-svc NodePort 34.118.239.112 <none> 80:31343/TCP 6h27m
service/postgres-svc ClusterIP 34.118.238.136 <none> 5432/TCP 6h1m
Create an Ingress#
Here’s an ingress resource to start with. Place this yaml in deployment/ingress.yaml
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: mysite-ing
annotations: {}
spec:
rules:
- host: "www.mydomain.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: mysite-svc
port:
number: 80
Note
Replace www.mydomain.com
with your domain name.
Load balancers take some time to create. You can watch the process with this command:
$ kubectl describe kubectl get ing
There’s more detain in:
$ kubectl describe kubectl describe ing
When your load balancer gets an IP address it’s ready! You will have to update your DNS records to access it.
Testing Ingress#
The ingress controller looks for a particular hostname. If that name has not been setup in DNS yet you won’t be able to load your site in a browser. But, you can still test the ingress controller with wget
by forcing the host
header to the correct value:
$ wget -d --header="host: www.mydomain.com" 34.49.69.101
Note
Replace 34.49.69.101
with the IP address of your ingress controller.