Nginx&Keepalived


1.下载依赖库
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.使用configure脚本自动生成Makefile文件 --prefix 指定安装目录
./configure --prefix=/usr/local/software/nginx
3.编译安装
make && make install
4.命令
4.1 sbin/nginx     启动nginx
4.2 sbin/nginx -s quit 关闭 nginx
4.3 sbin/nginx -s reload     重启 nginx ,当 nginx 配置文件修改后,可以重启生效
4.4 sbin/nginx -v   查看 nginx 版本
4.5 sbin/nginx -t   当修改配置文件后,可查看配置文件(nginx.conf)是否正确
4.6 sbin/nginx -h   查看帮助文档
5.配置
5.1 在主模块中  
daemon off | on ;   默认 on
是否已守护进程的方式运行 nginx ,守护进程是指脱离终端并且在后头运行的进程,关闭守护进程执行的方式可以让我们方便调试 nginx

master_process off | on ; 默认 on
是否以 Master/Worker 方式进行工作,在实际的环境中 nginx是以一个Master进程管理多个Worker进程的方式运行的,关闭后,nginx就不会 fork 出
Work子进程来处理请求,而是用Master自身进程处理请求 work_process number 默认 1 ,在Master/Worker运行方式下,worker进程的数目,一般情况 下与内核CPU数目相等

worker_process number默认为 1

vim nginx.conf
  http {
      upstream westos { #调度方法,设置它为westos
  ip_hash; #哈希算法
  server 172.25.30.2:80; #real server1
  server 172.25.30.3:80; #real server2
  }
        include       mime.types;
        default_type application/octet-stream;
        server {
                listen 80; #端口为80端口
                server_name www.westos.org;
                # 访问域名

                location /{ #当客户端访问时,调度站直接扔给westos来进行调度
                proxy_pass http://westos;
        }
}

-----------------------------------------------------------------------------------------------
1.配置反向代理
server {
  listen       80;
  server_name nginx-01.xiaoniu.cn;
  #172.16.203.101/hello.html
location / {
  root html;
      proxy_pass http://192.168.0.21:8080;
  }
}

2.动静分离
#动态资源 index.jsp
location ~ .*\.(jsp|do|action)$ {
  proxy_pass http://tomcat-01.xiaoniu.cn:8080;
}

#静态资源
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
  expires 3d;
}


3.负载均衡池
在http这个节下面配置一个叫upstream的,后面的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致。
http {
  upstream tomcats {
      server 172.16.203.20:8080 weight=1;
      server tomcat-02.xiaoniu.cn:8080 weight=1;
      server tomcat-02.xiaoniu.cn:8080 weight=1;
  }
  location ~ .*\.(jsp|do|action) {
      proxy_pass http://tomcats;
  }
}


4.nginx配置图片服务器
http://192.168.31.62/images/8.jpg

location /images {
      root /usr/local/nginx_images;
}

当浏览器请求 http://192.168.31.62/images/8.jpg 图片地址的时候 会 将 /images/8.jpg URI资源路径拿出来,然后与 root 位置进行结合
/usr/local/nginx_images
/images/8.jpg
/usr/local/nginx_images/images/8.jpg

https://www.cnblogs.com/kevingrace/p/6138185.html

Keepalived安装


1.解压
tar -zxvf keepalived-2.0.15.tar.gz -C /usr/local/software/
2.创建目录
mkdir keepalived
3.使用configure脚本自动生成Makefile文件 --prefix 指定安装目录
./configure --prefix=/usr/local/software/keepalived
4.编译安装
make && make install
5.启动前相关设置
mkdir /etc/keepalived
cp /usr/local/software/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/software/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/software/keepalived-2.0.15/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/software/keepalived/sbin/keepalived /usr/sbin/

添加keepalived到开机启动
chkconfig --add keepalived
chkconfig keepalived on

6.配置
MASTER
global_defs {
  router_id pengyy62
}
vrrp_instance VI_1 {
  state MASTER
  interface ens33
  mcast_src_ip 192.168.31.62
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
       192.168.31.61
  }
}


BACKUP节点
global_defs {
  router_id pengyy63
}
vrrp_instance VI_1 {
  state BACKUP
  interface ens33
  mcast_src_ip 192.168.31.63
  virtual_router_id 51
  priority 99
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
       192.168.31.61
  }
}

router_id 是路由标识,在一个局域网里面应该是唯一的 运行keepalived的机器的一个标识,通常可设为hostname;

vrrp_script chk_http_port #检测nginx服务是否在运行。有很多方式,比如进程,用脚本检测等等
script "/etc/keepalived/chk_nginx.sh"    #这里通过脚本监测
interval 2   #脚本执行间隔,每2s检测一次
weight -5                    #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
fall 2                    #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise 1                    #检测1次成功就算成功。但不修改优先级


vrrp_instance VI_1{...} #这是一个VRRP实例,里面定义了keepalived的主备状态、接口、优先级、认证和IP信息; keepalived在同一virtual_router_id中           priority(0-255)最大的会成为master,也就是接管VIP,当priority最大的主机发生故障后次priority将会接管;
state MASTER    #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器。注意这里的state指定instance(Initial)的初始状 态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置 为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会 就回抢占为MASTER
interface定义使用的接口,这里我的服务器用的网卡都是 ens33,根据实际来填写,
mcast_src_ip 服务器ip
virtual_router_id是虚拟路由ID标识,一组的keepalived配置中主备都是设置一致,priority是优先级,数字越大,优先级越大,
advert_int 1                 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
authentication {             #设置验证类型和密码。主从必须一样
      auth_type PASS           #设置vrrp验证类型,主要有PASS和AH两种
      auth_pass 1111           #设置vrrp验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
  }
auth_type是认证方式,auth_pass是认证的密码。
virtual_ipaddress {...}定义虚拟IP地址,可以配置多个IP地址,这里我定义为192.168.31.61,绑定了 ens33 的网络接口,虚拟接口 ens33:1
如果有多个VIP,继续换行填写


7.启动
service keepalived start

8.配置keepalived心跳检查

MASTER
global_defs {
  router_id pengyy62
}

vrrp_script chk_http_port {        
  script "/etc/keepalived/chk_nginx.sh"  
  interval 2                      
  weight -5                      
  fall 2                  
  rise 1                  
}

vrrp_instance VI_1 {
  state MASTER
  interface ens33
  mcast_src_ip 192.168.31.62
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
       192.168.31.61
  }
}


BACKUP节点
global_defs {
  router_id pengyy63
}

vrrp_script chk_http_port {        
  script "/etc/keepalived/chk_nginx.sh"  
  interval 2                      
  weight -5                      
  fall 2                  
  rise 1                  
}

vrrp_instance VI_1 {
  state BACKUP
  interface ens33
  mcast_src_ip 192.168.31.63
  virtual_router_id 51
  priority 99
  advert_int 1
  authentication {
      auth_type PASS
      auth_pass 1111
  }
  virtual_ipaddress {
       192.168.31.61
  }
}

--检测脚本
vim /etc/keepalived/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
  /usr/local/software/nginx/sbin/nginx --nginx命令启动路径
   sleep 2
   counter=$(ps -C nginx --no-heading|wc -l)
   if [ "${counter}" = "0" ]; then
      /etc/init.d/keepalived stop
   fi
fi

chmod -R 777 chk_nginx.sh

分别在 pengyy62 pengyy63 启动nginx 和 keepalived
sbin/nginx
keepalived

https://www.cnblogs.com/kevingrace/p/6138185.html

猜你喜欢

转载自www.cnblogs.com/pengyy/p/11317860.html