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:

  1. Change the service to type NodePort. This is required by GCP.

  2. 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     <none>        443/TCP        20h
service/mysite-svc     NodePort   <none>        80:31343/TCP   6h27m
service/postgres-svc   ClusterIP   <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
  name: mysite-ing
  annotations: {}
  - host: "www.mydomain.com"
      - path: /
        pathType: Prefix
            name: mysite-svc
              number: 80


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"


Replace with the IP address of your ingress controller.