Reference
show servers state
Get a block of data that represents the current state of servers in backends. Use this to store the state in a state file
on the filesystem.
Description Jump to heading
This command returns the state of the servers defined in the running load balancer. You would store this data in a file known as a state file
, which the load balancer will read during a process reload, ensuring that the state is preserved and reapplied.
Storing server state preserves the servers’ IP addresses, weights, and drain or maintenance mode.
Examples Jump to heading
You can store either one large file that contains the state of all backends or separate files for each backend, depending on how you set the load-server-state-from-file
directive.
For these examples, assume we have the following backend
sections defined in the configuration:
haproxy
backend webserversserver s1 127.0.0.1:8080 check weight 100backend apiserversserver s2 127.0.0.1:8081 check weight 100
haproxy
backend webserversserver s1 127.0.0.1:8080 check weight 100backend apiserversserver s2 127.0.0.1:8081 check weight 100
Store all server states in a single file Jump to heading
Follow these steps to store the state for all backends in a single file.
-
In your configuration file, add a
server-state-file
directive to theglobal
section.This indicates the name of the file that holds the current state of your servers.
haproxyglobalserver-state-file /tmp/my-state-filehaproxyglobalserver-state-file /tmp/my-state-file -
In a
defaults
section, set theload-server-state-from-file
directive toglobal
, which indicates that the load balancer should load the contents from the file pointed at by theserver-state-file
directive.haproxydefaultsload-server-state-from-file globalhaproxydefaultsload-server-state-from-file global -
Edit the load balancer service file.
- Call
sudo systemctl edit --full hapee-3.0-lb
, which opens a text editor. - Add a new
ExecReload
line that calls theshow servers state
Runtime API command. It must be placed before any calls to/bin/kill
:
nix[Service]Environment="CONFIG=/etc/hapee-3.0/hapee-lb.cfg" "PIDFILE=/run/hapee-3.0-lb.pid"EnvironmentFile=/etc/default/hapee-3.0-lbExecStart=/opt/hapee-3.0/sbin/hapee-lb -Ws -f $CONFIG -p $PIDFILE $OPTIONSExecReload=/opt/hapee-3.0/sbin/hapee-lb -c -f $CONFIGExecReload=/bin/sh -c 'echo "show servers state" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock > /tmp/my-state-file'ExecReload=/bin/kill -USR2 $MAINPIDKillMode=mixedType=notifynix[Service]Environment="CONFIG=/etc/hapee-3.0/hapee-lb.cfg" "PIDFILE=/run/hapee-3.0-lb.pid"EnvironmentFile=/etc/default/hapee-3.0-lbExecStart=/opt/hapee-3.0/sbin/hapee-lb -Ws -f $CONFIG -p $PIDFILE $OPTIONSExecReload=/opt/hapee-3.0/sbin/hapee-lb -c -f $CONFIGExecReload=/bin/sh -c 'echo "show servers state" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock > /tmp/my-state-file'ExecReload=/bin/kill -USR2 $MAINPIDKillMode=mixedType=notify - Call
-
Reload the Systemd unit file and restart the load balancer service:
nixsudo systemctl daemon-reloadsudo systemctl restart hapee-3.0-lbnixsudo systemctl daemon-reloadsudo systemctl restart hapee-3.0-lb -
Try changing the weight of a server and then reload the service. Check that the weight was preserved:
nixecho "set weight webservers/s1 50" | \sudo socat stdio tcp4-connect:127.0.0.1:9999nixecho "set weight webservers/s1 50" | \sudo socat stdio tcp4-connect:127.0.0.1:9999nixsudo systemctl reload hapee-3.0-lbnixsudo systemctl reload hapee-3.0-lbnixecho "get weight webservers/s1" | \sudo socat stdio tcp4-connect:127.0.0.1:9999nixecho "get weight webservers/s1" | \sudo socat stdio tcp4-connect:127.0.0.1:9999outputtext50 (initial 100)outputtext50 (initial 100)
Store server states in separate files Jump to heading
Follow these steps to store the state for each backend separately.
-
In your configuration file, add a
server-state-base
directive to theglobal
section.This indicates the path to the directory where state files will be stored.
haproxyglobalserver-state-base /tmp/haproxyglobalserver-state-base /tmp/ -
In a
defaults
section, set theload-server-state-from-file
directive tolocal
, which indicates that the load balancer should load files from the directory pointed at by theserver-state-base
directive.The files should have the same name as each backend.
haproxydefaultsload-server-state-from-file localhaproxydefaultsload-server-state-from-file local -
Edit the load balancer service file.
- Call
sudo systemctl edit --full hapee-3.0-lb
, which opens a text editor. - Add a new
ExecReload
line that calls theshow servers state
Runtime API command for each backend for which you want to store state. All of these lines must be placed before any calls to/bin/kill
.
In the example below, we store two state files:
/tmp/webservers
and/tmp/apiservers
:nix[Service]Environment="CONFIG=/etc/hapee-3.0/hapee-lb.cfg" "PIDFILE=/run/hapee-3.0-lb.pid"EnvironmentFile=/etc/default/hapee-3.0-lbExecStart=/opt/hapee-3.0/sbin/hapee-lb -Ws -f $CONFIG -p $PIDFILE $OPTIONSExecReload=/opt/hapee-3.0/sbin/hapee-lb -c -f $CONFIGExecReload=/bin/sh -c 'echo "show servers state webservers" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock > /tmp/webservers'ExecReload=/bin/sh -c 'echo "show servers state apiservers" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock > /tmp/apiservers'ExecReload=/bin/kill -USR2 $MAINPIDKillMode=mixedType=notifynix[Service]Environment="CONFIG=/etc/hapee-3.0/hapee-lb.cfg" "PIDFILE=/run/hapee-3.0-lb.pid"EnvironmentFile=/etc/default/hapee-3.0-lbExecStart=/opt/hapee-3.0/sbin/hapee-lb -Ws -f $CONFIG -p $PIDFILE $OPTIONSExecReload=/opt/hapee-3.0/sbin/hapee-lb -c -f $CONFIGExecReload=/bin/sh -c 'echo "show servers state webservers" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock > /tmp/webservers'ExecReload=/bin/sh -c 'echo "show servers state apiservers" | sudo socat stdio unix-connect:/var/run/hapee-3.0/hapee-lb.sock > /tmp/apiservers'ExecReload=/bin/kill -USR2 $MAINPIDKillMode=mixedType=notify - Call
-
Reload the Systemd unit file and restart the load balancer service:
nixsudo systemctl daemon-reloadnixsudo systemctl daemon-reloadnixsudo systemctl restart hapee-3.0-lbnixsudo systemctl restart hapee-3.0-lb -
Try changing the weight of a server and then reload the service. Check that the weight was preserved:
nixecho "set weight webservers/s1 50" | \sudo socat stdio tcp4-connect:127.0.0.1:9999nixecho "set weight webservers/s1 50" | \sudo socat stdio tcp4-connect:127.0.0.1:9999nixsudo systemctl reload hapee-3.0-lbnixsudo systemctl reload hapee-3.0-lbnixecho "get weight webservers/s1" | \sudo socat stdio tcp4-connect:127.0.0.1:9999nixecho "get weight webservers/s1" | \sudo socat stdio tcp4-connect:127.0.0.1:9999outputtext50 (initial 100)outputtext50 (initial 100) -
Optional: Set the
server-state-file-name
directive in abackend
to change the filename used to store state for that backend.In the example below, the file would normally be named
webservers
, but is changed towebservers-state
. You must update theshow servers state
command to reflect this.haproxybackend webserversserver-state-file-name webservers-statehaproxybackend webserversserver-state-file-name webservers-state
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?