Create LoadBalancer services

Once MetalLB is installed you can create services of type LoadBalancer, with MetalLB able to assign them an IP address from the IP address pools configured in the system.

In the example that follows, we will create an NGINX deployment and a ``LoadBalancer``service:

  1. Create a YAML file called nginx.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1
            ports:
            - name: http
              containerPort: 80
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
    spec:
      ports:
      - name: http
        port: 80
        protocol: TCP
        targetPort: 80
      selector:
        app: nginx
      type: LoadBalancer
    
  2. Create the deployment and service:

    kubectl apply -f nginx.yaml
    

    Example output:

    deployment.apps/nginx created
    service/nginx created
    
  3. Verify the IP assigned to the NGINX service:

    kubectl get svc
    

    Example output:

    NAME         TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)        AGE
    kubernetes   ClusterIP      10.96.0.1      <none>         443/TCP        74m
    nginx        LoadBalancer   10.96.42.228   192.168.10.0   80:34072/TCP
    20s