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

Kong网关、konga、postgresql搭建及使用

程序员文章站 2022-06-17 17:48:10
...

Kong网关搭建及使用(centons7)

使用kong网关需要三步:
1.数据库安装(postgresql)
2.kong网关安装(kong)
3.UI界面操作(dashboard)

1.安装PostgreSql数据库

1. 安装postgresql源
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm -y
  1. 查看postgresql版本(postgresql-12版本,无法使用。选择10版本)
yum list | grep posgreesql

3.安装

yum install -y postgresql10-contrib.x86_64 postgresql10-server.x86_64
  1. 启动
/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl start postgresql-10 
systemctl enable postgresql-10.service
  1. 登录postgresql默认用户设置密码
su - postgres
psql

设置密码
ALTER USER postgres WITH PASSWORD '密码';

6.创建数据库

CREATE DATABASE konga;

7.创建用户

CREATE USER konga CREATEDB LOGIN PASSWORD 'Admin1233';

8.将kong数据库所有权限赋予kong用户

GRANT ALL ON DATABASE konga TO konga;
GRANT ALL ON DATABASE kongDB TO kongUSER;

9.默认情况下postgresql是不用密码不支持远程登录的,我们需要修改配置文件。

vim /var/lib/pgsql/10/data/pg_hba.conf

原来:
Kong网关、konga、postgresql搭建及使用
修改后:
Kong网关、konga、postgresql搭建及使用

vim /var/lib/pgsql/10/data/postgresql.conf

原文件:
Kong网关、konga、postgresql搭建及使用
改成远程访问:
Kong网关、konga、postgresql搭建及使用
重启

systemctl restart postgresql-10

2.安装kong网关

1.下载kong网关
wget https://bintray.com/kong/kong-rpm/download_file?file_path=centos/7/kong-1.4.0.el7.amd64.rpm

2.安装

yum install -y kong-1.4.0.el7.amd64.rpm

3.修改kong配置文件,把数据库的信息写进去
cp -r /etc/kong/kong.conf.default kong.conf
vim /etc/kong/kong.conf

原文件:
Kong网关、konga、postgresql搭建及使用

修改后:
Kong网关、konga、postgresql搭建及使用

4.初始化数据库

kong migrations bootstrap -c /etc/kong/kong.conf

5.启动kong网关

kong start -c /etc/kong/kong.conf

6.本地访问

curl -i http://127.0.0.1:8001

7.如果需要外地访问,需要更改配置文件

vim /etc/kong/kong.conf

原文件:
Kong网关、konga、postgresql搭建及使用
Kong网关、konga、postgresql搭建及使用
8.重启使配置生效

kong restart -c /etc/kong/kong.conf

3.安装kong-dashboard(konga)

个人推荐docker安装,比较简便快捷。下包安装需要解决各种环境问题,这里没有使用官方的dashboard,使用了第三方的konga比较好用

1.先安装node
node官网(https://nodejs.org/en/download/)

根据自己的需求下载可以下载不同的版本

wget https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x64.tar.xz

解压

xz -d node-v12.13.0-linux-x64.tar.xz

再解压

tar xvf node-v12.13.0-linux-x64.tar.xz

移动到/opt

mv node-v12.13.0-linux-x64 /opt/

设置软连接

ln -s /opt/node-v12.13.0-linux-x64/bin/npm /usr/bin/npm
ln -s /opt/node-v12.13.0-linux-x64/bin/node /usr/bin/node

配置淘宝镜像

npm install -g cnpm --registry=https://registry.npm.taobao.org

2.安装Git

yum install -y git
yum install -y gcc-c++

3.下载konga包并安装

git clone https://github.com/pantsel/konga.git
cd konga
npm i

4.docker-compose安装konga

1.下载konga镜像(我这里因为下载特别慢所以我是在阿里下载的)

docker pull konga
docker pull registry.cn-hangzhou.aliyuncs.com/zhanghongzhuang-k8s/konga:latest

2.创建konga数据库,并且进行初始化

docker run --rm registry.cn-hangzhou.aliyuncs.com/zhanghongzhuang-k8s/konga -c prepare -a postgres -u postgresql://用户名:密码@ip:(prot默认是5432)5432/数据库名字

举个栗子:

docker run --rm registry.cn-hangzhou.aliyuncs.com/zhanghongzhuang-k8s/konga -c prepare -a postgres -u postgresql://konga:aaa@qq.com:5432/konga

这样就是初始化成功了
Kong网关、konga、postgresql搭建及使用

3.编辑docker-compose.

vim docker-compose.yml

把下面的内容复制进去,即可。
version: '2.1'
services:
  kong:
    image: "registry.cn-hangzhou.aliyuncs.com/zhanghongzhuang-k8s/konga:latest"
    environment:
      DB_ADAPTER: postgres
      DB_HOST: 10.100.24.64
      DB_PORT: 5432
      DB_USER: konga
      DB_PASSWORD: Admin1233
      DB_DATABASE: konga
      NODE_ENV: production
    ports:
      - "1337:1337/tcp"
    restart: on-failure

4.启动

docker-compose up -d

看到这个页面就是成功了:
Kong网关、konga、postgresql搭建及使用

浏览器就行访问:
Kong网关、konga、postgresql搭建及使用

4.konga简单实用

1.进行注册

Kong网关、konga、postgresql搭建及使用
2.登录

Kong网关、konga、postgresql搭建及使用
3.添加kong网关的地址信息

Kong网关、konga、postgresql搭建及使用
4.监控页面

Kong网关、konga、postgresql搭建及使用