使用Haproxy搭建web群集
使用Haproxy搭建web群集
安装Nginx,配置两个Nginx网站
挂载Linux光盘
[[email protected] ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
将Nginx压缩包复制到第二台Nginx网站服务器
[[email protected] ~]# scp /mnt/nginx-1.6.0.tar.gz [email protected]:/usr/src/
将Haproxy压缩包复制到Haproxy服务器
[[email protected] ~]# scp /mnt/haproxy-1.4.24.tar.gz [email protected]:/usr/src/
解压Nginx文件到指定目录
[[email protected] ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/
挂载系统光盘,安装Nginx依赖软件
[[email protected] ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[[email protected] ~]# yum -y install pcre-devel zlib-devel
创建Nginx用户禁止登录
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
编译安装Nginx
[[email protected] ~]# cd /usr/src/nginx-1.6.0/
[[email protected] nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[[email protected] nginx-1.6.0]# make && make install
优化Nginx程序运行目录
[[email protected] ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
第二台Nginx网站服务相同配置
创建Nginx网站根目录文件
[[email protected] ~]# echo “www.bene.com” > /usr/local/nginx/html/index.html
[[email protected] ~]# echo “www.accp.com” > /usr/local/nginx/html/index.html
启动Nginx
[[email protected] ~]# nginx
[[email protected] ~]# nginx
验证Nginx网站服务器
编译安装Haproxy
挂载系统光盘
[[email protected] ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
/dev/sr0 已经挂载到 /mnt 上
安装Haproxy依赖软件
[[email protected] ~]# yum -y install pcre-devel bzip2-devel
解压Haproxy软件包
[[email protected] ~]# tar zxvf /usr/src/haproxy-1.4.24.tar.gz -C /usr/src/
编译Haproxy
[[email protected] ~]# cd /usr/src/haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# make TARGET=linux26
[[email protected] haproxy-1.4.24]# make install
生成Haproxy配置文件
创建保存Haproxy配置文件目录
[[email protected] ~]# mkdir /etc/haproxy
生成主配置文件
[[email protected] ~]# cd /usr/src/haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
创建Haproxy服务控制脚本执行权限
[[email protected] ~]# cd /usr/src/haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[[email protected] haproxy-1.4.24]# chmod +x /etc/init.d/haproxy
添加系统服务设置开机自动启动
[[email protected] ~]# chkconfig --add haproxy
[[email protected] ~]# chkconfig --level 35 haproxy on
优化程序执行命令
[[email protected] ~]# cd /usr/src/haproxy-1.4.24/
[[email protected] haproxy-1.4.24]# cp haproxy /usr/sbin/
创建服务运行的临时目录
[[email protected] ~]# mkdir -p /usr/share/haproxy
修改Haproxy主配置文件
[[email protected] ~]# vi /etc/haproxy/haproxy.cfg
#this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 10
clitimeout 10
srvtimeout 10
listen nginx 192.168.100.30:80
balance roundrobin
server web01 192.168.100.10:80 check inter 2000 rise 2 fall 5
server web02 192.168.100.20:80 check inter 2000 rise 2 fall 5
运行脚本文件,启动Haproxy服务
[[email protected] ~]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ 确定 ]
[[email protected] ~]# netstat -anptu | grep 80
tcp 0 0 192.168.100.30:80 0.0.0.0:* LISTEN 40809/haproxy
udp 0 0 0.0.0.0:48056 0.0.0.0:* 40809/haproxy
[[email protected] ~]# netstat -anptu | grep haproxy
tcp 0 0 192.168.100.30:80 0.0.0.0:* LISTEN 40809/haproxy
udp 0 0 0.0.0.0:48056 0.0.0.0:* 40809/haproxy
客户端验证
安装DNS
挂载系统光盘
[[email protected] ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
安装DNS
[[email protected] ~]# yum -y install bind bind-chroot
启动DNS,开机自启
[[email protected] ~]# systemctl start named
[[email protected] ~]# systemctl enable named
备份DNS主配置文件
[[email protected] ~]# cp /etc/named.conf /etc/named.conf.bak
配置DNS主配置文件
[[email protected] ~]# echo “” > /etc/named.conf
[[email protected] ~]# vi /etc/named.conf
options {
listen-on port 53 { any; };
directory “/var/named”;
};
zone “benet.com” IN {
type master;
file “benet.com.zone”;
};
编辑正向解析文件
[[email protected] ~]# vi /var/named/benet.com.zone
$TTL 86400
@ SOA benet.com root.benet.com.(
2019120901
1H
15M
1W
1D
)
@ NS centos04.benet.com.
centos04 A 192.168.100.40
www A 192.168.100.30
[[email protected] ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2019120901
OK
重启DNS服务
[[email protected] ~]# systemctl restart named
编辑网卡配置
[[email protected] ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens32
DNS1=192.168.100.40
重启网卡
[[email protected] ~]# systemctl restart network
客户端IP配置
客户端验证
添加httpd服务
添加光盘
[[email protected] ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
安装httpd服务
[[email protected] ~]# yum -y install httpd
开启httpd服务
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd
创建网站根目录文件
[[email protected] ~]# echo “www.bdqn.com” > /var/www/html/index.html
编辑Haproxy主配置文件
listen nginx 192.168.100.30:80
balance roundrobin
server web01 192.168.100.10:80 check inter 2000 rise 2 fall 5
server web02 192.168.100.20:80 check inter 2000 rise 2 fall 5
server web03 192.168.100.50:80 check inter 2000 rise 2 fall 5 weight 1
重启Haproxy服务
[[email protected] ~]# /etc/init.d/haproxy stop
Stopping haproxy (via systemctl): [ 确定 ]
[[email protected] ~]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ 确定 ]
客户端访问