美文网首页API网关Kong实践笔记
MacDocker/K8s 部署 Kong Konga Post

MacDocker/K8s 部署 Kong Konga Post

作者: 国服最坑开发 | 来源:发表于2022-05-20 15:39 被阅读0次

    0x00 目的

    在MacDocker 环境部署kong, konga,postgres, 其中 postgres使用外部存储.

    0x01 部署带存储的Postgres

    # 在Mac Docker环境, 默认已经有一个本机存储的StorageClass
    # 直接部署下面的PVC, 可以动态绑定.
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: kong-db-pvc
      namespace: prod
    spec:
      accessModes:
        - ReadWriteOnce
      volumeMode: Filesystem
      resources:
        requests:
          storage: 1Gi
      storageClassName: hostpath
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: postgres
      namespace: prod
      labels:
        app: postgres
    spec:
      replicas: 1
      template:
        metadata:
          name: postgres
          labels:
            app: postgres
        spec:
          containers:
            - name: postgres
              image: postgres:9.6
              imagePullPolicy: IfNotPresent
              env:
                - name: POSTGRES_USER
                  value: kong
                - name: POSTGRES_DB
                  value: kong
                - name: POSTGRES_PASSWORD
                  value: kong
                - name: PGDATA
                  value: /var/lib/postgresql/data
                - name: POSTGRES_INITDB_WALDIR
                  value: /var/lib/postgresql/dblogs/logs
              volumeMounts:
                - name: data
                  mountPath: /var/lib/postgresql
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: kong-db-pvc
          restartPolicy: Always
      selector:
        matchLabels:
          app: postgres
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: postgres
      namespace: prod
    spec:
      selector:
        app: postgres
      ports:
        - port: 5432
          targetPort: 5432
      type: ClusterIP
    

    0x02 在kong pod中初始化 postgres 数据库

    部署一个无DB的Kong,进去初始化数据库

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: kong
      namespace: prod
      labels:
        app: kong
    spec:
      replicas: 1
      template:
        metadata:
          name: kong
          labels:
            app: kong
        spec:
          containers:
            - name: kong
              image: kong:2.5.1-alpine
              imagePullPolicy: IfNotPresent
              env:
                - name: KONG_DATABASE
                  value: off
          restartPolicy: Always
      selector:
        matchLabels:
          app: kong
    

    开始初始化:

    # docker exec -it kong-xxx  -- /bin/bash
    bash-5.1$ export KONG_DATABASE=postgres
    bash-5.1$ export KONG_PG_HOST=10.1.6.3
    bash-5.1$ export KONG_PG_PASSWORD=kong
    bash-5.1$ export KONG_PASSWORD=kong
    bash-5.1$ which kong
    /usr/local/bin/kong
    bash-5.1$ kong migrations bootstrap
    

    0x03 部署带数据库的 Kong

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: kong
      namespace: prod
      labels:
        app: kong
    spec:
      replicas: 1
      template:
        metadata:
          name: kong
          labels:
            app: kong
        spec:
          containers:
            - name: kong
              image: kong:2.5.1-alpine
              imagePullPolicy: IfNotPresent
              env:
                - name: KONG_DATABASE
                  value: postgres
                - name: KONG_PG_HOST
                  value: postgres
                - name: KONG_PG_USER
                  value: kong
                - name: KONG_PG_PASSWORD
                  value: kong
                - name: KONG_PROXY_ACCESS_LOG
                  value: /dev/stdout
                - name: KONG_ADMIN_ACCESS_LOG
                  value: /dev/stdout
                - name: KONG_PROXY_ERROR_LOG
                  value: /dev/stderr
                - name: KONG_ADMIN_ERROR_LOG
                  value: /dev/stderr
                - name: KONG_ADMIN_LISTEN
                  value: '0.0.0.0:8001, 0.0.0.0:8444 ssl'
              resources:
                requests:
                  cpu: 250m
                  memory: 750Mi
                limits:
                  cpu: 250m
                  memory: 750Mi
          restartPolicy: Always
      selector:
        matchLabels:
          app: kong
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: kong
      namespace: prod
    spec:
      selector:
        app: kong
      ports:
        - name: proxy
          port: 8000
          targetPort: 8000
        - name: admin
          port: 8001
          targetPort: 8001
      type: ClusterIP
    

    0x04 部署Konga

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: konga
      labels:
        app: konga
    spec:
      replicas: 1
      template:
        metadata:
          name: konga
          labels:
            app: konga
        spec:
          containers:
            - name: konga
              image: pantsel/konga
              imagePullPolicy: IfNotPresent
              env:
                - name: NODE_ENV
                  value: production
                - name: TOKEN_SECRET
                  value: somerandomstring
                - name: DB_ADAPTER
                  value: postgres
                - name: DB_HOST
                  value: postgres
                - name: DB_USER
                  value: kong
                - name: DB_PASSWORD
                  value: kong
                - name: DB_PG_SCHEMA
                  value: konga
          restartPolicy: Always
      selector:
        matchLabels:
          app: konga
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: konga
      namespace: prod
    spec:
      selector:
        app: konga
      ports:
        - port: 1337
          targetPort: 1337
      type: ClusterIP
    

    需要注意的是, konga 和 kong 是独立关系, 需要打开 konga网页后, 手动添加kong的信息:

    http://kong:8001
    

    后记: 如果要添加 自定义插件, 上面的 kong 部署方式 就不够用了, 目前感觉需要打自定义镜像了, 以后有空再说吧.

    0x05 EKS 上部署排坑

    • PVC部署参考另外的 nacos 文章可以解决
    • konga 数据库初始化办法 :

    先把下面的NODE_ENV 注释掉, 即: 开发者模式, 部署时, 会自动 创建 konga 表结构. 完成后, 可以再使用生产模式.

             - name: NODE_ENV
                  value: production
    
    • postgresql 访问
    kubectl exec -it postgresqlxxx  -- /bin/bash
    psql --username=kong
    create datbase konga
    

    相关文章

      网友评论

        本文标题:MacDocker/K8s 部署 Kong Konga Post

        本文链接:https://www.haomeiwen.com/subject/dwpfprtx.html