当前位置:网站首页>API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
2022-04-23 14:11:00 【anron】
一、前言
负载均衡是将访问流量根据转发策略分发到后端多台服务器的流量分发控制服务,可以通过流量分发扩展应用系统对外的服务能力,消除单点故障提升应用系统的可用性,常见的负载均衡有阿里云的SLB,华为云的ELB,Nginx等。
下面是Nginx的一个负载均衡配置例子:
upstream hello {
server 172.17.0.1:3000 weight=100;
server 172.17.0.1:3001 weight=100;
}
server {
listen 80;
location /hello {
proxy_pass http://hello;
}
}
接下来使用Kong实现相同的功能。
二、网络信息
我电脑的IP是192.168.1.51,安装Kong的宿主机IP是192.168.1.57和172.17.0.1(docker0),kong2_kong-net的网段是172.29.0.0,kong2_kong-net是docker-compose.yml中配置的网路。
[root@localhost kong2]# docker network ls
NETWORK ID NAME DRIVER SCOPE
8702510e499c bridge bridge local
d6460928cb8a host host local
1ade10679e59 kong2_kong-net bridge local
e00def8c74a2 none null local
[root@localhost kong2]# docker network inspect kong2_kong-net
[
{
"Name": "kong2_kong-net",
"Id": "1ade10679e59ba51f64113f42a18cdb49f5029f73475650237cc9f10415aa34c",
"Created": "2020-04-29T00:09:04.748628001+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.29.0.0/16",
"Gateway": "172.29.0.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"260a383f32c5a82d828a262fd26f9bb677a06f86b38d288d1c9fe03a9dc3a8e2": {
"Name": "kong-database",
"EndpointID": "a702002370d45dc6930c2379740762a59c4decfe6e54332f802d2ecb348f6e49",
"MacAddress": "02:42:ac:1d:00:03",
"IPv4Address": "172.29.0.2/16",
"IPv6Address": ""
},
"88c47fc737d002b9ffebb8e2ff604306997ed4388654477a157a68363f727b6d": {
"Name": "kong",
"EndpointID": "8ff362c0497e3664d43cf6e6564e558ee583b07dfb03b7a7617b4ca3f1aec12c",
"MacAddress": "02:42:ac:1d:00:06",
"IPv4Address": "172.29.0.4/16",
"IPv6Address": ""
},
"daa627c41d351331af22683d5bc29574c1fc703bcdd5df3f66e753d38b83dfd1": {
"Name": "konga",
"EndpointID": "4ea04ec7d9fa5bd8540f82e9c5c17540db4ebd074e20024a6d778fcb32c3b65a",
"MacAddress": "02:42:ac:1d:00:04",
"IPv4Address": "172.29.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "kong-net",
"com.docker.compose.project": "kong2",
"com.docker.compose.version": "1.25.0"
}
}
]
三、防火墙的配置
CentOS7用firewalld代替了原来的iptables,在192.168.1.57上通过firewalld-cmd开启端口或直接关闭firewalld防火墙
(1)开启端口:3000、3001、8000、8443
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --zone=public --add-port=3001/tcp --permanent
firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --zone=public --add-port=8443/tcp --permanent
命令的参数含义
--zone #作用域
--add-port=80/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效
(2)重新载入一下防火墙设置,使设置生效
firewall-cmd --reload
(3)查询端口号3000、3001、8000、8443是否开启
firewall-cmd --query-port=3000/tcp
firewall-cmd --query-port=3001/tcp
firewall-cmd --query-port=8000/tcp
firewall-cmd --query-port=8443/tcp
(4)查询有哪些端口是开启的
firewall-cmd --list-port
(5)停止firewalld服务
systemctl stop firewalld.service
(6)禁止firewalld服务开机启动
systemctl disable firewalld.service
#关闭防火墙后需求重启docker服务,否则会提示一下信息
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:
四、API接口服务
在192.168.1.57上运行2个Jar包提供API接口服务,各运行在3000和3001端口
4.1 AnronApplication.java文件
package com.anron;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class AnronApplication {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(AnronApplication.class, args);
}
@RequestMapping("/hi")
public String port() {
return environment.getProperty("server.port");
}
}
4.2 application.properties文件
#server.port=3000
server.port=3001
4.3 在192.168.1.51上测试
curl http://192.168.1.57:3000/hi
curl http://192.168.1.57:3001/hi
五、Konga UI配置负载均衡
5.1 注册Administrator account
第一次打开Konga需要注册一个账号
5.2 创建Connection
Kong Admin Url可以直接用kong主机名称(也是容器名称),因为都是同在kong2_kong-net网络中
5.3 创建Upstream
输入name=hello就可以了
5.4 编辑Upstream中的Target
添加172.17.0.1:3000和172.17.0.1:3001就可以了,因为2个Jar文件不是运行在docker-compose中,不在Kong2_kong-net网络中(网段:172.29.0.0),所以填宿主机的IP172.17.0.1
5.5 创建Service
输入name、host和port
host,即upstream中的name
port,默认是80,和upstream中的target可以不同,以upstream中的target为准
5.6 创建Router
输入paths和protocols,UI的交互有点特殊,输入内容后需要回车
5.7 配置完成
测试负载均衡,在192.168.1.51电脑调用API接口进行测试
curl http://192.168.1.57:8000/hello/hi
六、Kong API配置负载均衡
*** 通过Kong API也可以达到和Konga UI一样的效果 ***
创建一个名称 hello 的 upstream, 为 hello 添加两个负载均衡节点
curl -X POST http://localhost:8001/upstreams --data "name=hello"
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=172.17.0.1:3000" --data "weight=100"
curl -X POST http://localhost:8001/upstreams/hello/targets --data "target=172.17.0.1:3001" --data "weight=100"
配置一个service, host的值对应了 upstream 的名称,配置成功后会返回生成的 service 的 id
curl -X POST http://localhost:8001/services --data "name=service1" --data "host=hello"
为上面的 service 配置路由信息,service.id需要对应上面创建的service
curl -X POST http://localhost:8001/routes --data "paths[]=/hello" --data "service.id=3cd79ba1-93e8-4c04-a04f-304873536fd0"
七、总结
Kong 进程启动后会启动多个端口,每个端口功能也不一样:
-
8001 端口:http 管理 API;
-
8444 端口:https 管理 API;
-
8000 端口:接收处理 http 流量;
-
8443 端口:接收处理 https 流量;
Kong 的使用特别简单,需要搞懂几个概念就可以快速使用了:
-
Service:Service 是要对外暴露的上游服务;
-
Upstream:类似于Nginx反向代理配置的upstream;
-
Route:Route 定义了路由规则,外部流量如何路由到相应的 Service;
-
Consumer:类似账号的概念,可以设置不同的 Consumer 对 API 的访问限制。
版权声明
本文为[anron]所创,转载请带上原文链接,感谢
https://blog.csdn.net/anron/article/details/105732265
边栏推荐
猜你喜欢
关于云容灾,你需要知道这些
常见存储类型和FTP主被动模式解析
VMware15Pro在Deepin系统里面挂载真机电脑硬盘
统信UOS PHP7.2.3升级至PHP7.2.24
使用开源调研工具Prophet是一种什么体验?
什么是云迁移?云迁移的四种模式分别是?
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
KVM学习资源
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
随机推荐
HyperMotion云迁移完成阿里云专有云产品生态集成认证
dp-[NOIP2000]方格取数
Preview CSV file
在电视屏幕上进行debug调试
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
json date时间日期格式化
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
rsync+inotify远程同步
某政务云项目业务系统迁移调研实践
利用json-server在本地创建服务器请求
RecyclerView高级使用(一)-侧滑删除的简单实现
VMware 15pro mounts the hard disk of the real computer in the deepin system
Essential difference between restful WebService and gSOAP webservice
如何轻松做好一个项目
KVM学习资源
squid代理
JS parabola motion packaging method
Uni app message push
Some good articles on pthread multithreading