An Ingress Controller#
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.
Warning
You have to change your service definition! I didn’t fully understand how Google’s Load Balancers worked when I started teaching this course. Before you can get an ingress controller working you should change the service port in /deployment/mysite-service.yaml
.
apiVersion: v1
kind: Service
metadata:
name: mysite
spec:
type: NodePort
selector:
app: mysite
ports:
- protocol: TCP
port: 8000
targetPort: 8000
Introduction#
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/mysite-service.yaml
file and change the following line:
type: NodePort
Services can’t be converted with an update. They have to be deleted and re-applied:
$ kubectl delete service/mysite
$ kubectl apply -f deployment/mysite-service.yaml
Verify that the change happened:
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/mysite 1/1 Running 0 2m58s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.46.0.1 <none> 443/TCP 40d
service/mysite NodePort 10.46.2.95 <none> 80:30000/TCP 2m41s
Create an Ingress#
Here’s an ingress resource to start with:
Warning
Replace www.mydomain.com
with your domain name.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: cis-92-ingress
annotations: {}
spec:
rules:
- host: "www.mydomain.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: mysite
port:
number: 8000
Load balancers take some time to create. You can watch the process with this command:
$ kubectl describe ing/cis-92-ingress
When your load balancer gets an IP address it’s ready! You will have to update your DNS records to access it.