K8S 1.22部署Pgsql+Kong+Konga

部署Postgres 9.6​

注:K8S集群环境需要提前部署nfs存储,ingress-nginx,使用postgres:12.9版本时,部署konga后会一直报错,查询是版本问题,图省事直接按照网上选择的9.6版本

1.创建pvc

#pvc.yaml文件
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pgsql
namespace: kong-pg
spec:
storageClassName: pgsql-pvc
accessModes:
- ReadWriteMany
resources:
requests:
storage: 40Gi




2.创建pgsql

#pgsql.yaml文件
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgresql
namespace: kong-pg
spec:
serviceName: "postgres"
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
containers:
- name: postgresql
image: postgres:9.6
imagePullPolicy: IfNotPresent
#envFrom:
#- configMapRef:
# name: postgres-config
env:
- name: POSTGRES_DB
value: postgres
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: admin12345
- name: PGDATA
value: /var/lib/postgresql/data
ports:
- containerPort: 5432
protocol: TCP
name: postgresql-port
volumeMounts:
- name: postgresql-data
mountPath: /var/lib/postgresql/data
volumes:
- name: postgresql-data
persistentVolumeClaim:
claimName: pgsql
---
apiVersion: v1
kind: Service
metadata:
name: postgresql-svc
namespace: kong-pg
labels:
app: postgresql-svc
spec:
ports:
- name: postgresql-port
port: 5432
protocol: TCP
type: LoadBalancer
selector:
app: postgresql




3.创建并查看状态

K8S 1.22部署Pgsql+Kong+Konga_k8s1.22

部署Kong

#登录pgsql
psql -U postgres -h 10.244.2.28 -p5432
Password for user postgres:
#创建kong数据库
create user kong;
create database kong owner kong;
alter user kong with encrypted password 'kong12345';
grant all privileges on database kong to kong;



1.创建kong的pvc 

#conf-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: kong-conf
namespace: kong-pg
spec:
storageClassName: pgsql-pvc
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi


#plugin-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: kong-plugin
namespace: kong-pg
spec:
storageClassName: pgsql-pvc
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi



2.创建kong

#kong.yaml
# postgresql-svc.kong-pg.svc.cluster.local其中kong-pg是namespace名称
apiVersion: apps/v1
kind: Deployment
metadata:
name: kong
namespace: kong-pg
spec:
replicas: 1
selector:
matchLabels:
app: kong
template:
metadata:
labels:
app: kong
spec:
containers:
- name: kong
image: kong:2.7.0
imagePullPolicy: IfNotPresent
env:
- name: KONG_PG_USER
value: "kong"
- name: KONG_PG_PASSWORD
value: "kong12345"
- name: KONG_PG_HOST
value: "postgresql-svc.kong-pg.svc.cluster.local"
- name: KONG_PG_DATABASE
value: "kong"
- name: KONG_DATABASE
value: "postgres"
- name: KONG_PG_PORT
value: "5432"
- name: KONG_ADMIN_LISTEN
value: "0.0.0.0:8001, 0.0.0.0:8444 ssl"
- name: KONG_PROXY_ERROR_LOG
value: "/dev/stderr"
- name: KONG_PROXY_ACCESS_LOG
value: "/dev/stdout"
- name: KONG_ADMIN_ERROR_LOG
value: "/dev/stderr"
- name: KONG_ADMIN_ACCESS_LOG
value: "/dev/stdout"
command: ["/bin/sh","-c"]
args: [ "kong migrations bootstrap"]
ports:
- containerPort: 8000
protocol: TCP
name: kong-proxy
- containerPort: 8001
hostPort: 8001
protocol: TCP
name: kong-admin
- containerPort: 8443
protocol: TCP
name: kong-proxy-ssl
- containerPort: 8444
protocol: TCP
name: kong-admin-ssl
volumeMounts:
- name: kong-plugin
mountPath: /usr/local/share/lua/5.1/kong/plugins/cas
- name: kong-conf
mountPath: /etc/kong
volumes:
- name: kong-plugin
persistentVolumeClaim:
claimName: kong-plugin
- name: kong-conf
persistentVolumeClaim:
claimName: kong-conf
---
apiVersion: v1
kind: Service
metadata:
name: kong-svc
namespace: kong-pg
labels:
app: kong-svc
spec:
ports:
- name: kong-port
port: 8000
# nodePort: 30080
protocol: TCP
- name: kong-admin-port
port: 8001
protocol: TCP
type: ClusterIP
selector:
app: kong



注:kong要先执行kong migrations bootstrap,然后才能启动,不太会使用初始化容器,所以要先启动一次kong,然后把44-45行注释掉,在执行kubectl replace -f kong.yaml

3.创建并查看状态

K8S 1.22部署Pgsql+Kong+Konga_konga_02

部署konga:

1.创建konga

#konga.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: konga
namespace: kong-pg
spec:
replicas: 1
selector:
matchLabels:
app: konga
template:
metadata:
labels:
app: konga
spec:
containers:
- name: konga
image: pantsel/konga:latest
imagePullPolicy: IfNotPresent
env:
- name: DB_ADAPTER
value: "postgres"
- name: DB_DATABASE
value: "konga"
- name: DB_HOST
value: "postgresql-svc.kong-pg.svc.cluster.local"
- name: DB_USER
value: "konga"
- name: DB_PASSWORD
value: "konga12345"
- name: DB_PORT
value: "5432"
- name: DB_PG_SCHEMA
value: "public"
- name: KONGA_LOG_LEVEL
value: "debug"
- name: NODE_ENV
value: "development"
#value: "production"
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 1337
protocol: TCP
name: konga
---
apiVersion: v1
kind: Service
metadata:
name: konga-svc
namespace: kong-pg
labels:
app: konga-svc
spec:
ports:
- name: konga-port
port: 1337
protocol: TCP
type: ClusterIP
selector:
app: konga


注意: ENV这块要配置成development 不然会报错 kubectl create -f konga.yaml

2.创建并查看状态

K8S 1.22部署Pgsql+Kong+Konga_konga_03

配置Ingress

1.创建kong-ingress

#kong-ingrss.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: kong-ingress
namespace: kong-pg
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: 'kong.gateway.com'
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: kong-svc
port:
number: 8000

2.查看状态

K8S 1.22部署Pgsql+Kong+Konga_k8s1.22_04