Ingress tutorials

Add an auxiliary configuration

This page applies to:

  • HAProxy Kubernetes Ingress Controller 1.6 and newer
  • HAProxy Enterprise Kubernetes Ingress Controller 1.6 and newer

For the most part, the ingress controller will handle the underlying HAProxy configuration file and you only need to set annotations on your Ingress, Service, and ConfigMap resources. However, it’s still possible to write raw HAProxy configuration directives for features not yet covered by annotations.

You can provide an auxiliary configuration file that defines entirely new config sections, such as to define cache, mailers, or ring sections. This file will be loaded when the ingress controller starts up.

You can thus:

  • Configure anything not supported by Ingress Controller annotations.
  • Migrate a legacy HAProxy Enterprise configuration to the HAProxy Enterprise Kubernetes Ingress Controller.

Primary and auxiliary configuration files Jump to heading

Here are the differences between the two types of configuration files:

Filename Description Controlled by
haproxy.cfg Reflects the state of pods and services in your Kubernetes cluster. HAProxy Kubernetes Ingress Controller
haproxy-aux.cfg Supports additional HAProxy Enterprise directives. Kubernetes administrator

Deploy the ingress controller with the auxiliary config Jump to heading

To define the auxiliary configuration file and attach it to your ingress controller:

  1. Create a ConfigMap that defines what you want to add to your HAProxy configuration. A ConfigMap is an API object for storing non-confidential information in key-value pairs that you can provide to the ingress controller. In this example, we create a file named haproxy-auxiliary-configmap.yaml and add a key named haproxy-auxiliary.cfg. The value defines a cache object. Note that a large cache object can increase the pod’s startup time, so we set its maximum size to 50MB in this case.

    haproxy-auxiliary-configmap.yaml
    yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: haproxy-auxiliary-configmap
    namespace: haproxy-controller
    data:
    haproxy-auxiliary.cfg: |
    cache mycache
    total-max-size 50
    max-object-size 3000
    max-age 30
    haproxy-auxiliary-configmap.yaml
    yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: haproxy-auxiliary-configmap
    namespace: haproxy-controller
    data:
    haproxy-auxiliary.cfg: |
    cache mycache
    total-max-size 50
    max-object-size 3000
    max-age 30
  2. Apply the change with kubectl apply.

    nix
    kubectl apply -f haproxy-auxiliary-configmap.yaml
    nix
    kubectl apply -f haproxy-auxiliary-configmap.yaml
    output
    text
    configmap/haproxy-auxiliary-configmap created
    output
    text
    configmap/haproxy-auxiliary-configmap created
  3. Create the haproxy-controller namespace.

    nix
    kubectl create namespace haproxy-controller
    nix
    kubectl create namespace haproxy-controller
    output
    text
    namespace/haproxy-controller created
    output
    text
    namespace/haproxy-controller created
  4. Deploy the ingress controller again, this time with the ConfigMap attached as a volume. Below we show an example using Helm.

    a. Create a file named values.yaml that adds the ConfigMap as a volume. The mountPath must be set to /usr/local/etc/haproxy/haproxy-aux.cfg for both the Community and Enterprise versions of the ingress controller.

    values.yaml
    yaml
    controller:
    extraVolumes:
    - name: haproxy-auxiliary-volume
    configMap:
    name: haproxy-auxiliary-configmap
    extraVolumeMounts:
    - name: haproxy-auxiliary-volume
    mountPath: /usr/local/etc/haproxy/haproxy-aux.cfg
    subPath: haproxy-auxiliary.cfg
    values.yaml
    yaml
    controller:
    extraVolumes:
    - name: haproxy-auxiliary-volume
    configMap:
    name: haproxy-auxiliary-configmap
    extraVolumeMounts:
    - name: haproxy-auxiliary-volume
    mountPath: /usr/local/etc/haproxy/haproxy-aux.cfg
    subPath: haproxy-auxiliary.cfg

    b. Uninstall the Helm chart if you installed it previously.

    nix
    helm uninstall haproxy-kubernetes-ingress --namespace haproxy-controller
    nix
    helm uninstall haproxy-kubernetes-ingress --namespace haproxy-controller

    c. Install the Helm chart, referencing the values.yaml file with the -f flag.

    nix
    helm install haproxy-kubernetes-ingress haproxytech/kubernetes-ingress -f values.yaml --namespace haproxy-controller
    nix
    helm install haproxy-kubernetes-ingress haproxytech/kubernetes-ingress -f values.yaml --namespace haproxy-controller
  5. In our example, we’re adding a new cache configuration section. To use it, a backend must reference it with http-request cache-use and http-response cache-store directives.

    • In versions 3.4 and newer, use a Backend custom resource to add the directives.

      example-backend-crd.yaml
      yaml
      apiVersion: "ingress.v3.haproxy.org/v3"
      kind: Backend
      metadata:
      name: example-backend-crd
      namespace: default
      spec:
      filter_list:
      - type: cache
      cache_name: mycache
      http_request_rule_list:
      - type: cache-use
      cache_name: mycache
      http_response_rule_list:
      - type: cache-store
      cache_name: mycache
      example-backend-crd.yaml
      yaml
      apiVersion: "ingress.v3.haproxy.org/v3"
      kind: Backend
      metadata:
      name: example-backend-crd
      namespace: default
      spec:
      filter_list:
      - type: cache
      cache_name: mycache
      http_request_rule_list:
      - type: cache-use
      cache_name: mycache
      http_response_rule_list:
      - type: cache-store
      cache_name: mycache

      Apply the change with kubectl apply.

      nix
      kubectl apply -f example-backend-crd.yaml
      nix
      kubectl apply -f example-backend-crd.yaml

      Add the cr-backend annotation to your Ingress resource.

      example-ingress.yaml
      yaml
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:
      name: example-ingress
      annotations:
      haproxy.org/cr-backend: default/example-backend-crd
      ...
      example-ingress.yaml
      yaml
      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:
      name: example-ingress
      annotations:
      haproxy.org/cr-backend: default/example-backend-crd
      ...

      Apply the change with kubectl apply.

      nix
      kubectl apply -f example-ingress.yaml
      nix
      kubectl apply -f example-ingress.yaml
    • In versions older than 3.4, add a backend-config-snippet annotation to your application’s Service resource to add the directives.

      example-service.yaml
      yaml
      apiVersion: v1
      kind: Service
      metadata:
      labels:
      run: api
      name: api
      annotations:
      haproxy.org/backend-config-snippet: |
      http-request cache-use mycache
      http-response cache-store mycache
      example-service.yaml
      yaml
      apiVersion: v1
      kind: Service
      metadata:
      labels:
      run: api
      name: api
      annotations:
      haproxy.org/backend-config-snippet: |
      http-request cache-use mycache
      http-response cache-store mycache

Update the configuration file Jump to heading

Follow these steps to update the auxiliary configuration file later.

  1. Call kubectl edit to edit the ConfigMap.

    nix
    kubectl edit configmap haproxy-auxiliary-configmap --namespace haproxy-controller
    nix
    kubectl edit configmap haproxy-auxiliary-configmap --namespace haproxy-controller
  2. The file opens in your text editor. Make changes to the file and then save and close it. The ingress controller will detect the change and reload the file.

See also Jump to heading

  • For an overview of the ConfigMaps key-value mapping object, see the Kubernetes documentation at ConfigMaps.

Do you have any suggestions on how we can improve the content of this page? Please leave feedback!