
Vultr Load Balancer is a fully-managed solution to distribute traffic to groups of servers which decouples the availability of a backend service from the health of a single server. Vultr Load Balancer ensures your service stay online by distributing the load across multiple servers to ensure that servers don't get overloaded.
If you are new to Vultr Load Balancers, you should read the Load Balancer Quickstart Guide first.
Vultr Kubernetes Engine (VKE) is a fully-managed Kubernetes product. When deploying an application to VKE, Kubernetes automatically spreads Pods across different nodes in a cluster for better availability.
Vultr Load Balancers are compatible with VKE to distribute traffic across multiple Pods in different nodes. Vultr Load Balancer in VKE offers all the same features and capabilities as the fully-managed solution for standalone scenarios.
This guide explains how to deploy and configure Vultr Load Balancers in Vultr Kubernetes Engine (VKE) with detailed configuration information.
Before you begin, you should:
kubectl and git in your machine.This section shows how to deploy web servers to the Kubernetes cluster using a Deployment. The web server in this article is a Python web server that returns the hostname of the pod and HTTP request headers.
This example application has a public Docker image (quanhua92/whoami) in the Docker Hub. You can go to this GitHub repository to see the application's source code.
Create a file named deployment.yaml with the following content:
Deploy the application using kubectl
Notice that the deployment name in this example is whoami, and the Pod listens to requests on the port 8080.
This section shows how to deploy a load balancer for HTTP traffic on port 80. You deploy a Kubernetes Service with the LoadBalancer type and use metadata annotations to configure VKE Load Balancer.
The default load-balancing algorithm is the Round Robin algorithm. This works by using each server behind the load balancer in turns.
Create a file named service.yaml with the following content. The app selector whoami matches the existing deployment, and the target port 8080 matches the container port in the previous step.
Deploy the Service using kubectl
Run the following command to see the VKE Load Balancer setup progress:
The result should look like:
You can also go to the Load Balancer page in the Vultr Console to inspect your Load Balancers.
You can navigate to the IP address of your Load Balancer to access the application.
Notice that it may take a few minutes before you can access the application through the Load Balancer IP address.
The response of the application should look like:
Refresh the website a few times. Notice that the Hostname changes after a few requests, meaning the Load Balancer can distribute the traffic across multiple pods.
The least connections load balancing algorithm is a dynamic load balancing algorithm that distributes the client requests to the application server with the least number of active connections at the time the load balancer receives the client request. This algorithm works best in environments where the application servers have similar capabilities.
Change the service.yaml as follows:
Deploy the Service using kubectl
Vultr Load Balancers provides health checks to determine if the application servers respond to client requests. Here are some configurations that you can customize:
Here are some configurations that you can customize:
healthcheck-protocol: The protocol that load balancers use to perform health check. Two possible values are tcp and http. The default value is tcphealthcheck-path: The URL path that load balancers use to check on the application server. The default value is the root path, /.healthcheck-port: The port that load balancers use to check on the application server. The Kubernetes defines this value. You should not change this value in normal scenarios.healthcheck-check-interval: The interval between health checks in seconds. The default value is 15.healthcheck-response-timeout: The response timeout in seconds. The default value is 5.healthcheck-unhealthy-threshold: The number of unhealthy requests before the load balancer removes the application server from the server pool. The default value is 5.healthcheck-healthy-threshold: The number of healthy requests before load balancers adds the application server back to the server pool. The default value is 5.The example application in this article has an endpoint for the health check, /health. The benefit of using /health instead of / is that:
In the example application code, the endpoint returns an empty response with 200 status without any complex computation.
Change the service.yaml as follows:
Deploy the Service using kubectl
This section shows how to deploy a load balancer for HTTPS traffic on port 443.
Here are some approaches to obtaining TLS/SSL Certificates:
Self-Signed Certificates: Use your own Certificate Authority to create and sign TLS/SSL certificates. This is a great option for development environments.Purchase TLS/SSL Certificates: You need to buy a TLS/SSL certificate from a well-known Certificate Authority for production use-cases.Use Free TLS/SSL Certificates: Use free TLS/SSL certificates from Let's Encrypt or ZeroSSL.In this section, you install NGINX Ingress Controller to handle incoming SSL/TLS traffic and Cert Manager to manage free TLS/SSL certificates from Let's Encrypt.
NGINX Ingress Controller creates a LoadBalancer service to handle incoming traffic. This LoadBalancer service is also a VKE Load Balancer, so you don't need the service created in the previous sections.
VKE Load Balancer routes incoming traffics to a pool of server nodes. Then, each server node routes the load into the NGINX Ingress Controllers. Each NGINX Ingress Controller routes the requests into the corresponding application pods.
By default, there is only one NGINX Ingress Controller. You can scale the NGINX Ingress Controller depending on the traffic of your system.
Cert Manager automates the creation and management of TLS/SSL certificates from various issuing sources, including Let's Encrypt, HashiCorp Vault, Venafi, and private public key infrastructure.
You need a domain name to issue and manage free TLS/SSL Let's Encrypt certificates.
(Optional) Delete the Service in the previous section by using the following command:
Create a Service file service-02.yaml with the following content. The app selector whoami matches the existing deployment, and the target port 8080 matches the container port in the previous step. Notice that this service is not a LoadBalancer type, and the name of this service is whoami-service.
Run the command to create the service
Install NGINX Ingress Controller (ingress-nginx)
Go to your Load Balancers dashboard and get the IP Address of the newly created Load Balancer. This is the Load Balancer created for the NGINX ingress.
(Optional) Run the following command to wait for IP of the newly created Load Balancer. The IP is in the EXTERNAL-IP column.
Create an A record in your domain DNS that points to the above IP address.
(Optional) Scale NGINX Ingress Controller to 03 replicas.
This section shows how to set up Cert Manager to use HTTP01 challenge solver to verify the ownership. If you want to use DNS01 challenge solver, see the article How to Automate DNS/TLS with External DNS and Let's Encrypt on Vultr Kubernetes Engine
Install cert-manager to manage SSL certificates
Create a manifest file letsencrypt.yaml to handle Let's Encrypt certificates. Replace
Run the command to install the above Let's Encrypt issuers.
Create an Ingress manifest file ingress.yaml with the following content. Replace whoami-service with your service name.
Run the command to create the ingress
Run the command kubectl get ingress to see the newly created ingress. The result should look like:
Check the certificates
Navigate to https://<YOUR_DOMAIN> to access your application.
By default, a load balancer routes each request independently to a pool of servers based on the load-balancing algorithm. However, you can use the sticky session (also known as session affinity) feature to bind a user's session to a specific server.
In a Kubernetes environment, the sticky session feature helps keep the session from the client to a specific application pod. If the application pod is not available, the load balancer re-routes the requests to another application pod.
This section shows how to achieve sticky sessions using the NGINX Ingress Controller from the previous section.
You need to add the following annotations to the Ingress manifest file:
nginx.ingress.kubernetes.io/affinity: enable the Sticky Session. The value has to be cookie".nginx.ingress.kubernetes.io/session-cookie-name: name of the cookie to track the instance for each request to each application pod.nginx.ingress.kubernetes.io/session-cookie-max-age: time until the cookie expires in secondsnginx.ingress.kubernetes.io/session-cookie-expires: a legacy version of the previous annotation for compatibility with old browsers.Change the ingress.yaml as follows:
Apply the changes using kubectl
Confirm that the Ingress works
Check if the server responds a Set-Cookie header
The result should look like:
Notice that the response contains a set-cookie header with the sticky key. This cookie contains information about the upstream server. The NGINX Ingress Controller tries to route requests with the same cookie to the same application pod.
Refresh the website a few times. Notice that the Hostname doesn't change until the cookie expires, meaning the sticky session works as expected.
Proxy Protocol is a network protocol for preserving a client's connection information (such as IP addresses) when the client's connection passes through a proxy. The ability to preserve the client information is essential to analyze the traffic logs or change the application functionality based on the geographic IP address.
This section shows how to set up a Vultr Load Balancer with Proxy Protocol to distribute traffic to the NGINX Ingress Controller and preserve client information.
Notice that you should set up Cert Manager to use the DNS01 challenge solver to issue TLS/SSL certificates and avoid problems with the HTTP01 challenge.
Download the installation manifest of the NGINX Ingress Controller
Open the deploy.yaml with your favorite text editor
Search for the text kind: ConfigMap and replace the content of the ConfigMap resource as follows. Replace the text 1.5.1 with your NGINX Ingress Controller version.
Search for the text type: LoadBalancer and replace the content of that Service as follows. Replace the text 1.5.1 with your NGINX Ingress Controller version.
Apply the changes using kubectl
0 Comments
Be the first to comment and share your perspective with the community.