APISIX 是一個雲原生、高性能、可擴展的微服務 API 網關。
它是基於 OpenResty 和 etcd 來實現,和傳統 API 網關相比,APISIX 具備動態路由和插件熱加載,特別適合微服務體系下的 API 管理。
服務器三台
CentOS:7.6 內核:3.1
IP:172.18.235.156/24 , 172.18.235.154/24, 172.18.235.155/24
注意:
1、三台服務器相同的操作,在配置etcd時需要更改IP 。
2、關閉服務器防火牆,selinux
1、配置依賴
1.1、安裝 epel, luarocks 需要它
]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
]# rpm -ivh epel-release-latest-7.noarch.rpm
1.2 安裝 etcd
]# mkdir -pv /opt/src
]# cd /opt/src
]# wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
]# tar -xf etcd-v3.4.13-linux-amd64.tar.gz -C /opt
]# mv etcd-v3.4.13-linux-amd64 etcd-v3.4.13
]# ln -sf /opt/etcd-v3.4.13 /opt/etcd
]# cd /opt/etcd
]#cat etcd.sh ##編寫etcd 啟動腳本(172.18.235.156)
#!/bin/sh
./etcd --name="node1" --data-dir="/opt/etcd/default.etcd" --listen-peer-urls="http://172.18.235.156:2380" --listen-client-urls="http://172.18.235.156:2379,http://localhost:2379" --initial-advertise-peer-urls="http://172.18.235.156:2380" --advertise-client-urls="http://172.18.235.156:2379" --initial-cluster="node1=http://172.18.235.156:2380,node2=http://172.18.235.154:2380,node3=http://172.18.235.155:2380" --initial-cluster-token="etcd-cluster" --initial-cluster-state="new" >> /opt/etcd/etcd.log 2>&1 &
]# cat etcd.sh ##編寫etcd 啟動腳本(172.18.235.154)
#!/bin/sh
./etcd --name="node1" --data-dir="/opt/etcd/default.etcd" --listen-peer-urls="http://172.18.235.156:2380" --listen-client-urls="http://172.18.235.156:2379,http://localhost:2379" --initial-advertise-peer-urls="http://172.18.235.156:2380" --advertise-client-urls="http://172.18.235.156:2379" --initial-cluster="node1=http://172.18.235.156:2380,node2=http://172.18.235.154:2380,node3=http://172.18.235.155:2380" --initial-cluster-token="etcd-cluster" --initial-cluster-state="new" >> /opt/etcd/etcd.log 2>&1 &
]# cat etcd.sh ##編寫etcd 啟動腳本(172.18.235.155)
#!/bin/sh
./etcd --name="node3" --data-dir="/opt/etcd/default.etcd" --listen-peer-urls="http://172.18.235.155:2380" --listen-client-urls="http://172.18.235.155:2379,http://localhost:2379" --initial-advertise-peer-urls="http://172.18.235.155:2380" --advertise-client-urls="http://172.18.235.155:2379" --initial-cluster="node1=http://172.18.235.156:2380,node2=http://172.18.235.154:2380,node3=http://172.18.235.155:2380" --initial-cluster-token="etcd-cluster" --initial-cluster-state="new" >> /usr/local/etcd-v3.4.13-linux-amd64/etcd.log 2>&1 &
啟動:
]# bash etcd.sh
驗證:
]# ./etcdctl member list
1.3、部署OpenResty
]# yum install yum-utils
]# yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
]# yum install -y openresty curl git gcc luarocks lua-devel gcc-c++ ##安裝 OpenResty 和 編譯工具
1.4、安裝go、yarn、node(軟件先下載,並上傳至服務器)
]# cd /opt/src
]# tar -xf yarn-v1.22.4.tar.gz -C /opt/
]# ln -sf /opt/yarn-v1.22.4/ /opt/yarn
]# tar -xf node-v12.16.1-linux-x64.tar.xz -C /opt/yarn
]# mv /opt/node-v12.16.1-linux-x64/ /opt/node-v12.16.1
]# ln -sf /opt/node-v12.16.1 /opt/node
]# tar -xf go1.13.linux-amd64.tar.gz -C /opt/
]# mv go go-v1.13
]# ln -sf /opt/go-v1.13 /opt/go
]# vim /etc/profile ##文件最后添加
。。。。。。。。省略。。。。。。
PATH=/opt/yarn/bin/:$PATH
PATH=/opt/node/bin/:$PATH
PATH=$HOME/bin:$HOME/.local/bin:$PATH
export PATH=$PATH:/opt/go/bin
export GOROOT=/opt/go
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/go/bin
]# source /etc/profile
2、安裝apisix
]# mkdir -pv /opt/apisix-2.0
]# cd /opt/src
]# wget https://downloads.apache.org/apisix/2.0/apache-apisix-2.0-src.tgz
]# tar zxvf apache-apisix-2.0-src.tgz -C /opt/apisix-2.0
]# ln -sf /opt/apisix-v2.0 /opt/apisix
]# cd /opt/apisix
]# ./bin/apisix start
驗證:
]# ps -ef | grep apisix
]# netstat -tnulp ## 9080、9443 端口
3、安裝apisix-dashboard 插件
]# cd /opt/
]# git clone https://github.com/apache/apisix-dashboard.git
]# cd apisix-dashboard
]# go env -w GO111MODULE=on
]# export APIX_ETCD_ENDPOINTS="127.0.0.1:2379"
]# go env -w GOPROXY=https://goproxy.cn,direct
]# vim api/run.sh
# config
export SYSLOG_HOST=0.0.0.0 ##修改IP,將127.0.0.1改成 0.0.0.0
]# nohuo ./api/run.sh > /dev/null &
]# yarn install
]# yarn build ##如果此步成功,在當前目錄下生成一個 dist 目錄,里面有web 程序
驗證:
]# ps -ef | grep manager-api
]# netstat -tnulp ##8080 端口
瀏覽器訪問: http://IP:8080 ##賬號密碼默認admin,登錄進去后添加內容,然后再打開其它兩台服務器的web 頁面會看到剛添加進去的內容