Guides

Add a load balancer server

A load balancer (LB) is a second server that runs streams and serves them to viewers. The panel installs a program called the agent on it. This document explains the installation from the panel, what the agent does, the network rules and the checks.

Before you start

  • The main server is installed and healthy.
  • The new server runs Debian or Ubuntu with systemd and has a public IPv4 address.
  • You have the root SSH password of the new server. The panel installs the agent over SSH with a password.
  • Root login with a password is allowed on the new server during the installation.
  • The firewall rules in the section "Firewall rules" are possible with your provider.
  • Your license is valid. With an overdue license check the panel refuses to add a server.

Which method to use

Use the panel. The panel writes the complete agent configuration, grants database access and sets up nginx on the new server.

Do not use the script install-lb.sh from the install package for a new load balancer. Answer no to the installer question Add LB now? (y/n) [n]:, which starts that script. An agent that was set up by the script alone does not have the settings it needs and stops with [Agent] AGENT_SERVER_ID is required and must be > 0.

Steps

1. Add the server in the panel

  1. Log in to the panel as an administrator.
  2. Open Servers and choose Add Server.
  3. Enter a name and the IP address of the new server. Enter the SSH user (root), the SSH password and the SSH port (22 unless you changed it).
  4. Save.

Possible message: Name and IP are required.

2. Install the agent

Start Install Server for the new entry. The panel then runs nine steps on the new server over SSH:

StepAction
1/9apt-get update and installation of ffmpeg curl wget htop iftop net-tools.
2/9Creates /opt/xtreamgo-agent, /app/data/{hls,vod,series,catchup,tmp,tv_archive}, /app/logs and /home/xtreamgo/{content,images,wwwdir}.
3/9Copies the agent program to /opt/xtreamgo-agent/xtreamgo-agent.
4/9Writes /opt/xtreamgo-agent/.env with the server ID, a generated API key, the ports, the database address of the main server and the panel URL.
5/9Creates /etc/systemd/system/xtreamgo-agent.service.
6/9Creates a database user for the IP of the new server with the rights SELECT, INSERT, UPDATE and DELETE on the panel database.
7/9Installs nginx on the new server. nginx listens on port 80 and forwards to the agent.
8/9Moves the agent to port 8080 when the HTTP port of the server is 80.
9/9Enables and starts xtreamgo-agent.

The panel then waits up to 60 seconds for the first heartbeat of the agent and installs the signed media runtime (ffmpeg and ffprobe) on the new server. The installation log ends with Agent heartbeat received! Installation successful.

3. Assign streams

Choose in the panel which streams run on the new server.

What the agent does

  • It connects to the database of the main server and reads its own entry in the server list.
  • It writes a heartbeat with load figures into the database every 30 seconds. The panel shows a server as online from this heartbeat.
  • It starts and stops ffmpeg for the streams assigned to it and writes HLS segments to /app/data/hls.
  • It serves /live/, /movie/, /series/ and HLS requests from viewers on its HTTP port. The main server answers a viewer request with a redirect to the load balancer when the stream runs there.
  • It forwards requests that belong to the panel to the panel URL.
  • It listens on a control port (6661 by default) for commands from the panel. Each command must carry the API key of the server.
  • It accepts a new version of itself from the panel only when the file carries a valid signature. After a self-update it watches the service for 60 seconds and puts the previous program back when the new one does not stay up.

Firewall rules

FromToPortNeeded for
Main serverLoad balancerSSH port (22)Installation and agent updates.
Main serverLoad balancer6661/tcpAgent control API.
Load balancerMain server3306/tcpDatabase.
Load balancerMain serverHTTP port of the panel (80)Forwarded panel requests.
ViewersLoad balancer80/tcpStreams.

Port 3306 on the main server:

  • The database container listens on 127.0.0.1:3306 only.
  • The timer xtreamgo-db-access-sync.timer runs /home/xtreamgo/scripts/db_lb_access_sync.sh every 20 seconds.
  • When at least one load balancer is registered, the script starts the service xtreamgo-db-forward (a socat forwarder on the first IP address of the main server) and allows port 3306 for the load balancer IPs only. All other sources are dropped by the iptables chain XTREAMGO_DBACCESS.
  • When no load balancer is registered, the forwarder is stopped and port 3306 stays closed.

You do not need to add these rules on the main server by hand. If your provider has a cloud firewall, allow port 3306 from the load balancer IP there.

The panel does not configure a firewall on the load balancer. Do it manually with the rules in the table above. Where you can, allow port 6661 from the main server only. The port listens on all interfaces and is protected by the API key.

How to check it worked

On the load balancer:

bash
systemctl is-active xtreamgo-agent
journalctl -u xtreamgo-agent --no-pager -n 30
grep -E '^AGENT_(SERVER_ID|HTTP_PORT|API_PORT)=' /opt/xtreamgo-agent/.env
curl -s http://127.0.0.1/health
ss -ltnp | grep -E ':(80|8080|6661)\s'

Expected results: active, no line that starts with [Agent] Failed, the three settings, {"status":"ok"}, and listeners on port 80 (nginx), on the agent HTTP port and on 6661.

On the main server:

bash
journalctl -u xtreamgo-db-access-sync --no-pager -n 5
systemctl is-active xtreamgo-db-forward
iptables -S XTREAMGO_DBACCESS

Expected results: a line that starts with [db-access-sync] ok and lists the load balancer IP, active, and one ACCEPT rule per load balancer IP followed by a DROP rule.

In the panel: the server shows as online on the Servers page.

To verify that streams run on the load balancer:

bash
# on the load balancer
pgrep -a ffmpeg | head
ls /app/data/hls | head

# from any machine, with a real subscriber line and a stream assigned to the load balancer
curl -sI "http://<main-server>/live/<username>/<password>/<stream-id>.ts" | grep -iE '^(HTTP|Location)'

Expected results: ffmpeg processes for the assigned streams, one directory or file set per running stream, and a 302 answer whose Location header points at the load balancer.

If something goes wrong

MessageMeaningAction
The license check with the license server is overdue, so adding a server is paused.The panel has not completed a recent license check.See License.
Cannot connect to server via SSHThe panel cannot log in to the new server.Check IP, SSH port, user and password. Allow root login with a password. Check the provider firewall.
Server SSH password not configuredThe server entry has no SSH password.Edit the server and enter the password.
Server is already installed. Use force_reinstall=true to reinstallThe entry is marked as installed.Use the Reinstall action.
Cannot install agent on the main panel serverYou chose the main server.Choose the load balancer entry.
Agent binary not found at /app/xtreamgo-agentThe panel has no agent program to copy.Contact XtreamGo support.
ERROR: SCP transfer failedThe agent file could not be copied. An existing agent is left untouched.Check disk space and SSH access on the new server.
Media-runtime gate FAILED: ... server NOT marked installed.The signed ffmpeg runtime could not be installed.Send the installation log to XtreamGo support.
Agent service not active. followed by agent logsThe agent stopped after start.Read the lines below and act on them.
[Agent] Failed to connect to database or [Agent] Failed to ping databaseThe load balancer cannot reach port 3306 on the main server.Check the provider firewall. On the main server run the three check commands above. Check DB_HOST in /opt/xtreamgo-agent/.env.
[Agent] Server ID <n> not found in streaming_serversThe server entry was deleted in the panel.Add the server again and reinstall.
[db-access-sync] WARN: LB query failedThe sync script could not read the server list. It keeps the current state.Check that xtreamgo-db is healthy.
Server shows offline but the agent is activeNo heartbeat reaches the database.Check port 3306 from the load balancer: timeout 3 bash -c '</dev/tcp/<main-ip>/3306' && echo open.

Back to documentation