Exposing the frontend service
In order to make it publicly available, we have to edit the service yaml file. Run the following command to download the file:
curl -O -L https://k8s.io/examples/application/guestbook/frontend-service.yaml
Use the online editor to edit it:
code frontend-service.yaml
Comment out the NodePort line and uncomment the type:LoadBalancer line:
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# comment or delete the following line if you want to use a LoadBalancer
# type: NodePort # line commented out
# if your cluster supports it, uncomment the following to automatically create
# an external load-balanced IP for the frontend service.
type: LoadBalancer # line uncommented
ports:
- port: 80
selector:
app: guestbook
tier: frontend
Next, run the following command:
kubectl create -f frontend-service.yaml
This step takes some time the first time you run it. In the background, Azure has to perform lots of magic, to make it seamless. It has to create an Azure Load Balancer (ALB), and set the port-forwarding rules to forward traffic on port 80 to internal ports of the cluster.
Run the following until there is a value in the EXTERNAL-IP column:
ab443838-9b3e-4811-b287-74e417a9@Azure:~$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend LoadBalancer 10.0.89.120 52.183.18.103 80:31761/TCP 42m
In the Azure portal, if you click on All Resources and filter on Load Balancer, you will see a Kubernetes Azure Load Balancer. Clicking on it shows you something similar to the following screenshot. The highlighted sections shows the connection between the public IP (that is, External-IP):80 to the internal NodePort of 31761 across all the nodes in the cluster: