欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

centos搭建kong+konga

程序员文章站 2022-06-03 20:18:10
...

centos搭建kong+konga

前言:最近正在学习kong网关 记录一下搭建服务的过程 也是百度东拼西凑出来的 基于华为云服务器 保证可用!

  1. gcc
  2. pcre
  3. zlib
  4. openssl
  5. postgresql9.6+
  6. konga

安装 gcc 编译环境:

sudo yum install -y pcre pcre-devel

pcre 安装

sudo yum install -y pcre pcre-devel

zlib 安装

sudo yum install -y zlib zlib-devel

openssl 安装

sudo yum install -y openssl openssl-devel

postgresql 安装
kong持久化数据有postgresql和cassandra两种数据库选择 这里选择postgresql
PS:不要图省事直接 yum install postgresql,因为这个版本比较低,kong不适用,至少要9.5+。

sudo yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-redhat-repo-42.0-11.noarch.rpm
yum install postgresql96-server

postgresql配置
为kong创建用户以及数据库

// 新建 linux kong 用户 
sudo adduser kong

// 使用管理员账号登录 psql 创建用户和数据库
// 切换 postgres 用户
// 切换 postgres 用户后,提示符变成 `-bash-4.2$` 
su postgres

// 进入 psql 控制台
psql

// 此时会进入到控制台(系统提示符变为'postgres=#')
// 先为管理员用户postgres修改密码
\password postgres

// 建立新的数据库用户(和之前建立的系统用户要重名)
create user kong with password '123456';

// 为新用户建立数据库
create database kong owner kong;

// 把新建的数据库权限赋予 kong
grant all privileges on database kong to kong;

// 退出控制台
\q

修改postgresql权限控制文件pg_hba.conf

local   all             all                                     trust
host    all             all             0.0.0.0/0               md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

修改postgresql文件pg_hba.conf以开启远程访问

listen_addresses = '*'

kong安装

yum install -y https://kong.bintray.com/kong-community-edition-rpm/centos/7/kong-community-edition-0.13.1.el7.noarch.rpm

修改kong配置文件

sudo cp /etc/kong/kong.conf.default /etc/kong/kong.conf
sudo vi /etc/kong/kong.conf

#------------------------------------------------------------------------------
# DATASTORE
#------------------------------------------------------------------------------

# Kong will store all of its data (such as APIs, consumers and plugins) in
# either Cassandra or PostgreSQL.
#
# All Kong nodes belonging to the same cluster must connect themselves to the
# same database.

database = postgres              # Determines which of PostgreSQL or Cassandra
                                 # this node will use as its datastore.
                                 # Accepted values are `postgres` and
                                 # `cassandra`.

pg_host = 127.0.0.1             # The PostgreSQL host to connect to.
pg_port = 5432                  # The port to connect to.
pg_user = kong                  # The username to authenticate if required.
pg_password = 123456            # The password to authenticate if required.
pg_database = kong              # The database name to connect to.

ssl = off                       # 如果不希望开放 8443 的 ssl 访问可关闭
#初始化kong数据库表
kong migrations up -c  /etc/kong/kong.conf
#启动
kong start
curl 127.0.0.1:8001

安装开源项目konga

#安装git
yum install -y git
curl --silent --location https://rpm.nodesource.com/setup_9.x | bash -
yum install -y nodejs
$ git clone https://github.com/pantsel/konga.git
$ cd konga
$ npm i
su - postgres 
psql
CREATE USER konga WITH PASSWORD '123456';
CREATE DATABASE konga OWNER konga;
GRANT ALL PRIVILEGES ON DATABASE konga to konga;
cp .env_example .env

cat .env

PORT=1337
NODE_ENV=production
KONGA_HOOK_TIMEOUT=120000
DB_ADAPTER=postgres
DB_URI=postgresql://konga:[email protected]:5432/konga
KONGA_LOG_LEVEL=warn
TOKEN_SECRET=some_secret_token

# 创建数据
node ./bin/konga.js  prepare --adapter postgres --uri postgresql://localhost:5432/konga
pm2 start npm --name 'konga'  -- run production
相关标签: kong 网关