如何将Kong部署到Docker群?

时间:2018-06-26 08:35:15

标签: 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,为什么不能迁移?

1 个答案:

答案 0 :(得分:2)

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

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

我尝试使用存储在Consul中的配置来部署Traefik到Swarm。 情况是:

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

我无法在同一堆栈中部署consultraefik_inittraefik。 由于我曾经使用Ansible,所以我创建了一个剧本,内容如下:

  • 部署consul堆栈
  • 等待所有领事节点同步
  • traefik_init作为仅具有1个副本的服务启动
  • 等待配置存储在领事中
  • 部署traefik堆栈。

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

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

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

您还可以在https://docs.docker.com/compose/startup-order/

中找到示例。