Using kubernetes metallb.io with access
TypeScriptMetalLB is a load balancer implementation for bare metal Kubernetes clusters, using standard routing protocols. It is often used when you're not using a Kubernetes cluster on a cloud provider with built-in load balancer services.
In a typical MetalLB setup, you would need to:
- Deploy MetalLB to your Kubernetes cluster.
- Configure MetalLB with a pool of IP addresses that it can use to assign external IP addresses to your services.
- Create a Kubernetes service of type
LoadBalancer
that will automatically get an external IP from MetalLB.
The following Pulumi TypeScript program demonstrates deploying a simple Nginx deployment and exposing it using MetalLB as a
LoadBalancer
service in a Kubernetes cluster. Before using the code, ensure that you have a Kubernetes cluster running and havekubectl
configured to communicate with the cluster.Firstly, we will configure MetalLB with a ConfigMap that specifies the range of IP addresses that MetalLB is allowed to use. MetalLB operates in two modes:
layer2
andBGP
. The example below useslayer2
mode, where MetalLB will respond to ARP requests for the external IPs directly.Then we will apply a manifest for an Nginx deployment and service of type
LoadBalancer
. MetalLB will assign an external IP from the specified range to this service.Here is the Pulumi TypeScript code:
import * as k8s from "@pulumi/kubernetes"; // Create a ConfigMap for MetalLB with the desired IP address pool. const metallbConfig = new k8s.core.v1.ConfigMap("metallb-config", { metadata: {namespace: "metallb-system", name: "config"}, data: { config: `address-pools: - name: default protocol: layer2 addresses: - 192.168.1.240-192.168.1.250` // Specify your IP range here }, }); // Deploy MetalLB to the Kubernetes cluster. // Ensure you have the proper rights to deploy to the `metallb-system` namespace. const metallbNamespace = new k8s.core.v1.Namespace("metallb-system", { metadata: {name: "metallb-system"} }); const metallbManifest = new k8s.yaml.ConfigFile("metallb-manifest", { file: "https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/namespace.yaml" // Check for the latest version }); // Create a deployment and service for Nginx, exposing it via a LoadBalancer const nginxLabels = { app: "nginx" }; const nginxDeployment = new k8s.apps.v1.Deployment("nginx-deployment", { metadata: {labels: nginxLabels}, spec: { replicas: 2, selector: { matchLabels: nginxLabels }, template: { metadata: { labels: nginxLabels }, spec: { containers: [{ name: "nginx", image: "nginx:1.17", // Replace with the Nginx version you want ports: [{containerPort: 80}], }], }, }, }, }); const nginxService = new k8s.core.v1.Service("nginx-service", { metadata: {labels: nginxLabels}, spec: { type: "LoadBalancer", selector: nginxLabels, ports: [{ port: 80, targetPort: 80 }], }, }); // Export the external IP address assigned by MetalLB to the Nginx service export const nginxExternalIp = nginxService.status.loadBalancer.ingress[0].ip;
In this code, we start by importing the necessary
@pulumi/kubernetes
library which allows us to interact with Kubernetes resources using Pulumi.We declare a
ConfigMap
for MetalLB that defines the range of IP addresses that MetalLB can assign to services. MetalLB uses thisConfigMap
to know the pool of IP addresses it can manage.Next, we deploy MetalLB to the cluster by applying its namespace manifest. We reference the official MetalLB manifest; it's important to check for the latest version and use it in the code.
After that, we define an Nginx deployment with a couple of replicas. The deployment includes a pod template that specifies an Nginx container to be run.
We also create a service of type
LoadBalancer
. When this type of service is created, Kubernetes will ask MetalLB to assign it an external IP from the configured pool.Finally, we export the IP address that MetalLB will assign to the Nginx service, which can be found under the
status.loadBalancer.ingress
field once the service is up and running.You can apply this code with Pulumi using the following commands:
pulumi stack init dev pulumi up
If MetalLB and the Nginx deployment are successfully deployed, you should see the Nginx IP address in the outputs of
pulumi up
.Please note that you may need to adapt parts of this code to fit your existing Kubernetes cluster configuration, such as the IP address pool and namespace.