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:
-
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.yamland add a key namedhaproxy-auxiliary.cfg. The value defines acacheobject. 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.yamlyamlapiVersion: v1kind: ConfigMapmetadata:name: haproxy-auxiliary-configmapnamespace: haproxy-controllerdata:haproxy-auxiliary.cfg: |cache mycachetotal-max-size 50max-object-size 3000max-age 30haproxy-auxiliary-configmap.yamlyamlapiVersion: v1kind: ConfigMapmetadata:name: haproxy-auxiliary-configmapnamespace: haproxy-controllerdata:haproxy-auxiliary.cfg: |cache mycachetotal-max-size 50max-object-size 3000max-age 30 -
Apply the change with
kubectl apply.nixkubectl apply -f haproxy-auxiliary-configmap.yamlnixkubectl apply -f haproxy-auxiliary-configmap.yamloutputtextconfigmap/haproxy-auxiliary-configmap createdoutputtextconfigmap/haproxy-auxiliary-configmap created -
Create the
haproxy-controllernamespace.nixkubectl create namespace haproxy-controllernixkubectl create namespace haproxy-controlleroutputtextnamespace/haproxy-controller createdoutputtextnamespace/haproxy-controller created -
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.yamlthat adds the ConfigMap as a volume. ThemountPathmust be set to/usr/local/etc/haproxy/haproxy-aux.cfgfor both the Community and Enterprise versions of the ingress controller.values.yamlyamlcontroller:extraVolumes:- name: haproxy-auxiliary-volumeconfigMap:name: haproxy-auxiliary-configmapextraVolumeMounts:- name: haproxy-auxiliary-volumemountPath: /usr/local/etc/haproxy/haproxy-aux.cfgsubPath: haproxy-auxiliary.cfgvalues.yamlyamlcontroller:extraVolumes:- name: haproxy-auxiliary-volumeconfigMap:name: haproxy-auxiliary-configmapextraVolumeMounts:- name: haproxy-auxiliary-volumemountPath: /usr/local/etc/haproxy/haproxy-aux.cfgsubPath: haproxy-auxiliary.cfgb. Uninstall the Helm chart if you installed it previously.
nixhelm uninstall haproxy-kubernetes-ingress --namespace haproxy-controllernixhelm uninstall haproxy-kubernetes-ingress --namespace haproxy-controllerc. Install the Helm chart, referencing the
values.yamlfile with the-fflag.nixhelm install haproxy-kubernetes-ingress haproxytech/kubernetes-ingress -f values.yaml --namespace haproxy-controllernixhelm install haproxy-kubernetes-ingress haproxytech/kubernetes-ingress -f values.yaml --namespace haproxy-controller -
In our example, we’re adding a new
cacheconfiguration section. To use it, abackendmust reference it withhttp-request cache-useandhttp-response cache-storedirectives.-
In versions 3.4 and newer, use a Backend custom resource to add the directives.
example-backend-crd.yamlyamlapiVersion: "ingress.v3.haproxy.org/v3"kind: Backendmetadata:name: example-backend-crdnamespace: defaultspec:filter_list:- type: cachecache_name: mycachehttp_request_rule_list:- type: cache-usecache_name: mycachehttp_response_rule_list:- type: cache-storecache_name: mycacheexample-backend-crd.yamlyamlapiVersion: "ingress.v3.haproxy.org/v3"kind: Backendmetadata:name: example-backend-crdnamespace: defaultspec:filter_list:- type: cachecache_name: mycachehttp_request_rule_list:- type: cache-usecache_name: mycachehttp_response_rule_list:- type: cache-storecache_name: mycacheApply the change with
kubectl apply.nixkubectl apply -f example-backend-crd.yamlnixkubectl apply -f example-backend-crd.yamlAdd the
cr-backendannotation to your Ingress resource.example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:haproxy.org/cr-backend: default/example-backend-crd...example-ingress.yamlyamlapiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: example-ingressannotations:haproxy.org/cr-backend: default/example-backend-crd...Apply the change with
kubectl apply.nixkubectl apply -f example-ingress.yamlnixkubectl apply -f example-ingress.yaml -
In versions older than 3.4, add a
backend-config-snippetannotation to your application’sServiceresource to add the directives.example-service.yamlyamlapiVersion: v1kind: Servicemetadata:labels:run: apiname: apiannotations:haproxy.org/backend-config-snippet: |http-request cache-use mycachehttp-response cache-store mycacheexample-service.yamlyamlapiVersion: v1kind: Servicemetadata:labels:run: apiname: apiannotations:haproxy.org/backend-config-snippet: |http-request cache-use mycachehttp-response cache-store mycache
-
Update the configuration file Jump to heading
Follow these steps to update the auxiliary configuration file later.
-
Call
kubectl editto edit the ConfigMap.nixkubectl edit configmap haproxy-auxiliary-configmap --namespace haproxy-controllernixkubectl edit configmap haproxy-auxiliary-configmap --namespace haproxy-controller -
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.