一、下载相关软件包
nginx官网 http://nginx.org/
keepalived官网 http://www.keepalived.org
二、安装keepalived和nginx
1、安装keepalived
tar xf keepalived-1.3.2.tar.gz
cd keepalived-1.3.2
./configure –prefix=/usr/local/keepalived
make && make install
2、配置
安装完成后,进入安装目录的etc目录下,将keepalived相应的配置文件拷贝到系统相应的目录当中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件,如果没有找到则使用默认的配置。/etc/keepalived目录安装时默认是没有安装的,需要手动创建。配置文件目录结构如下所示:
shell> tree -l /usr/local/keepalived/etc -- keepalived | |-- keepalived.conf | `-- samples | |-- keepalived.conf.status_code | |-- keepalived.conf.track_interface | |-- keepalived.conf.vrrp | |-- 。。。 |-- rc.d | `-- init.d | `-- keepalived `-- sysconfig `-- keepalived
分别对应系统目录(忽略samples目录):
/etc/keepalived/keepalived.conf /etc/rc.d/init.d/keepalived /etc/sysconfig/keepalived
将配置文件拷贝到系统对应的目录下:
shell> mkdir /etc/keepalived shell> cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf shell> cp /usr/local/keepalived/sbin/keepalived /etc/rc.d/init.d/keepalived shell> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
启动keepalived
/etc/init.d/keepalived
另外需要注意的一点是,keepalived启动时不会检查配置文件的语法是否正确,所以我们在编写配置文件时要特别小心,别写错了,否则会出现一些意想不到的现象。
# Options for keepalived. See `keepalived --help' output and keepalived(8) and # keepalived.conf(5) man pages for a list of all options. Here are the most # common ones : # # --vrrp -P Only run with VRRP subsystem. # --check -C Only run with Health-checker subsystem. # --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop. # --dont-release-ipvs -I Dont remove IPVS topology on daemon stop. # --dump-conf -d Dump the configuration data. # --log-detail -D Detailed log messages.日志默认输出在/var/log/message文件中 # --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON) # KEEPALIVED_OPTIONS="-D"
运行keepalived –help可以查看启动时的可选参数,这些可选参数都可以配置在/etc/sysconfig/keepalived文件中的KEEPALIVED_OPTIONS选项中,作为服务启动时的参数。
keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程。
shell> ps -ef | grep keepalived root 831 1 0 11:22 ? 00:00:00 keepalived -D root 840 831 0 11:22 ? 00:00:00 keepalived -D root 841 831 0 11:22 ? 00:00:00 keepalived -D
到此keepalived就安装完成了。
3、安装nginx
tar -zxvf nginx-1.10.0.tar.gz cd nginx-1.10.0 执行下面的命令安装nginx的依赖库: yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx make && make install
三、配置keepalived和nginx
配置文件如下:
1、keepalived.conf
[root@proxy01 nginx-1.10.0]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 50 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.208/24 dev eth0 label eth0:1 } }
2、nginx.conf
user root; worker_processes 1; events { worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; ## FastDFS Tracker Proxy upstream fastdfs_tracker { server 192.168.1.206:8000 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.207:8000 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } ## FastDFS Proxy location /dfs { root html; index index.html index.htm; proxy_pass http://fastdfs_tracker/; proxy_set_header Host $http_host; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 300m; } } }
四、启动测试
访问http://192.168.1.208,就可以跳转到后端的服务器页面,当一台nginx宕机后,不会影响网络服务,VIP漂移到新的web服务器上。
- 转载请注明来源:Keepalived+Nginx实现高可用和双主节点负载均衡
- 本文永久链接地址:http://www.hongxiaowei.com/xiaowei/159.html