簡體   English   中英

如何將Kong部署到Docker群?

[英]How to deploy Kong to docker swarm?

按照官方指南編寫堆棧部署文件:

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本身相關。

在以群體模式3版本的Compose文件部署堆棧時,將忽略depends_on選項。

我嘗試使用存儲在Consul to Swarm中的配置來部署Traefik。 該場景是:

  • 我必須先部署我的存儲后端: 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/找到示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM