【玩转Lighthouse】网关APISIX 的基础使用
主题内容
apisix
一个高性能网关。
搭建网站大家都知道使用 Nginx
但是配置 Nginx
比较繁琐,对于新手来说还是有一定的难度的。
apisix
既满足入门选手的简单的路由配置,又满足高级系统的复杂性。
选择演示apisix
是有以下原因:
- 低延时 0.2 毫秒
- 高性能 单核
18000
qps - 仪表盘
环境
为降低难度我们使用 【腾讯云 Lighthouse】 Docker 基础镜像
安装 apisix
查看 Docker 版本
- ubuntu@VM-16-6-ubuntu:~/apisix-docker/example$ docker info
- Client:
- Context: default
- Debug Mode: false
- Plugins:
- app: Docker App (Docker Inc., v0.9.1-beta3)
- buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
-
- Server:
- ERROR: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/info: dial unix /var/run/docker.sock: connect: permission denied
- errors pretty printing info
- ```
- 将当前用户添加到 Docker 用户组
- sudo su
- usermod -aG docker ubuntu
为降低难度我们使用 docker-compose
安装
- #将 Apache APISIX 的 Docker 镜像下载到本地
- git clone https://github.com/apache/apisix-docker.git
- # 将当前的目录切换到 apisix-docker/example 路径下
- cd apisix-docker/example
- # 运行 docker-compose 命令,安装 Apache APISIX
- docker-compose -p docker-apisix up -d
删除容器
docker-compose -p docker-apisix down -v
- github 超时可以使用
- git clone https://ghproxy.com/https://github.com/apache/apisix-docker.git
注意:请在运行 Docker 的宿主机上执行 curl 命令。
- curl "http://127.0.0.1:9080/apisix/admin/services/" -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'
- 返回数据如下
- {
- "count":0,
- "action":"get",
- "node":{
- "key":"/apisix/services",
- "nodes":[],
- "dir":true
- }
- }
yml
分析
- ubuntu@VM-16-6-ubuntu:~/apisix-docker/example$ ls
- apisix_conf docker-compose-alpine.yml etcd_conf prometheus_conf
- apisix_log docker-compose-arm64.yml grafana_conf upstream
- dashboard_conf docker-compose.yml mkcert
控制面板
- apisix-dashboard:
- image: apache/apisix-dashboard:2.10.1-alpine
- restart: always
- volumes: # 配置文件映射
- - ./dashboard_conf/conf.yaml:/usr/local/apisix-dashboard/conf/conf.yaml
- ports:
- - "9000:9000" # 端口号9000
- networks:
- apisix:
- 我们可以在本地配置文件中修改密码
vim ./dashboard_conf/conf.yaml
- expire_time: 3600 # jwt token expire time, in second
- users: # yamllint enable rule:comments-indentation
- - username: admin # username and password for login `manager api`
- password: admin
- - username: user
- password: user
网关
- apisix:
- image: apache/apisix:2.13.0-alpine
- restart: always
- volumes:
- - ./apisix_log:/usr/local/apisix/logs
- - ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
- depends_on:
- - etcd
- ##network_mode: host
- ports:
- - "9080:9080/tcp" # http 端口 9080
- - "9091:9091/tcp" # metrics 指标端口 /apisix/prometheus/metrics
- - "443:9443/tcp" # https 端口 改为 443
- - "9092:9092/tcp" # Control API 接口 暴露 APISIX 内部状态信息
- networks:
- apisix:
Prometheus 监控
- prometheus:
- image: prom/prometheus:v2.25.0
- restart: always
- volumes: # 配置文件路径
- - ./prometheus_conf/prometheus.yml:/etc/prometheus/prometheus.yml
- ports:
- - "9090:9090"
- networks:
- apisix:
Grafana 面板
- grafana:
- image: grafana/grafana:7.3.7
- restart: always
- ports:
- - "3000:3000"
- volumes:
- - "./grafana_conf/provisioning:/etc/grafana/provisioning"
- - "./grafana_conf/dashboards:/var/lib/grafana/dashboards"
- - "./grafana_conf/config/grafana.ini:/etc/grafana/grafana.ini"
- networks:
- apisix:
配置路由
- 示例中已经创建 web 应用
- curl http://127.0.0.1:9081/
- hello world
- 外网访问, 注意防火墙开端口
创建上游服务器,这里是我们的 web1 应用
因为是 docker 部署,这里需要使用docker对外端口
172.17.0.1
- 配置路由
/hello
,选择刚刚创建的上游 - 放行 9080 端口