kution 发表于 2018-12-31 07:02:47

nginx keepalived 搭建负载均衡

  1下载安装包

  yum install -y pcre-devel
  wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
  tar zxvf keepalived-1.2.2.tar.gz
  cd keepalived-1.2.2
  ./configure --prefix=/usr/local/keepalived
  make&& make install
  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  mkdir /etc/keepalived
  cd /etc/keepalived/
  vim keepalived.conf
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  yuhongchun027@163.com
  }
  notification_email_from keepalived@chtopnet.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  mcast_src_ip 192.168.93.130
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass chtopnet
  }
  virtual_ipaddress {
  192.168.93.132
  }
  }
  service keepalived start
  tail /var/log/messages看keepalived是否启动
  ip a看vip是否已存在
  ip a 来检查
  1: lo:mtu 16436 qdisc noqueue
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host
  valid_lft forever preferred_lft forever
  2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:0c:29:ab:e6:22 brd ff:ff:ff:ff:ff:ff
  inet 192.168.93.130/24 brd 192.168.93.255 scope global eth0
  inet 192.168.93.132/32 scope global eth0
  inet6 fe80::20c:29ff:feab:e622/64 scope link
  valid_lft forever preferred_lft forever
  3: sit0:mtu 1480 qdisc noop
  link/sit 0.0.0.0 brd 0.0.0.0
  很明显已经存在了,
  这样主服务器 就配置好了,辅机的配置大致一样,除了配置文件 有少部分的变化,下面贴出辅机的配置文件:
  wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
  

  #tar zxvf keepalived-1.2.2.tar.gz
  #cd keepalived-1.2.2
  #./configure --prefix=/usr/local/keepalived
  #make
  #make install
  #cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  #mkdir /etc/keepalived
  #cd /etc/keepalived/
  #vim keepalived.conf
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  yuhongchun027@163.com
  }
  notification_email_from keepalived@chtopnet.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  mcast_src_ip 192.168.93.131
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass chtopnet
  }
  virtual_ipaddress {
  192.168.93.132
  }
  }
  #service keepalived start
  很显然vrrp已经启动,我们还可以通过命令:#ip a 来检查
  1: lo:mtu 16436 qdisc noqueue
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host
  valid_lft forever preferred_lft forever
  2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:0c:29:4a:2a:eb brd ff:ff:ff:ff:ff:ff
  inet 192.168.93.131/24 brd 192.168.93.255 scope global eth0
  inet6 fe80::20c:29ff:fe4a:2aeb/64 scope link
  valid_lft forever preferred_lft forever
  3: sit0:mtu 1480 qdisc noop
  link/sit 0.0.0.0 brd 0.0.0.0
  

  2 nginx的安装
  tar -xzf nginx-1.4.1.tar.gz
  cd nginx-1.4.1
  sed -i -e 's/1.2.2//g' -e 's/nginx\//TDTWS/g' -e 's/"NGINX"/"TDTWS"/g' src/core/nginx.h
  ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
  make && make install
  /usr/local/nginx/sbin/nginx
  测试
  测试其效果方法很简单,分别在主辅机上建立不同的主页,
  然后用客户机上http://192.168.93.132 ,显示nginx主的页面效果130
  

  1)主机nginx down掉后辅机会马上接替提供服务 ,间隔时间几乎无法感觉出来,
  我们关闭nginx主
  访问http://192.168.93.132 显示 131
  可查看naginx从=》主 后131的变换 #ip a 就可以看到inet 192.168.93.132/32 scope global eth0
  1: lo:mtu 16436 qdisc noqueue
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host
  valid_lft forever preferred_lft forever
  2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:0c:29:4a:2a:eb brd ff:ff:ff:ff:ff:ff
  inet 192.168.93.131/24 brd 192.168.93.255 scope global eth0
  inet 192.168.93.132/32 scope global eth0
  inet6 fe80::20c:29ff:fe4a:2aeb/64 scope link
  valid_lft forever preferred_lft forever
  3: sit0:mtu 1480 qdisc noop
  link/sit 0.0.0.0 brd 0.0.0.0
  

  2)在开启nginx主服务器(keepalived 开机自动启动)
  访问http://192.168.93.132 显示 130
  可查看naginx从后131的变换 #ip a
  1: lo:mtu 16436 qdisc noqueue
  link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  inet 127.0.0.1/8 scope host lo
  inet6 ::1/128 scope host
  valid_lft forever preferred_lft forever
  2: eth0:mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:0c:29:4a:2a:eb brd ff:ff:ff:ff:ff:ff
  inet 192.168.93.131/24 brd 192.168.93.255 scope global eth0
  inet6 fe80::20c:29ff:fe4a:2aeb/64 scope link
  valid_lft forever preferred_lft forever
  3: sit0:mtu 1480 qdisc noop
  link/sit 0.0.0.0 brd 0.0.0.0
  

  

  小结:
  vip两台服务器是一样的
  搭建时遇到的问题
  1gcc没安装
  2prce
  3./configure: error: the HTTP rewrite module requires the PCRE library.
  

  安装pcre-devel解决问题
  yum -y install pcre-deve
  

  4./configure时老是报!
  checking openssl/ssl.h usability... no
  checking openssl/ssl.h presence... no
  checking for openssl/ssl.h... no
  configure: error:
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.
  

  解决:
  yum install -y openssl openssl-devel
  

  

  

  5keepalived出现configure: error: Popt libraries is required的解决方法
  

  解决方法:
  yum install popt-devel
  安装好popt的开发包。
  重新./configure 即可。
  

  

  

  

  




页: [1]
查看完整版本: nginx keepalived 搭建负载均衡