docker - 如何将Kong部署到docker swarm?

标签 docker docker-compose database-migration docker-swarm kong

跟随official guide编写堆栈部署文件:

stack.yml

version: "3"
services:
  kong-database:
    image: cassandra:3
    ports:
      - "9042:9042"
    networks:
      - kong-net

  kong-migration:
    image: kong:latest
    depends_on:
      - kong-database
    environment:
      - KONG_DATABASE=cassandra
      - KONG_CASSANDRA_CONTACT_POINTS=kong-database
    command: kong migrations up
    networks:
      - kong-net

  kong:
    image: kong:latest
    depends_on:
      - kong-database
      - kong-migration
    deploy:
      replicas: 3
    environment:
      - KONG_DATABASE=cassandra
      - KONG_CASSANDRA_CONTACT_POINTS=kong-database
      - KONG_PROXY_ACCESS_LOG=/dev/stdout
      - KONG_ADMIN_ACCESS_LOG=/dev/stdout
      - KONG_PROXY_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_ERROR_LOG=/dev/stderr
      - KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl
    ports:
      - "8000:8000"
      - "8443:8443"
      - "8001:8001"
      - "8444:8444"
    networks:
      - kong-net

networks:
  kong-net:

部署:
$ docker stack deploy -c stack.yml gateway

检查服务:
$ docker service ls
ID                  NAME                      MODE                REPLICAS            IMAGE                                             PORTS
xg3qld08ziio        gateway_kong              replicated          1/3                 kong:latest                                       *:8000-8001->8000-8001/tcp, *:8443-8444->8443-8444/tcp
kam7fw265ons        gateway_kong-database     replicated          1/1                 cassandra:3                                       *:9042->9042/tcp
kr0vqoc66izn        gateway_kong-migration    replicated          0/1                 kong:latest

检查gateway_kong日志:
gateway_kong.2.zn0tfalwxylx@ip-1-2-3-4    |        [C]: in function 'error'
gateway_kong.2.zxuwxm5xbroe@ip-1-2-3-4    |        init_by_lua:3: in main chunk
gateway_kong.2.zxuwxm5xbroe@ip-1-2-3-4    | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:172: [cassandra error] the current database schema does not match this version of Kong. Please run `kong migrations up` to update/initialize the database schema. Be aware that Kong migrations should only run from a single node, and that nodes running migrations concurrently will conflict with each other and might corrupt your database schema!
gateway_kong.1.zr8biqoaccfz@ip-1-2-3-4    |        init_by_lua:3: in main chunk
gateway_kong.1.zy0u3ul9gp0l@ip-1-2-3-4    |        init_by_lua:3: in main chunk
gateway_kong.2.zn0tfalwxylx@ip-1-2-3-4    |        /usr/local/share/lua/5.1/kong/init.lua:169: in function 'init'
gateway_kong.1.zy0u3ul9gp0l@ip-1-2-3-4    | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:172: [cassandra error] the current database schema does not match this version of Kong. Please run `kong migrations up` to update/initialize the database schema. Be aware that Kong migrations should only run from a single node, and that nodes running migrations concurrently will conflict with each other and might corrupt your database schema!
gateway_kong.2.zxuwxm5xbroe@ip-1-2-3-4    | stack traceback:
gateway_kong.2.zxuwxm5xbroe@ip-1-2-3-4    |        [C]: in function 'assert'
gateway_kong.2.zn0tfalwxylx@ip-1-2-3-4    |        init_by_lua:3: in main chunk
gateway_kong.1.zy0u3ul9gp0l@ip-1-2-3-4    | stack traceback:
gateway_kong.1.zy0u3ul9gp0l@ip-1-2-3-4    |        [C]: in function 'assert'
gateway_kong.2.zxuwxm5xbroe@ip-1-2-3-4    |        /usr/local/share/lua/5.1/kong/init.lua:172: in function 'init'
gateway_kong.2.zxuwxm5xbroe@ip-1-2-3-4    |        init_by_lua:3: in main chunk
gateway_kong.1.zy0u3ul9gp0l@ip-1-2-3-4    |        /usr/local/share/lua/5.1/kong/init.lua:172: in function 'init'
gateway_kong.1.zy0u3ul9gp0l@ip-1-2-3-4    |        init_by_lua:3: in main chunk
gateway_kong.3.zq9rrsn2pd2r@ip-1-2-3-5    |       /usr/local/share/lua/5.1/kong/init.lua:172: in function 'init'
gateway_kong.3.zq9rrsn2pd2r@ip-1-2-3-5    |       init_by_lua:3: in main chunk

已经定义了kong-migration并设置了命令kong migrations up。即使设置取决于depends_on,为什么也不能迁移?

最佳答案

因为这里没有答案,所以我会留下一些我最近学到的东西,即使这与Kong无关。这个问题似乎与容器启动顺序更相关,而不是与Kong本身相关。

The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.

我尝试使用存储在Consul to Swarm中的配置来部署Traefik。
该场景是:

  • 我必须首先部署我的存储后端:consul
  • 然后部署一个traefik实例,该实例会将配置上传到领事:traefik_init
  • 最后,我可以使用领事后端部署所有traefik实例:traefik

  • 我无法在同一堆栈中部署consultraefik_inittraefik
    由于我曾经使用Ansible,所以我创建了一个剧本,执行以下操作
  • 部署consul堆栈
  • 等待所有领事节点同步
  • 仅使用1个副本
  • traefik_init作为服务启动
  • 等待配置存储在领事
  • 部署traefik堆栈。

  • 如果我选择覆盖容器的ENTRYPOINTCMD来使用脚本来等待所需的服务启动,然后在启动时执行命令,则可以将所有服务部署在同一堆栈中。

    如果我还没有使用Ansible,那我肯定会走那条路。

    您可以查看wait-for-it(用于测试并等待TCP主机和端口的可用性的纯bash脚本)或wait-for(用于等待其他服务可用的脚本)的示例。

    您还可以在https://docs.docker.com/compose/startup-order/中找到示例

    关于docker - 如何将Kong部署到docker swarm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51038414/

    相关文章:

    docker - 无法访问 Dockerized Vue 的 localhost :8080 on host machine (despite EXPOSE-ing port)

    docker - 用于 Ingress 的 Docker Desktop kubernetes 上的空地址

    mysql - 在Docker中更改MYSQL_DATABASE等

    php - Laravel 迁移 : auto increment key when it is not primary

    python - Django 中用户的默认类型是什么?

    python - 使用已应用的迁移应用南迁移

    docker - 在 Fedora 上构建 Docker RPM

    python - 在 Docker 中安装和使用 pip 和 virtualenv

    redis - docker-compose中的Redis主从设置-只读从

    php - docker compose : container command not found 的问题