Use the Backend custom resource
With the Backend custom resource, you can manage how traffic is load balanced across pods. To use it:
-
Create a YAML file that declares a
Backend
resource and add properties to itsspec.config
section.In the example below, the
balance.algorithm
property changes the load balancing algorithm to least connections.example-backend.yaml
apiVersion: "core.haproxy.org/v1alpha1" kind: Backend metadata: name: example-backend namespace: default spec: config: balance: algorithm: "leastconn"
-
Deploy it to your cluster using
kubectl apply
.$ kubectl apply -f example-backend.yaml
-
Decide which Kubernetes services the resource should apply to. Do one of the following:
-
To have the properties apply to all services, create a new ConfigMap with the name
haproxy-kubernetes-ingress
to override the one that ships with the ingress controller. Add thecr-backend
key to thedata
section to implement the backend properties.example-configmap.yaml
apiVersion: v1 kind: ConfigMap metadata: name: haproxy-kubernetes-ingress namespace: haproxy-controller data: cr-backend: default/example-backend
Update the ConfigMap in your cluster by using
kubectl apply
:$ kubectl apply -f example-configmap.yaml
-
To have the properties apply to all services that the Ingress routes traffic to, add the
cr-backend
annotation to your Ingress definition . The Ingress below applies to the services example-service1 and example-service2:example-ingress.yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress annotations: haproxy.org/cr-backend: default/example-backend spec: rules: - http: paths: - path: /example1 pathType: Prefix backend: service: name: example-service1 port: number: 80 - path: /example2 pathType: Prefix backend: service: name: example-service2 port: number: 80
Update the Ingress in your cluster by using
kubectl apply
:$ kubectl apply -f example-ingress.yaml
-
To have the properties apply to only a specific Kubernetes service, add the
cr-backend
annotation to your Service definition. The Service below applies the properties only to the service named example-service1.example-service.yaml
apiVersion: v1 kind: Service metadata: labels: run: app name: example-service1 annotations: haproxy.org/cr-backend: default/example-backend spec: selector: run: app ports: - name: http port: 80 protocol: TCP targetPort: 8080
Update the Service in your cluster by using
kubectl apply
:$ kubectl apply -f example-service.yaml
-
Next up
Backend custom resource reference