To route external traffic to pods running in a TKG 2.0 cluster, you create a Service of type LoadBalancer. The load balancer service exposes a public IP address from which inbound traffic is routed to pods.
You can provision an external load balancer for Kubernetes pods that are exposed as services. For example, you can deploy a Nginx container and expose it as a Kubernetes service of type LoadBalancer.
Procedure
- Create the following nginx-lbsvc.yaml YAML file.
This YAML file defines a Kubernetes service of type LoadBalancer and deploys a Nginx container as an external load balancer for the service.
kind: Service
apiVersion: v1
metadata:
name: srvclb-ngnx
spec:
selector:
app: hello
tier: frontend
ports:
- protocol: "TCP"
port: 80
targetPort: 80
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: loadbalancer
spec:
replicas: 2
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
tier: frontend
spec:
containers:
- name: nginx
image: "nginxdemos/hello"
- Apply the YAML.
kubectl apply -f nginx-lbsvc.yaml
- Verify the deployment of the Nginx service.
The
srvclb-ngnx
is up with an external and internal IP address.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
srvclb-ngnx LoadBalancer 10.11.12.19 10.19.15.89 80:30818/TCP 18m
- Using a browser, enter the external IP address for the Nginx LoadBalancer service.
You see the message
NGINX
banner and details of the load balancer.