If you have used online media website Netflix, you might have thought how they are streaming movie live which needs lot of video data transfer. think about you are getting streamed at Mbps speed. If we assume fair possibility of 10,000 users at any time, then server needs to handle 10's of Gbps. Isn't it very big number? How this is possible? This is possible using software which balances load. nginx is such open source software..
NGINX is a free, open-source, high-performance HTTP server and reverse proxy.
nginx can be deployed in Linux platform. For ubuntu, its easier to deploy using apt-get
Moreover, it can be deployed as Linux container (docker).
Install nginx using apt-get:
Install HAproxy
apt-get install haproxy
We need to enable HAProxy to be started by the init script /etc/default/haproxy. Set ENABLED option to 1 as:
ENABLED=1
To verify if this change is done properly, execute the init script of HAProxy without any parameters. You should see the following:
$ service haproxy <press_tab_key>
reload restart start status stop
HAProxy is now installed.
Configuring HAProxy
/etc/haproxy/haproxy.cfg carries the configuration for haproxy. Note that in below sample, two backend servers are reachable via 10.102.53.233 and 10.102.169.145
Sample haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
contimeout 5000
clitimeout 50000
srvtimeout 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend haproxy_in
bind *:80
default_backend haproxy_http
backend haproxy_http
balance roundrobin
mode http
server vm1 10.102.53.233:80 check
server vm2 10.102.169.145:80 check
To apply configuration, restart HAProxy daemon
Apply config
nsroot@ubuntu:~$ sudo service haproxy restart
* Restarting haproxy haproxy [ OK ]
nsroot@ubuntu:~$ service haproxy status
haproxy is running.
Testing HAProxy is straightforward. Here you need to perform http via web browser or using linux curl tool
from within the host ubuntu as shown in below example
Test output
nsroot@ubuntu:~$ curl http://localhost | less
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 11510 100 11510 0 0 1027k 0 --:--:-- --:--:-- --:--:-- 1124k
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2014-03-19
See: https://launchpad.net/bugs/1288690
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache2 Ubuntu Default Page: It works</title>
<style type="text/css" media="screen">
* {
Sample haproxy.cfg
global
daemon
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend haproxy_in
bind *:80
default_backend haproxy_http
backend haproxy_http
balance roundrobin
mode http
server vm1 10.102.53.233:80 check
server vm2 10.102.169.145:80 check
Create HAProxy docker instance
HAProxy docker in host mode
[root@ubuntu /personal/]# docker run -dt -p 80 --net=host -v /home/haproxy/:/usr/local/etc/haproxy/:ro --name deepakk_haproxy_host haproxy
823490cb4def4601f11508fcc3f8a21cc5a0ae8edf29c63bdcc8d2b9ad5f1240
[root@ubuntu /personal/]# docker ps | grep deepakk_haproxy_host
823490cb4def haproxy "/docker-entrypoint.s" 9 seconds ago Up 8 seconds deepakk_haproxy_host
HAProxy docker in bridge mode
root@ubuntu:~# docker run -dt -p 1133:80 -v /home/haproxy/:/usr/local/etc/haproxy/:ro --name deepakk_haproxy haproxy
1d039a2239e4002d2c6fd43bb091b72bba595959e76d2b18a1819378c1057369
root@ubuntu:~# docker ps | grep deepakk_haproxy
1d039a2239e4 haproxy "/docker-entrypoint.s" 15 seconds ago Up 15 seconds 0.0.0.0:1133->80/tcp deepakk_haproxy
To see running haproxy
Check haproxy running
root@ubuntu:~# docker exec -it deepakk_haproxy /bin/bash
root@1d039a2239e4:/# ps -aef | grep haproxy
root 1 0 0 14:54 ? 00:00:00 /usr/local/sbin/haproxy-systemd-wrapper -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg
root 12 1 0 14:54 ? 00:00:00 /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
root 13 12 0 14:54 ? 00:00:00 /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
root 25 14 0 14:56 ? 00:00:00 grep haproxy
Testing HAProxy is straightforward. Here you need to perform http via web browser or using linux curl tool
from within the host ubuntu as shown in below example
Sample test output
root@ubuntu:~# curl http://localhost:1133 | less
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Modified from the Debian original for Ubuntu
Last updated: 2014-03-19
See: https://launchpad.net/bugs/1288690
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Apache2 Ubuntu Default Page: It works</title>
<style type="text/css" media="screen">
* {
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
body, html {
padding: 3px 3px 3px 3px;
background-color: #D8DBE2;
font-family: Verdana, sans-serif;
font-size: 11pt;
text-align: center;
}
div.main_page {
position: relative;
display: table;
width: 800px;
To refresh config, docker kill -s SIGHUP <HAProxy ID> is used
Pull nginx docker image.
I assumed that docker is installed in your ubuntu box. If not, first deploy docker. And then pull Nginx
Install nginx
root@deepakk-HVM-domU:/personal# docker images | grep nginx
root@deepakk-HVM-domU:/personal# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
94ed0c431eb5: Pull complete
9406c100a1c3: Pull complete
aa74daafd50c: Pull complete
Digest: sha256:788fa27763db6d69ad3444e8ba72f947df9e7e163bad7c1f5614f8fd27a311c3
Status: Downloaded newer image for nginx:latest
root@deepakk-HVM-domU:/personal# docker images | grep nginx
nginx latest b8efb18f159b 6 days ago 107.5 MB
Install Nginx
root@kubenode1:~# docker run --name some-nginx2 -p 8081:80 -v /some/content:/usr/share/nginx/html:ro -d nginx:1.13
4fd3c3ca5e1ade711f069704df06727c1fa15b238d5b14090919b4941db65861
root@kubenode1:~# curl -k http://localhost:8081
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.13.12</center>
</body>
</html>
https://blog.docker.com/2015/04/tips-for-deploying-nginx-official-image-with-docker//
https://hub.docker.com/_/nginx