Splash集群部署方案

既然Splash是渲染动态页面的神器,批量部署服务器的方法页不能少

【方法1】 Nginx部署

一台专职主机Centos7来管理 负载均衡机群

下载与安装nginx

前提,必须开放管理机的8050端口与80端口

把下载文件下载到src中, 这里是1.15.7版本

1
wget http://nginx.org/download/nginx-1.15.7.tar.gz -P /usr/src

解压压缩包

1
tar xf 文件名

进目录解压好的目录中

1
cd 目录

安装必要命令工具(gcc编译器 pcre重写功能 zlib解压工具)

1
yum -y install gcc pcre-devel zlib zlib-devel

指定软件安装路径

1
./configure --prefix=/usr/local/nginx

安装
make and make install

注意,如果中途出现error那么请从头再来!!!


配置与启动

  1. 进入 nginx的sbin目录 输入./nginx -t检查配置文件位置与状态
  2. 根据位置,vim 编辑 配置文件. 这是我默认安装的位置 vim /usr/local/nginx/conf/nginx.conf

最闲均衡配置

least_conn代表最少链接负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
upstream splash {
least_conn;
server 41.159.27.223:8050;
server 41.159.27.221:8050;
server 41.159.27.9:8050;
server 41.159.117.119:8050;
}
server {
listen 8050;
location / {
proxy_pass http://splash;
}
}
}

权重均衡配置

按照设置权重优先均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http {
upstream splash {
server 41.159.27.223:8050 weight=4;
server 41.159.27.221:8050 weight=2;
server 41.159.27.9:8050 weight=2;
server 41.159.117.119:8050 weight=1;
}
server {
listen 8050;
location / {
proxy_pass http://splash;
}
}
}

IP散列负载均衡

设置后会让相同连续请求分配给同一台机器,比如淘宝登陆后会继续用登陆那台均衡继续操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
upstream splash {
ip_hash;
server 41.159.27.223:8050;
server 41.159.27.221:8050;
server 41.159.27.9:8050;
server 41.159.117.119:8050;
}
server {
listen 8050;
location / {
proxy_pass http://splash;
}
}
}

  1. 验证80端口是否被占用 netstat -ntpl (如果没被占用可以进行下一步)
  2. /usr/local/nginx/sbin/nginx 指定路径启动
  3. /usr/local/nginx/sbin/nginx -s reload 重启命令 (备用)
  4. 干掉nginx命令 kill -QUIT id

【方法2】 HAproxy 均衡负载

可用利用HAproxy 对Splash进行批量部署与观察

【方法3】 Ansible 均衡负载

Ansible 也可以做批量 均衡负载

【测试效果】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import requests
from urllib.parse import quote
import re

lua = '''
function main(splash, args)
local treat = require("treat")
local response = splash:http_get("http://httpbin.org/get")
return treat.as_string(response.body)
end
'''

url = 'http://splash:8050/execute?lua_source=' + quote(lua)
response = requests.get(url, auth=('admin', 'admin'))
ip = re.search('(\d+\.\d+\.\d+\.\d+)', response.text).group(1)
print(ip)

将最后url 地址 换成 负载均衡管理主机ip即可


每一次响应的 ip都不同,则配置成功