目录
部署Haproxy+
一,软件开发定制配置主服务器(haproxy)
软件开发定制根据上篇博客 操作,软件开发定制再添加一台haproxy服务器做(备用)软件开发定制边缘服务器,部署keepalived。
1,软件开发定制关闭防火墙
2,软件开发定制安装依赖环境
[root@haproxy ~]#yum -y install pcre-devel zlib-devel gcc gcc-c++ make
3,编译安装haproxy
- tar zxvf haproxy-1.6.9.tar.gz #软件开发定制解压安装包
- cd haproxy-1.6.9/ #软件开发定制进入解压目录
-
- make TARGET=linux2628 ARCH=x86_64 #进行编译
- (TARGET=linux2628 软件开发定制表示内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628)
- (ARCH=x86_64 系统位数,现在基本都是64位)
-
- make install # 安
4,haproxy服务配置文件修改
[root@kya haproxy-1.5.19]#mkdir /etc/haproxy
[root@kya haproxy-1.5.19]#cp /opt/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
[root@kya haproxy-1.5.19]#cd /etc/haproxy/
[root@kya haproxy]#ls
haproxy.cfg
删除将原来的listen配置项,添加新配置:
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server nginx1 192.168.135.90:80 check inter 2000 fall 3
server nginx2 192.168.135.91:80 check inter 2000 fall 3
5,添加haproxy系统服务
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxyln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
service haproxy start 或 /etc/init.d/haproxy start //如果ningx服务器未部署会提示找不到web服务器,可以等web部署完后再开启haproxy
- systemctl stop firewalld
- setenforce 0
- yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
- tar zxvf haproxy-1.6.9.tar.gz
- cd haproxy-1.6.9/
- make TARGET=linux2628 ARCH=x86_64 #进行编译
- make install
-
-
- mkdir /etc/haproxy #创建haproxy目录
- cp examples/haproxy.cfg /etc/haproxy/ #将软件包中的配置文件模板复制到haproxy目录中
- cd
- vim /etc/haproxy/haproxy.cfg #修改haproxy.cfg配置参数
-
- 下面两项前面加上注释
- #chroot /usr/share/haproxy #固有目录,可注释掉
- #redispatch #当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉了,但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个后端server上,以保证服务的正常。需要注释掉
-
- 删除将原来的listen配置项,添加新配置:
- listen webcluster 0.0.0.0:80
- option httpchk GET /index.html
- balance roundrobin
- server nginx1 192.168.135.90:80 check inter 2000 fall 3
- server nginx2 192.168.135.91:80 check inter 2000 fall 3
-
-
- 优化服务启动:
- cp haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
- vi /etc/init.d/haproxy
-
- 修改:
- # chkconfig: 35 85 15
-
- chmod 755 /etc/init.d/haproxy
- chkconfig --add /etc/init.d/haproxy
- ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
- systemctl start haproxy
-
2.keepalived配置
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
yum -y install keepalived
cp keepalived/etc/init.d/keepalived /etc/init.d ###加入系统管理服务
systemctl enable keepalived.service ###设置开机自启动
vi /etc/keepalived/keepalived.conf ###编辑配置文件
删除原有配置,重新添加:! Configuration File for keepalived
global_defs { #全局参数
router_id LVS_01 #指定名称,各个服务器名称要不一样
}
vrrp_instance VI_1 { #指定vrrp热备参数
state MASTER #服务器角色是master,备份服务器设置为BACKUP
interface ens33 #修改物理网卡名称,默认是centos6的eth0
virtual_router_id 10 #组号相同
priority 150 #优先级,主服务器设置要大于备服务器
advert_int 1
authentication {
auth_type PASS #验证类型和密码,不建议修改
auth_pass 123
}
virtual_ipaddress {
192.168.135.100 #漂移地址(VIP)地址,可以有多个
}
}[root@haproxy2 ~]# systemctl start keepalived
[root@haproxy2 ~]# ip addr
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
删除原有配置,重新添加
! Configuration File for keepalived
global_defs { #全局参数
router_id LVS_01 #指定名称,各个服务器名称要不一样
}
vrrp_instance VI_1 { #指定vrrp热备参数
state MASTER #服务器角色是master,备份服务器设置为BACKUP
interface ens33 #修改物理网卡名称,默认是centos6的eth0
virtual_router_id 10 #组号相同
priority 150 #优先级,主服务器设置要大于备服务器
advert_int 1
authentication {
auth_type PASS #验证类型和密码,不建议修改
auth_pass 123
}
virtual_ipaddress {
192.168.135.100 #漂移地址(VIP)地址,可以有多个
}
}
二,备用服务器(haproxy)
1,haproxy配置(和主服务器相同)
systemctl stop firewalld
setenforce 0
yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
tar zxvf haproxy-1.6.9.tar.gz
cd haproxy-1.6.9/
make TARGET=linux2628 ARCH=x86_64 #进行编译
make install
mkdir /etc/haproxy #创建haproxy目录
cp examples/haproxy.cfg /etc/haproxy/ #将软件包中的配置文件模板复制到haproxy目录中
cd
vim /etc/haproxy/haproxy.cfg #修改haproxy.cfg配置参数下面两项前面加上注释
#chroot /usr/share/haproxy #固有目录,可注释掉
#redispatch #当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉了,但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个后端server上,以保证服务的正常。需要注释掉删除将原来的listen配置项,添加新配置:
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server nginx1 192.168.135.90:80 check inter 2000 fall 3
server nginx2 192.168.135.91:80 check inter 2000 fall 3
优化服务启动:
cp haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
vi /etc/init.d/haproxy修改:
# chkconfig: 35 85 15chmod 755 /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
systemctl start haproxy
前面已经做过直接看keepalived配置
2,keepalived配置
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
yum -y install keepalived
cp keepalived/etc/init.d/keepalived /etc/init.d ###加入系统管理服务
systemctl enable keepalived.service ###设置开机自启动
vi /etc/keepalived/keepalived.conf ###编辑配置文件
删除原有配置,重新添加:! Configuration File for keepalived
global_defs { #全局参数
router_id LVS_02 #指定名称,各个服务器名称要不一样
}
vrrp_instance VI_1 { #指定vrrp热备参数
state BACKUP #服务器角色是master,备份服务器设置为BACKUP
interface ens33 #修改物理网卡名称,默认是centos6的eth0
virtual_router_id 10 #组号相同
priority 140 #优先级,主服务器设置要大于备服务器
advert_int 1
authentication {
auth_type PASS #验证类型和密码,不建议修改
auth_pass 123
}
virtual_ipaddress {
192.168.135.100 #漂移地址(VIP)地址,可以有多个
}
}[root@haproxy2 ~]# systemctl start keepalived
[root@haproxy2 ~]# ip addr
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
删除原有配置,重新添加
! Configuration File for keepalived
global_defs { #全局参数
router_id LVS_02 #指定名称,各个服务器名称要不一样
}
vrrp_instance VI_2 { #指定vrrp热备参数
state BACKUP #服务器角色是master,备份服务器设置为BACKUP
interface ens33 #修改物理网卡名称,默认是centos6的eth0
virtual_router_id 10 #组号相同
priority 140 #优先级,主服务器设置要大于备服务器
advert_int 1
authentication {
auth_type PASS #验证类型和密码,不建议修改
auth_pass 123
}
virtual_ipaddress {
192.168.135.100 #漂移地址(VIP)地址,可以有多个
}
}
三,nginx1服务器配置
- [root@nginx1 ~]# systemctl stop firewalld
- [root@nginx1 ~]# setenforce 0
-
- [root@nginx1 nginx-1.12.2]# cd
- [root@nginx1 ~]# echo "<h1>192.168.135.90</h1>" > /usr/local/nginx/html/index.html #创建首页内容
- [root@nginx1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin #优化启动项
- [root@nginx1 ~]# nginx
- [root@nginx1 ~]# netstat -anpt | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58600/nginx: master
- systemctl start nginx
四,nginx2服务器配置
- [root@nginx1 ~]# systemctl stop firewalld
- [root@nginx1 ~]# setenforce 0
-
- [root@nginx1 nginx-1.12.2]# cd
- [root@nginx1 ~]# echo "<h1>192.168.135.91</h1>" > /usr/local/nginx/html/index.html #创建首页内容
- [root@nginx1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin #优化启动项
- [root@nginx1 ~]# nginx
- [root@nginx1 ~]# netstat -anpt | grep nginx
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58600/nginx: master
- systemctl start nginx
五,访问测试(192.168.135.100)
关闭主服务器后,漂移ip由备份服务器承担 ,继续工作
再次开启主服务器