Expose Your Application To The Internet With A Loa
- To expose an application to the Internet, you need to use a
kubernetes service. - A
kubernetes serviceis a loadbalancer that directs traffic from the Internet to the Kubernetes Pods. - A loadbalancer service has a public and static IP address.
- The static IP is important, as pods and their IPs change frequently.
- The
serviceIP needs to remain the same.
- The
- Example
yamlmanifest for a service: ``` — apiVersion: v1 kind: Service metadata: name: demo-service namespace: development spec: selector: app: pod-info ports:- port: 80 targetPort: 3000 type: LoadBalancer ```
- Very important part of the above
yamlis the:selector: app: pod-info - That sends traffic to any pods with the label
pod-info. - All the pods deployed in the
deployment.yamlfile also need the labelpod-info.- If they do not have this, they will not be able to direct traffic to the pods.
- In the
service.yamlfile, we set theportto80, so that theportdoes not need to be typed in the URL: ``` ports:- port: 80 ```
- The service however is directing traffic to port
3000in the container:targetPort: 3000 - Finally, we are specifying that the service
typeis aloadbalancer:type: LoadBalancer - A
LoadBalanceris one type of Kubernetes service. There are in fact 3 types of services:LoadBalancerNodePortClusterIP
- Another command is
minikube tunnel. This shows an output similar to:Status: machine: minikube pid: 69570 route: 10.96.0.0/12 -> 192.168.49.2 minikube: Running services: [] errors: minikube: no errors router: no errors loadbalancer emulator: no errors - This allows minikube to access the external network. Exposes the application to the Internet.
ctrl + cto stop the service.
- We create the service with:
kubectl apply -f ./Ex_Files_Learning_Kubernetes/Exercise\ Files/04_01/service.yaml - Running
kubectl get services -n development, then shows the internal IP asCLUSTER-IPand the external IP asEXTERNAL-IP - If you create a Kubernetes cluster on AWS, Azure, GCP etc, they provide you with a public IP address.
- For example, copy and paste the external IP into your browser and you will see the JSON object:
10.101.236.59like so:{"pod_name":"pod-info-deployment-7587d5cc86-vk6br","pod_namespace":"development","pod_ip":"10.244.0.9"}