建立AB测试环境(第一部分)
我想尝试做的事情
我希望在Nginx上进行负载均衡,启动WEB#1 / WEB#2,并使用fluentd将Apache日志传输到日志服务器上,以便实时查看其内容,并创建AB测试环境的基础。
動機就是一個人進行行動或追求目標的原因或力量。
我经常看到一些以冗余配置方式运作的服务在WEB#1 / WEB#2上,所以我觉得通过确认自己发布的服务是否具有真正的价值,我可以提出技术建议。
搭建环境
像往常一样,使用Vagrant来建立虚拟环境。
config.vm.define "nginx" do |node|
node.vm.box = "centos7"
node.vm.network "forwarded_port", guest: 80, host: 8080
node.vm.network :private_network, ip: "192.168.33.10", virtualbox__intnet: "intnet"
end
config.vm.define "web1" do |node|
node.vm.box = "centos7"
#node.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2222
#node.vm.network "forwarded_port", guest: 80, host: 8080
node.vm.network :private_network, ip: "192.168.33.100", virtualbox__intnet: "intnet"
end
config.vm.define "web2" do |node|
node.vm.box = "centos7"
node.vm.network "forwarded_port", guest: 80, host: 8082
node.vm.network :private_network, ip: "192.168.33.102", virtualbox__intnet: "intnet"
end
安装Nginx
请参考以下链接,其中包含适用于 CentOS 的设置:
https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
$ sudo yum -y 安装nginx
$ nginx -v
安装的版本为nginx version: nginx/1.12.0
启动Nginx
$ sudo systemctl start nginx
$ sudo systemctl status nginx
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: active (running) since 火 2017-05-16 14:02:29 UTC; 6s ago
Docs: http://nginx.org/en/docs/
Process: 21017 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 21015 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 21019 (nginx)
CGroup: /system.slice/nginx.service
├─21019 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─21020 nginx: worker process
5月 16 14:02:28 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
5月 16 14:02:28 localhost.localdomain nginx[21015]: nginx: the configuration file /etc/nginx/nginx.conf... ok
5月 16 14:02:28 localhost.localdomain nginx[21015]: nginx: configuration file /etc/nginx/nginx.conf tes...ful
5月 16 14:02:28 localhost.localdomain systemd[1]: PID 20976 read from file /run/nginx.pid does not exist.
5月 16 14:02:29 localhost.localdomain systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
确认 nginx 是否正常工作
- http://localhost:8080
安装 Apache。
$ sudo systemctl stop firewalld
$ sudo ssytemctl disable firewalld
$ sudo yum install httpd
$ sudo systemctl status httpd
$ sudo systemctl start httpd
$ sudo systemctl status httpd
$ sudo systemctl enable httpd
$ sudo echo "<html><body>web1 running ...</body></html>" > /var/www/html/index.html
$ curl localhost
<html><body>web1 running ...</body></html>
$ sudo echo "<html><body>web2 running ...</body></html>" > /var/www/html/index.html
$ curl localhost
<html><body>web2 running ...</body></html>
负载均衡设置
将web#1或web#2配置成可以由nginx进行负载均衡的设置。
upstream myapp1 {
server 192.168.33.100;
server 192.168.33.102;
}
server {
listen 80;
location / {
set $do_not_cache 0;
proxy_pass http://myapp1;
}
}
我有点困扰。在上述文件夹中有一个 default.conf 文件,当我创建了 server.conf 文件后,无法参考 default.conf 的设置并进行负载均衡的确认。
$ sudo systemctl restart nginx
确认动作
http://本地主机:8080
最后一步 yī bù)
我想利用之前尝试过的 Fluentd(备忘录+其他)来创建一个使用 Apache 服务器日志进行 AB 测试准备的环境。