1、api网关介绍
api网关现在使用的是一个开源项目(apisix),然后在其上进行的二次开发(修改配置和增加插件),api网关本身相当于是代理服务器,或者说其本质就是一个代理服务器。
2、提前安装etcd
需要安装etcd3.4以上的版本;
Apache APISIX 从 v2.0 开始不再支持 v2
版本的 etcd,并且 etcd 最低支持版本为 v3.4.0,因此请使用 etcd 3.4.0+。
# 1、以下是etcd安装步骤
下载etcd安装包解压安装
wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
# 2. 解压
tar -xvf etcd-v3.4.13-linux-amd64.tar.gz && cd etcd-v3.4.13-linux-amd64 && sudo cp -a etcd etcdctl /usr/bin/
#3.创建etcd目录以及用户
chattr -i /etc/passwd
chattr -i /etc/group
mkdir -p /etc/etcd /var/lib/etcd
useradd -s /sbin/nologin etcd
chown etcd:etcd /var/lib/etcd
# 4.etcd配置文件
vim /etc/etcd/etcd.conf
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://本机ip:2380"
ETCD_LISTEN_CLIENT_URLS="http://本机ip:2379,http://127.0.0.1:2379"
ETCD_NAME=etcd01
ALLOW_NONE_AUTHENTICATION=yes
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://本机ip:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://本机ip:2379,http://127.0.0.1:2379"
#ETCD_INITIAL_CLUSTER="etcd01=http://:2380,etcd02=http://:2380,etcd03=http://:2380"
#ETCD_INITIAL_CLUSTER_TOKEN="0NQ9mbJx"
#ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_ENABLE_V2="true"
# 5.systemd 配置文件
vim /etc/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd
Type=notify
[Install]
WantedBy=multi-user.target
# 6.服务开机自启动
systemd daemon-reload
systemctl enable etcd && systemd start etcd
[root@shjd-test-apisix ~]# etcd --version
etcd Version: 3.4.13
Git SHA: ae9734ed2
Go Version: go1.12.17
Go OS/Arch: linux/amd64
3、安装openresty
# 添加 OpenResty 源
sudo
yum
install
yum-utils
sudo
yum-config-manager --add-repo https:
//openresty
.org
/package/centos/openresty
.repo
# 安装 OpenResty 和 编译工具
sudo
yum
install
-y openresty curl git gcc openresty-openssl111-devel unzip pcre pcre-devel openldap-devel.x86_64 openldap
4、安装apisix
安装apisix之前建议yum -y update
sudo yum install -y https://repos.apiseven.com/packages/centos/apache-apisix-repo-1.0-1.noarch.rpm
yum-config-manager --add-repo https://repos.apiseven.com/packages/centos/apache-apisix.repo
yum install apisix-3.0.0
[root@shjd-test-apisix ~]# apisix version
/usr/local/openresty/luajit/bin/luajit /usr/local/apisix/apisix/cli/apisix.lua version
3.0.0
5、APISIX dashboard(控制台)安装
yum install -y https://github.com/apache/apisix-dashboard/releases/download/v3.0.0/apisix-dashboard-3.0.0-0.el7.x86_64.rpm
[root@shjd-test-apisix ~]# systemctl status apisix-dashboard
● apisix-dashboard.service - apisix-dashboard
Loaded: loaded (/usr/lib/systemd/system/apisix-dashboard.service; static; vendor preset: disabled)
Active: active (running) since Thu 2023-10-19 13:46:30 CST; 1h 2min ago
Main PID: 8857 (manager-api)
CGroup: /system.slice/apisix-dashboard.service
└─8857 /usr/local/apisix/dashboard/manager-api -c /usr/local/apisix/dashboard/conf/conf.yaml
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: Version : 3.0.0
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: GitHash : f494919
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: Config File: /usr/local/apisix/dashboard/conf/conf.yaml
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: Listen : 0.0.0.0:9000
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: Loglevel: warn
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: ErrorLogFile: /usr/local/apisix/dashboard/logs/error.log
Oct 19 13:46:30 shjd-test-apisix manager-api[8857]: AccessLogFile: /usr/local/apisix/dashboard/logs/access.log
Oct 19 13:46:37 shjd-test-apisix systemd[1]: Dependency Conflicts=apisix-dashboard.service dropped from unit apisix-dashboard.service
Oct 19 13:46:53 shjd-test-apisix systemd[1]: Dependency Conflicts=apisix-dashboard.service dropped from unit apisix-dashboard.service
Oct 19 13:47:16 shjd-test-apisix systemd[1]: Dependency Conflicts=apisix-dashboard.service dropped from unit apisix-dashboard.service
查看apisix-dashboard监听端口
[root@shjd-test-apisix ~]# netstat -anp | grep 9000
tcp6 0 0 :::9000 :::* LISTEN 8857/manager-api
修改配置文件,添加远端访问网段信息
配置文件路径:/usr/local/apisix/dashboard/conf/conf.yaml
打开以后,如果远程登录要进行配置:
# yamllint disable rule:comments-indentation
conf:
listen:
# host: 127.0.0.1 # the address on which the `Manager API` should listen.
# The default value is 0.0.0.0, if want to specify, please enable it.
# This value accepts IPv4, IPv6, and hostname.
port: 9000 # The port on which the `Manager API` should listen.
# ssl:
# host: 127.0.0.1 # the address on which the `Manager API` should listen for HTTPS.
# The default value is 0.0.0.0, if want to specify, please enable it.
# port: 9001 # The port on which the `Manager API` should listen for HTTPS.
# cert: "/tmp/cert/example.crt" # Path of your SSL cert.
# key: "/tmp/cert/example.key" # Path of your SSL key.
allow_list: # If we don't set any IP list, then any IP access is allowed by default.
- 127.0.0.1 # The rules are checked in sequence until the first match is found.
- ::1 # In this example, access is allowed only for IPv4 network 127.0.0.1, and for IPv6 network ::1.
- 172.0.0.0/8 # It also support CIDR like 192.168.1.0/24 and 2001:0db8::/32
- 10.0.0.0/8
etcd:
endpoints: # supports defining multiple etcd host addresses for an etcd cluster
- 127.0.0.1:2379
# yamllint disable rule:comments-indentation
浏览器登录apisix-dashboard,默认用户名user 密码user