HAProxy Enterprise Documentation 2.3r1

show servers state

Get a block of data that represents the current of servers in backends. Use this to store the state in a state file on the filesystem.

Description

This command returns the state of the servers defined in the running HAProxy Enterprise instance. 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

You can store either one large file that contains that 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:

backend webservers
   server s1 127.0.0.1:8080 check weight 100

backend apiservers
   server s2 127.0.0.1:8081 check weight 100

Store all server states in a single file

Follow these steps to store the state for all backends in a single file.

  1. In your configuration file, add a server-state-file directive to the global section.

    This indicates the name of the file that holds the current state of your servers.

    global
       server-state-file /tmp/my-state-file
  2. In a defaults section, set the load-server-state-from-file directive to global, which indicates that HAProxy Enterprise should load the contents from the file pointed at by the server-state-file directive.

    defaults
       load-server-state-from-file global
  3. Edit the HAProxy Enterprise service file.

    • Call sudo systemctl edit --full hapee-2.3-lb, which opens a text editor.

    • Add a new ExecReload line that calls the show servers state Runtime API command. It must be placed before any calls to /bin/kill:

    [Service]
    Environment="CONFIG=/etc/hapee-2.3/hapee-lb.cfg" "PIDFILE=/run/hapee-2.3-lb.pid"
    EnvironmentFile=/etc/default/hapee-2.3-lb
    ExecStart=/opt/hapee-2.3/sbin/hapee-lb -Ws -f $CONFIG -p $PIDFILE $OPTIONS
    ExecReload=/opt/hapee-2.3/sbin/hapee-lb -c -f $CONFIG
    ExecReload=/bin/sh -c 'echo "show servers state" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock > /tmp/my-state-file'
    ExecReload=/bin/kill -USR2 $MAINPID
    KillMode=mixed
    Type=notify
  4. Reload the Systemd unit file and restart the HAProxy Enterprise service:

    $ sudo systemctl daemon-reload
    $ sudo systemctl restart hapee-2.3-lb
  5. Try changing the weight of a server and then reload the service. Check that the weight was preserved:

    $ echo "set weight webservers/s1 50" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock
    
    $ sudo systemctl reload hapee-2.3-lb
    
    $ echo "get weight webservers/s1" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock
    50 (initial 100)

Store server states in separate files

Follow these steps to store the state for each backend separately.

  1. In your configuration file, add a server-state-base directive to the global section.

    This indicates the path to the directory where state files will be stored.

    global
       server-state-base /tmp/
  2. In a defaults section, set the load-server-state-from-file directive to local, which indicates that HAProxy Enterprise should load files from the directory pointed at by the server-state-base directive.

    The files should have the same name as each backend.

    defaults
       load-server-state-from-file local
  3. Edit the HAProxy Enterprise service file.

    • Call sudo systemctl edit --full hapee-2.3-lb, which opens a text editor.

    • Add a new ExecReload line that calls the show 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:

    [Service]
    Environment="CONFIG=/etc/hapee-2.3/hapee-lb.cfg" "PIDFILE=/run/hapee-2.3-lb.pid"
    EnvironmentFile=/etc/default/hapee-2.3-lb
    ExecStart=/opt/hapee-2.3/sbin/hapee-lb -Ws -f $CONFIG -p $PIDFILE $OPTIONS
    ExecReload=/opt/hapee-2.3/sbin/hapee-lb -c -f $CONFIG
    ExecReload=/bin/sh -c 'echo "show servers state webservers" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock > /tmp/webservers'
    ExecReload=/bin/sh -c 'echo "show servers state apiservers" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock > /tmp/apiservers'
    ExecReload=/bin/kill -USR2 $MAINPID
    KillMode=mixed
    Type=notify
  4. Reload the Systemd unit file and restart the HAProxy Enterprise service:

    $ sudo systemctl daemon-reload
    $ sudo systemctl restart hapee-2.3-lb
  5. Try changing the weight of a server and then reload the service. Check that the weight was preserved:

    $ echo "set weight webservers/s1 50" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock
    
    $ sudo systemctl reload hapee-2.3-lb
    
    $ echo "get weight webservers/s1" | sudo socat stdio /var/run/hapee-2.3/hapee-lb.sock
    50 (initial 100)
  6. Optionally, set the server-state-file-name directive in a backend 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 to webservers-state. You must update the show servers state command to reflect this.

    backend webservers
       server-state-file-name webservers-state

See also


Next up

show sess