设为首页 收藏本站
查看: 1441|回复: 0

[经验分享] LVS+KEEPALIVED+NGINX负载均衡

[复制链接]

尚未签到

发表于 2018-11-10 13:46:55 | 显示全部楼层 |阅读模式
  本文的搭建环境是在centos5.9和Centos6.4下面进行搭建配置的。
  IP分配:
  centos6.4:192.168.1.199
  centos5.9:192.168.1.198
  两台服务器都是新安装的系统,纯净的,没安装任何服务。
  1、安装nginx
  wget   http://www.nginx.com.cn/download/nginx-1.3.9.tar.gz
  wget  ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
  wget  http://zlib.net/zlib-1.2.8.tar.gz
  wget http://www.openssl.org/source/openssl-1.0.0j.tar.gz
  wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz   图片缓存模块
  解压,配置。编译,安装
  ./configure --user=www  --group=www   --prefix=/usr/local/nginx  --with-openssl=../openssl-1.0.0j --with-zlib=../zlib-1.2.8 --with-pcre=../pcre-8.30  --with-http_ssl_module    --add-module=../ngx_cache_purge-2.1 --with-http_sub_module --with-http_stub_status_module
  make && make  install
  --with-pcre --with-zlib=../zlib-1.2.8  --with-openssl均是指向他的源码包,不是指向安装后的地址
  安装完毕。接下来就是nginx服务器的配置。
  nginx.conf内容
  user www www;
  worker_processes 10;
  error_log /usr/local/nginx/logs/error.log crit;
  pid /usr/local/nginx/logs/nginx.pid;
  worker_rlimit_nofile 65535;
  events
  {
  use epoll;
  worker_connections 65535;
  }
  http
  {
  include mime.types;
  default_type application/octet-stream;
  charset utf-8;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  limit_zone crawler $binary_remote_addr 10m;
  client_max_body_size 300m;
  client_body_buffer_size 128k;
  proxy_connect_timeout 10;
  proxy_read_timeout 60;
  proxy_send_timeout 10;
  proxy_buffer_size 16k;
  proxy_buffers 4 64k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;
  proxy_temp_path /var/proxy_temp_dir;
  proxy_cache_path /var/proxy_cache_dir levels=1:2 keys_zone=cache_one:256m inactive=1d
  max_size=30g;
  include upstream/*.conf;
  include vhost/*.conf;
  include 301/*.conf;
  }
  upstream文件内容(反向代理)
  upstream 10_10_1_1_pool {
  server 10.10.1.1:80 weight=1;
  }
  upstream 192_168_10_1_pool {
  server 192.168.1.1:80 weight=1;
  }
  vhost文件内容
  server
  {
  listen 80 ;
  server_name .ouyanglinux.com;
  location /
  {
  #proxy_next_upstream http_502 http_504 error timeout invalid_header;
  proxy_cache cache_one;
  proxy_cache_valid 200 304 12h;
  proxy_cache_key $host$uri$is_args$args;
  proxy_pass http://178_79_148_41_pool;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $remote_addr;
  expires 1d;
  }
  location ~/purge(/.*)
  {
  allow 127.0.0.1;
  #allow X.X.X.X/X;
  allow all;
  deny all;
  proxy_cache_purge cache_one $host$1$is_args$args;
  }
  location ~.*\.(php|jsp|cgi)?$
  {
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded_For $remote_addr;
  proxy_pass http://192_168_10_1_pool;
  }
  location ~.*\.(html|htm|css)?$
  {
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded_For $remote_addr;
  proxy_pass http://192_168_10_1_pool;
  }
  access_log /usr/local/nginx/logs/ouyanglinux.com_access.log;
  }
  服务器搭建并配置完毕。
  接下开始部署lvs了
  uname -a
  ln  -s  /usr/src/kernels/2.6.32-358.11.1.el6.x86_64/  /usr/src/linux     ### 创建软链接,将当前的kernels连接到/usr/src/linux 否则无法支持IPVS
  下载软件
  wget  http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
  wget  http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
  安装ipvsadm前需要安装 kernel-devel否则会报错
  yum -y install kernel-devel
  安装完后,进入到ipvsadm的源码目录直接make && make  install
  接下来安装keepalived
  ./configure --prefix=/usr/local/keepalived
  编译的时候出现了
  configure: error:
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.            !!!
  原因是缺少部分依赖包
  yum  -y  install  e2fsprogs-devel keyutils-libs-devel  libsepol-devel libselinux-devel   krb5-devel zlib-devel openssl-devel   popt-devel
  ./configure --prefix=/usr/local/keepalived  && make  && make  install
  把keepalived作为系统服务
  cp  /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  cp  /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  mkdir  /etc/keepalived
  cp  /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  cp  /usr/local/keepalived/sbin/keepalived /usr/sbin/
  做完这些,我们开始修改keepalived的配置文件。首先在lvs_master上修改
  LVS_MASTER的配置:
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  xx@163.com
  }
  notification_email_from xx@163.com
  smtp_server smtp.163.com
  smtp_connect_timeout 30
  //LVS
  负载均衡标识,在一个网络内,它是唯一标识
  router_id   LVS_DEVEL     ////LVS负载均衡标识,在一个网络内,它是唯一标识
  //LVS
  负载均衡标识,在一个网络内,它是唯一标识
  }
  vrrp_script chk_http {
  script "/usr/local/keepalived/nginx_pid.sh"
  interval 9
  weight 1
  }
  vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  track_script {
  chk_http
  }
  virtual_ipaddress {
  192.168.1.188
  }
  }
  vrrp_instance VI_2{
  state BACKUP
  interface eth0
  virtual_router_id 49
  proiority  99
  advert_in 1
  authentication{
  auth_type PASS
  auth_pass 1111
  }
  #      virtual_ipaddress{
  #       192.168.1.189
  #       }
  }
  LVS_BACKUP的 配置文件为
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  xx@163.com
  }
  notification_email_from xx@163.com
  smtp_server smtp.163.com
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_script chk_http {
  script "/usr/local/keepalived/nginx_pid.sh"
  interval 9
  weight 1
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 99
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  track_script {
  chk_http
  }
  virtual_ipaddress {
  192.168.1.188
  }
  }
  vrrp_instance VI_2{
  state MASTER
  interface eth0
  virtual_router_id 49
  proiority  100
  advert_in 1
  authentication{
  auth_type PASS
  auth_pass 1111
  }
  #        virtual_ipaddress{
  #       192.168.1.189
  #        }
  }
  配置好了,可以启动keepalived了。
  /usr/local/keepalived/sbin/keepalived -D -f  /etc/keepalived/keepalived.conf
  然后可以用命令查看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
  2: eth0:  mtu 1500 qdisc pfifo_fast qlen 1000
  link/ether 00:15:5d:01:64:01 brd ff:ff:ff:ff:ff:ff
  inet 192.168.1.198/24 brd 192.168.1.255 scope global eth0
  inet 192.168.1.188/32 scope global eth0
  在从服务器上也可以用此命令查看
  1: lo:  mtu 16436 qdisc noqueue state UNKNOWN
  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 state UP qlen 1000
  link/ether 00:15:5d:01:64:00 brd ff:ff:ff:ff:ff:ff
  inet 192.168.1.199/24 brd 192.168.1.255 scope global eth0
  inet 192.168.1.188/32 scope global eth0
  inet6 fe80::215:5dff:fe01:6400/64 scope link
  valid_lft forever preferred_lft forever
  也可以用命令查看日志文件tail  -100  /var/log/message
  可以看到现在主从主要工作还是在V1上
  然后便nginx_pid.sh脚本
  #!/bin/bash
  A=`ps -C nginx --no-header |wc -l`
  if [ $A -eq 0 ];then
  /usr/local/nginx/sbin/nginx
  sleep 5
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  killall keepalived
  fi
  fi
  ~
  编辑权限
  chownwww.www nginx_pid.sh
  chmod  744  nginx_pid.sh
  下面就是测试了.我们输入http://192.168.1.188http://192.168.1.198 结果都一样。http://192.168.1.199显示是从nginx的信息。
  然后关闭主nginx服务器
  发现lvs会自动切换到从库上.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633317-1-1.html 上篇帖子: Nginx之二:负载均衡及高可用 下篇帖子: Nginx 使用中文URL,中文目录路径
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表