Skip to content

Instantly share code, notes, and snippets.

@moonming
Created June 9, 2020 08:19
Show Gist options
  • Save moonming/32aed4922ea428db1abfe2edfd730b3a to your computer and use it in GitHub Desktop.
Save moonming/32aed4922ea428db1abfe2edfd730b3a to your computer and use it in GitHub Desktop.
How to use Consul as Registration Center in Apache APISIX?
## Install Consul
1. download consul
```
wget https://releases.hashicorp.com/consul/1.7.3/consul_1.7.3_linux_amd64.zip
```
2. unzip to `/usr/bin`
```
sudo unzip consul_1.7.3_linux_amd64.zip -d /usr/bin
```
3. create consul service file
```
sudo vim /lib/systemd/system/consul.service
[Unit]
Description=consul
[Service]
ExecStart=/usr/bin/consul agent -config-dir /etc/consul
KillSignal=SIGINT
```
4. create server json file
```
sudo mkdir /etc/consul/
sudo vim /etc/consul/server.json
{
"data_dir": "/var/consul",
"log_level": "INFO",
"node_name": "test",
"server": true,
"ui": true,
"bootstrap_expect": 1,
"client_addr": "0.0.0.0",
"advertise_addr": "127.0.0.1",
"ports": {
"dns": 53
},
"advertise_addr_wan": "127.0.0.1"
}
```
5. start consul
```
sudo systemctl start consul
```
## start service - golang version
```
git clone https://github.com/api7/consul-test-golang.git
cd consul-test-golang
nohup go run main.go &
```
## install etcd -- need by Apache APISIX
```
sudo yum install etcd
nohup /usr/bin/etcd --enable-v2=true &
```
## install openresty -- need by Apache APISIX
```
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
sudo yum install openresty -y
```
## install Apache APISIX
1. install from RPM, and you can get the latest version from https://github.com/apache/incubator-apisix/releases
```
wget https://github.com/apache/incubator-apisix/releases/download/1.3/apisix-1.3-0.el7.noarch.rpm
sudo yum install apisix-1.3-0.el7.noarch.rpm -y
```
2. change config.yaml
vi /usr/local/apisix/conf/config.yaml
add consul address to `dns_resolver`:
```
dns_resolver:
- 127.0.0.1
```
3. start Apache APISIX:
```
sudo apisix start
```
## Test
1. add route in Apache APISIX:
```
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"uri": "/healthz",
"upstream": {
"type": "roundrobin",
"nodes": {
"go-consul-test.service.consul:8080": 1
}
}
}'
```
**go-consul-test.service.consul is registered DNS SRV by consul-test-golang service**
2. test:
```
curl http://127.0.0.1:9080/healthz
```
will return:
```
{"message":"consul test healthz"}
```
Cool, it works!
If you have any questions, please goto https://github.com/apache/incubator-apisix
@houyinke
Copy link

houyinke commented May 7, 2021

环境;Centos7

adduser admin
yum install wget -y
wget https://releases.hashicorp.com/consul/1.7.3/consul_1.7.3_linux_amd64.zip

sudo unzip consul_1.7.3_linux_amd64.zip -d /usr/bin

sudo vim /lib/systemd/system/consul.service

[Unit]
Description=consul
[Service]
ExecStart=/usr/bin/consul agent -config-dir /etc/consul
KillSignal=SIGINT

sudo mkdir /etc/consul/
sudo vim /etc/consul/server.json

{
"data_dir": "/var/consul",
"log_level": "INFO",
"node_name": "test",
"server": true,
"ui": true,
"bootstrap_expect": 1,
"client_addr": "0.0.0.0",
"advertise_addr": "127.0.0.1",
"ports": {
"dns": 53
},
"advertise_addr_wan": "127.0.0.1"
}

sudo systemctl start consul

安装go环境

下载

wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz

解压压缩包到/usr/local目录

tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz

将/usr/local/go/bin 目录添加至PATH环境变量

vi /etc/profile

export PATH=$PATH:/usr/local/go/bin

命令行添加 最方便

echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile

重新读取

source /etc/profile

查看版本

go version

git clone https://github.com/api7/consul-test-golang.git

cd consul-test-golang

nohup go run main.go &

sudo yum install etcd

nohup /usr/bin/etcd --enable-v2=true &

wget https://openresty.org/package/centos/openresty.repo

sudo mv openresty.repo /etc/yum.repos.d/

sudo yum install openresty -y

wget https://github.com/apache/incubator-apisix/releases/download/1.3/apisix-1.3-0.el7.noarch.rpm

sudo yum install apisix-1.3-0.el7.noarch.rpm -y

vi /usr/local/apisix/conf/config.yaml

add consul address to dns_resolver

dns_resolver:

  • 127.0.0.1

sudo apisix start

[admin@apisix consul-test-golang]$ ps -ef|grep consul
root 429 1 0 10:59 ? 00:00:48 /usr/bin/consul agent -config-dir /etc/consul
admin 1267 927 0 17:12 pts/0 00:00:00 grep --color=auto consul

[admin@apisix consul-test-golang]$ ps -ef|grep etcd
admin 615 1 0 11:05 pts/0 00:03:02 /usr/bin/etcd --enable-v2=true
admin 1269 927 0 17:12 pts/0 00:00:00 grep --color=auto etcd

[admin@apisix consul-test-golang]$ ps -ef|grep go
root 1015 927 0 11:25 pts/0 00:00:00 sudo go run main.go
admin 1271 927 0 17:12 pts/0 00:00:00 grep --color=auto go

[admin@apisix consul-test-golang]$ ps -ef|grep apisix
root 993 1 0 11:20 ? 00:00:00 nginx: master process openresty -p /usr/local/apisix -c /usr/local/apisix/conf/nginx.conf
admin 1273 927 0 17:12 pts/0 00:00:00 grep --color=auto apisix

dig @127.0.0.1 go-consul-test.service.consul
#没有关联上
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.5 <<>> @127.0.0.1 go-consul-test.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 48770
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;go-consul-test.service.consul. IN A

;; AUTHORITY SECTION:
consul. 0 IN SOA ns.consul. hostmaster.consul. 1620378703 3600 600 86400 0

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: 五 5月 07 17:11:43 CST 2021
;; MSG SIZE rcvd: 108

curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"uri": "/healthz",
"upstream": {
"type": "roundrobin",
"nodes": {
"go-consul-test.service.consul:8080": 1
}
}
}'

返回201

HTTP/1.1 201 Created
Date: Fri, 07 May 2021 09:10:58 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: *
Access-Control-Max-Age: 3600
Server: APISIX web server

{"node":{"createdIndex":38,"key":"/apisix/routes/1","value":{"uri":"/healthz","priority":0,"upstream":{"type":"roundrobin","hash_on":"vars","nodes":{"go-consul-test.service.consul:8080":1}}},"modifiedIndex":38},"action":"set"}

curl http://127.0.0.1:9080/healthz

<title>500 Internal Server Error</title>

500 Internal Server Error


openresty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment