从Apacheapisix日志中获取客户端ip
一、apisix介绍
Apache APISIX 是一个动态、实时、高性能的 API 网关。APISIX API Gateway提供丰富的流量管理功能,如负载均衡、动态上行、金丝雀发布、熔断、认证、可观察性等。您可以使用APISIX API 网关来处理传统的南北向流量,以及服务之间的东西向流量。它也可以用作k8s 入口控制器。
Apache APISIX 的技术架构:
二、准备环境
1、开启一台操作系统为centos的ec2用于安装apisix相关的服务,开启另外一台ec2搭建wordpress服务。
2、安装 OpenResty 和 Apache APISIX 的RPM仓库 sudo su #切到root用户 yum install -y https://repos.apiseven.com/packages/centos/apache-apisix-repo-1.0-1.noarch.rpm
3、安装etcd #指定etcd版本 ETCD_VERSION="3.4.18" #安装wget yum install wget #下载 wget https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz #解压缩 tar -xvf etcd-v${ETCD_VERSION}-linux-amd64.tar.gz && cd etcd-v${ETCD_VERSION}-linux-amd64 && sudo cp -a etcd etcdctl /usr/bin/ #启动etcd服务 nohup etcd >/tmp/etcd.log 2>&1 &
4、安装apisix #安装指定版本2.13.1-0.el7 yum install apisix-2.13.1-0.el7
5、apisix 相关命令 # 运行以下命令初始化 NGINX 配置文件和 etcd. initialize NGINX config file and etcd $ apisix init # generate `nginx.conf` from `config.yaml` and test it $ apisix test # 启动 Apache APISIX. start Apache APISIX server $ apisix start # 其他命令 # 优雅停机 stop Apache APISIX server gracefully $ apisix quit # 强制停机 stop Apache APISIX server immediately $ apisix stop # 其他操作 more actions find by `help` $ apisix help
6、安装apisix-dashboard yum install -y https://github.com/apache/apisix-dashboard/releases/download/v2.13.1/apisix-dashboard-2.13.1-0.el7.x86_64.rpm #apisix-dashboard启动、查看状态、重启命令 systemctl start apisix-dashboard systemctl status apisix-dashboard systemctl restart apisix-dashboard
三、验证
1、登录apisix控制台进行配置,路由以及上游
2、配置nlb
1)target配置
2)创建nlb
3)访问nlb
4)开启nlb的target上面的保留ip
5)开启apisix上的日志递归配置并重启 vim /usr/local/apisix/conf/config-default.yaml #重启apisix systemctl restart apisix
6)查看日志( 开启nlb的target上面的保留ip过几分钟才能看到client ip)
7)本地的公网出口ip
注:real_ip_recursive说明
nginx 从 real_ip_header 指令指定的头字段中获取 IP,可能会有多个 IP 值; 当 real_ip_recursive 指令值为 off,nginx 从获取到 IP 值中从右往左(也即从后往前)的顺序,以最后一个 IP 值作为客户端的真实 IP ,此时不会排除授信 IP; 当 real_ip_recursive 指令值为 on,nginx 从获取到 IP 值中从右往左(也即从后往前)的顺序,排除 set_real_ip_from 指令指定的授信 IP,以最后一个非授信 IP 值作为客户端的真实 IP 。