流浪的Docker Provisioner-无法卷曲到localhost

时间:2019-05-23 06:48:48

标签: docker curl vagrant vagrantfile kong

我正在尝试使用Vagrant和Docker模拟我们的微服务框架。我有一个Vagrant VM,可以用作API网关。在Vagrantfile中,我使用了Docker Provisioning来设置Kong。我的目标是使用cURL在配置中引导必要的Kong服务。

我的Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.hostname = 'apigateway'
  config.vm.box = "centos/7"
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.110.110"
  config.vm.synced_folder ".", "/vagrant"
  config.vm.provision "docker" do |docker|
    config.vm.provision "shell" do |sh|
      docker.pull_images "postgres:9.6"
      docker.pull_images "kong"
      docker.run "postgres:9.6",
        image: "postgres:9.6",
        args: "--name kong-db --network='host' -p 5432:5432 -e 'POSTGRES_USER=kong'  -e 'POSTGRES_DB=kong'  -e 'POSTGRES_PASSWORD=kong'",
        auto_assign_name: false
      sh.inline = "firewall-cmd --permanent --add-port=5432/tcp"
      sh.inline = "firewall-cmd --reload"
      docker.run "kong-migrations",
        image: "kong",
        cmd: "kong migrations bootstrap",
        args: "--name kong-migrations --network='host' -e 'KONG_DATABASE=postgres' -e 'KONG_PG_HOST=localhost' -e 'KONG_PG_USER=kong' -e 'KONG_PG_PASSWORD=kong'  -e 'KONG_PG_DATABASE=kong'",
        auto_assign_name: false,
        restart: "on-failure"
      docker.run "kong",
        image: "kong",
        args: "--name kong  --network='host' -p 8000:8000 -p 8443:8443 -p 8001:8001 -p 8444:8444 -e 'KONG_DATABASE=postgres' -e 'KONG_PG_HOST=localhost' -e 'KONG_PG_USER=kong' -e 'KONG_PG_PASSWORD=kong' -e 'KONG_PG_DATABASE=kong' -e 'KONG_ADMIN_LISTEN=127.0.0.1:8001, 127.0.0.1:8444 ssl'",
        auto_assign_name: false
      sh.inline = "firewall-cmd --permanent --add-port=8000/tcp"
      sh.inline = "firewall-cmd --permanent --add-port=8001/tcp"
      sh.inline = "firewall-cmd --permanent --add-port=8443/tcp"
      sh.inline = "firewall-cmd --permanent --add-port=8444/tcp"
      sh.inline = "firewall-cmd --reload"
      ###### The cURL shell scripts that raised the errors #######
      sh.inline = "docker exec kong curl -i -X POST --url http://localhost:8001/services/ --data 'name=admin-api' --data 'url=http://localhost:8001'"
      sh.inline = "docker exec kong curl -i -X POST --url http://localhost:8001/services/admin-api/routes --data 'path=/admin-api'"
      sh.inline = "docker exec kong curl -i -X POST --url http://localhost:8001/services/admin-api/plugins --data 'name=jwt'"
      ####### --End of cURL scripts #####
    end
  end
end

问题在于cURL脚本引发了错误

default: curl: (7) Failed to connect to localhost port 8001: Connection refused

但是,当我在vagrant ssh之后在VM中执行cURL脚本时,该操作将成功,即

docker exec kong curl -i -X POST --url http://localhost:8001/services/ --data 'name=admin-api' --data 'url=http://localhost:8001'

我尝试使用VM IP 192.168.110.110代替localhost,但得到Connection reset by peer

此设置有什么问题?应该怎么办?

1 个答案:

答案 0 :(得分:0)

如果在设置后直接与ssh连接时可以正常工作,那么我的猜测是您在vagrantfile中的卷曲太快了。
卷曲时,也许服务器尚未启动。 如果在卷曲之前稍等一下会怎样?可以吗?