Expose an NGINX deployment through a service of type LoadBalancer, and open the external IP address in your browser.
- You have a project with a deployed Kubernetes cluster in the Ready status, and the cloud credentials of the project support load balancers.
- You have a kubeconfig file for the cluster and a local terminal with kubectl. For more information, see Kubeconfig files in Overview.
- Save the following deployment and service in a file named lb-test.yaml.
The labels on the Pods must match the selector in the service. In this example, both use the label app: lb-test.
apiVersion: apps/v1
kind: Deployment
metadata:
name: lb-test
spec:
replicas: 3
selector:
matchLabels:
app: lb-test
template:
metadata:
labels:
app: lb-test # All pods created by this deployment have the label app: lb-test
spec:
containers:
- name: my-container
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: lb-test-svc
spec:
selector:
app: lb-test # Send packets to pods that have the label app: lb-test
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
- Apply the file:
kubectl apply -f lb-test.yaml
- Wait for the external IP address.
The EXTERNAL-IP column of the service changes from <pending> to an address. LiveOps also shows the external IP address of the service on its Services page.
kubectl get service lb-test-svc --watch
- Open the external IP address in your browser, for example
http://<external-ip>. - Optional: Set up DNS.
Point your DNS records to the external IP address of the load balancer, and then go to your domain in a browser. If you do not have a custom domain, use an sslip.io wildcard domain, which resolves automatically. For example, use <external-ip>.sslip.io.
- Optional: To release the load balancer, delete the resources:
kubectl delete -f lb-test.yaml
The browser displays the NGINX welcome page.