Microservice-kong installation, API gateway design (principle)

overview

The second key component of microservice practice, microservice API gateway design, API gateway is to implement unified authentication, current limiting, black and white list, load balancing and other functions for microservices. In this article, we will first introduce the meaning of Api gateway And the components needed to install kong/konga.

The role and significance of the gateway

The gateway can make the service itself more focused on its own field, and isolate the service provider and the service caller.

  • Collect multiple Apis and unify the Api entry
  • Avoid internal information leakage
  • Provide security verification
  • Support mixed communication protocols (Http/Rpc)
  • Reduce the complexity of microservices

shortcoming:

  • Collections add additional management and maintenance costs
  • Avoid the need to follow the routing rules of the gateway during development
  • prone to failure

Comparison of mainstream gateways

  • Nginx: Nginx has natural advantages as a gateway, high performance, reverse proxy and other functions
  • Zuul:
  • Kong: A platform focused on microservice API gateways

Kong implementation principle

  • Kong is a gateway component in microservices. It has high availability and scalability. It can provide the RestFul Api used to operate and configure the Api management system. At the same time, it can evenly distribute requests to each server through the load balancing function. , to handle a large number of requests.

  • Kong is the Api gateway and Api service management layer developed by the company. It is based on Nginx and OpenResty. It is a microservice abstraction layer with features and functions such as distributed, high performance, high concurrency, scalability, and sub-millisecond latency.

Conga

Kong's UI management interface, Konga in the form of a plug-in, the characteristics of Konga:

  • Multi-user management, manage multiple kong nodes
  • Backup, restore and migrate Kong nodes using snapshots
  • Monitor Node and Api Status with Health Check
  • Easy database integration postgresSQl

Kong key concepts

  • Upstream: Upstream represents a virtual host name, which can be used to load balance incoming requests through multiple services, corresponding to Nginx's Upstream concept.
  • Target: The IP address and host of the target, and its port represents the instance of the backend service. There can be multiple Targets, and the Target can be added dynamically.
  • Service : Refers to the upstream backend service, corresponding to the backend service configured by Nginx Upstream.
  • Route: corresponds to Nginx Location.
  • Consumer : Indicates the user or user of the service
  • Plugin: Kong can configure global and specific routing and service plug-ins through AdminApi, by embedding business logic in different life cycles and nodes of agent forwarding.

Install

The versions of these three images must match, otherwise various problems will occur when initializing kong and konga data.

docker pull postgres:9.6-bullseye
docker pull kong/kong-gateway:2.4.1.0-alpine
docker pull pantsel/konga:0.14.9

Create the network and put all dependencies networkinto one.

docker network create gateway_net

1. Install postgres, kong depends on postgres:

docker run -d --network=gateway_net --name postgres \
    -p 5432:5432 \
    -e "POSTGRES_USER=你的数据库用户名" \
    -e "POSTGRES_DB=你的数据库名" \
    -e "POSTGRES_PASSWORD=你的数据库密码" \
    postgres:9.6-bullseye

2. Initialize the kong data table information:

docker run --rm --network=gateway_net \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=postgres" \
  -e "KONG_PG_PASSWORD=kong" \
  -e "KONG_PASSWORD=kong" \
kong/kong-gateway:2.4.1.0-alpine kong migrations bootstrap

3. Start kong, kong has 5 ports that need to be exposed to the outside world:

  • 8000: The corresponding http request proxy port, which will be used when configuring the proxy later, here the exposed port is changed to 9000
  • 8001: Management port for the http interface
  • 8443: The proxy port of the corresponding https request
  • 8444: Management port of https interface
  • 8002: Do some data analysis on the api
docker run -d --name kong-ee --network=gateway_net \
  -e "KONG_DATABASE=postgres" \
  -e "KONG_PG_HOST=postgres" \
  -e "KONG_PG_PASSWORD=kong" \
  -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
  -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
  -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
  -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
  -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
  -e "KONG_ADMIN_GUI_URL=http://127.0.0.1:9002" \
    -p 9000:8000 \
    -p 9443:8443 \
    -p 9001:8001 \
    -p 9444:8444 \
    -p 9002:8002 \
  kong/kong-gateway:2.4.1.0-alpine

After kong starts, you can enter in the browser: http://127.0.0.1:9002/overview, you can check whether kong is installed successfully through this link.

** 4. Initialize konga data information **

docker run --rm --network=gateway_net \
pantsel/konga:latest -c prepare -a postgres -u \
postgresql://kong:kong@postgres:5432/kong

5. Start konga

docker run -d -p 1337:1337 --network gateway_net --name konga \
-e "DB_ADAPTER=postgres" \
-e "DB_URI=postgresql://kong:kong@postgres:5432/kong" \
-e "DB_PASSWORD=kong" \
-e "NODE_ENV=production" \
pantsel/konga:0.14.9

Kong, konga, and postgres are installed successfully.

insert image description here

Guess you like

Origin blog.csdn.net/xuezhiwu001/article/details/130391225