nginx(grpc)+ keepalived
一、安装nginx
1,下载
1)公网下载地址未编译:http://nginx.org/download/nginx-1.18.0.tar.gz
2,编译
tar zxvf nginx-1.18.0.tar.gz && cd nginx-1.18.0/
#安装依赖
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
#配置
./configure --with-http_ssl_module —with-http_v2_module --prefix=.
#编译
make
#安装
make install
#验证
cd nginx-1.18.0/ && ./sbin/nginx -V
3,配置
可以到 github 这里下载配置文件
### nginx.conf
worker_processes 12; # 定义nginx工作进程数量
worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 100000000000; #将工作进程绑定到CPU组。每个CPU集由允许的CPU的位掩码表示
pid logs/nginx.pid;
worker_rlimit_nofile 102400;
events {
use epoll;
worker_connections 102400;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 75s;
underscores_in_headers on;
open_file_cache max=1000 inactive=60;
client_max_body_size 0;
client_header_timeout 60s;
client_body_timeout 60s;
send_timeout 20s;
server_tokens off;
sendfile on;
proxy_send_timeout 30s;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" host=[$host] upstream_addr=[$upstream_addr] '
'request_time=[$request_time] upstream_time=[$upstream_response_time]';
include vhost/*.conf;
}
### asr.conf
server
{
listen 8051 http2; #绑定端口 后面需要指定http2
client_max_body_size 2050m;
client_body_buffer_size 1024k;
location / { #注意location区域下面都是grpc与传统不同注意观察
grpc_pass grpc://asr;
grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
}
access_log logs/asr_acc.log main;
error_log logs/asr_err.log error;
}
### upstream.conf
upstream asr {
#ip_hash;
server 192.168.1.2:30051 weight=1 ;
server 192.168.1.3:30051 weight=1 ;
server 192.168.1.4:30051 weight=1 ;
}
4,启动
#启动
cd nginx-1.18.0/ && ./sbin/nginx -c conf/nginx.conf
#关闭
cd nginx-1.18.0/ && ./sbin/nginx -s quit
#重新加载
./sbin/nginx -s reload
#脚本
#!/bin/bash
case $1 in
start)
./sbin/nginx -c conf/nginx.conf
;;
stop) # 服务停止需要做的步骤
./sbin/nginx -s quit
;;
restart) # 重启服务需要做的步骤
./sbin/nginx -s reload
;;
*) echo "$0 {start|stop|restart}"underscores_in_headers
exit 4
;;
esac
#####
二、安装keepalived及配置
1,下载
1)公网下载地址:https://www.keepalived.org/download.html
2,编译
tar -zxvf /home/keepalived-2.0.10.tar.gz
cd keepalived-2.0.10/
./configure --prefix=/usr/local/keepalived
编译过程中出现的错误:
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
解决方案:
yum install -y libnl libnl-devel
继续编译,若出现下面错误
configure: error: libnfnetlink headers missing
解决方案:
yum install -y libnfnetlink-devel
继续编译,然后安装
./configure --prefix=/usr/local/keepalived
make && make install
制作快捷启动,在源文件的目录/opt/software/keepalived-2.0.10/keepalived/etc/init.d下有两个个快捷启动文件和生成/usr/local/keepalived目录下一个配置文件需要复制,具体执行如下命令:
cp /home/keepalived-2.0.10/keepalived/etc/init.d/keepalived /etc/init.d/
# 需要提前在/etc下创建keepalived目录
mkdir -p /etc/keepalived
##cp之前先改好配置文件如下附件。
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /home/keepalived-2.0.10/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
systemctl daemon-reload
这样就可以执行service keepalived [start | stop | reload | restart ]命令。
启动报“Starting keepalived: /bin/bash: keepalived: command not found”错误:
解决方案:
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
或者
systemctl start keepalived
3,keeplived-1的配置
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_haproxy {
script "/usr/bin/killall -0 haproxy" #检查haproxy状态
interval 2
weight -4 #当检查失败减4个权重
}
vrrp_instance VI_1 {
state MASTER #当前角色为master
interface bond4 #网卡名称
virtual_router_id 152 #路由id,要在整个网段中唯一切与slave相同
priority 100
nopreempt #设置不抢占模式当自身恢复后不会抢占vip
unicast_src_ip 10.96.37.53 #本地ip
unicast_peer {
10.96.37.54 #slave的IP地址
}
advert_int 1
authentication {
auth_type PASS
auth_pass passAI
}
virtual_ipaddress {
10.96.37.74/24 #虚ip
}
track_script {
chk_haproxy
}
}
keeplived-2的配置
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight -4
}
vrrp_instance VI_1 {
state BACKUP
interface bond4
virtual_router_id 152
priority 99
unicast_src_ip 10.96.37.54
unicast_peer {
10.96.37.53
}
advert_int 1
authentication {
auth_type PASS
auth_pass passAI
}
virtual_ipaddress {
10.96.37.74/24
}
track_script {
chk_haproxy
}
}