nginx(grpc)+ keepalived

本文详细介绍了如何使用Nginx和Keepalived构建高可用的服务器集群,包括Nginx的安装、编译、配置以及与gRPC的结合使用,同时深入探讨了Keepalived的安装过程、编译错误解决方法和配置技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
    }
  
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值