Generate an HAProxy Configuration File

HAProxy is one of the most popular open-source TCP load balancers, and CockroachDB includes a built-in command for generating a configuration file that is preset to work with your running cluster.

To generate an HAProxy config file for a secure cluster, run the cockroach gen haproxy command, specifying the location of certificate directory and the address of any instance running a CockroachDB node:

The file is preset with the minimal configurations needed to work with your running cluster:

Field

Description

timeout connecttimeout clienttimeout server

Timeout values that should be suitable for most deployments.

bind

The port that HAProxy listens on. This is the port clients will connect to and thus needs to be allowed by your network configuration.

This tutorial assumes HAProxy is running on a separate machine from CockroachDB nodes. If you run HAProxy on the same machine as a node (not recommended), you'll need to change this port, as 26257 is likely already being used by the CockroachDB node.

balance

The balancing algorithm. This is set to roundrobin to ensure that connections get rotated amongst nodes (connection 1 on node 1, connection 2 on node 2, etc.). Check the HAProxy Configuration Manual for details about this and other balancing algorithms.

option httpchk

The HTTP endpoint that HAProxy uses to check node health. /health?ready=1 ensures that HAProxy doesn't direct traffic to nodes that are live but not ready to receive requests.

server

For each node in the cluster, this field specifies the interface that the node listens on, i.e., the address passed in the --host flag on node startup.