Traefik集群未能在Consul中找到密钥

时间:2018-06-12 16:32:23

标签: consul traefik docker-swarm-mode

我决定关注this指南,但我遇到了很多问题。

首先需要在traefik中指定command命令,否则我发现entrypoint.sh无法找到命令storedata的错误,和是> yaml语法是在docker-compose.yml中传递多行命令的有效方法

所以这里有一个docker-compose.yml:

docker-compose.yml
    visualizer:
      image: dockersamples/visualizer:latest
      volumes:
        - "/var/run/docker.sock:/var/run/docker.sock"
      networks:
        - traefik
        - default
      ports:
        - "8001:8080"
      deploy:
        labels:
          - "traefik.port=8080"
          - "traefik.tags=monitoring"
          - "traefik.docker.network=infra_traefik"
          - "traefik.backend=visualizer"
          - "traefik.frontend.rule=Host:visualizer.swarm.xxx.io"
          - "traefik.frontend.auth.basic=admin:$$apr1$$dxw2H03E$$VWrfVhKQWyaRiZ4XsfWCK/"
        restart_policy:
          condition: on-failure
        replicas: 1
        placement:
          constraints:
            - node.labels.name == master
    consul:
      image: consul
      command: agent -server -bootstrap-expect=1
      volumes:
        - consul-data:/consul/data
      environment:
        - CONSUL_LOCAL_CONFIG={"datacenter":"ams3","server":true}
        - CONSUL_BIND_INTERFACE=eth0
        - CONSUL_CLIENT_INTERFACE=eth0
      deploy:
        labels:
          - "traefik.enable=false"
        replicas: 1
        placement:
          constraints:
            - node.role == manager
        restart_policy:
         condition: on-failure
      networks:
        - traefik
    proxy_init:
      image: traefik:1.6.3-alpine
      command: >
         traefik
         storeconfig
         --api
         --entrypoints='Name:http Address::80 Redirect.EntryPoint:https'
         --entrypoints='Name:https Address::443 TLS'
         --defaultentrypoints=http,https
         --acme
         --acme.storage="traefik/acme/account"
         --acme.entryPoint=https
         --acme.httpChallenge.entryPoint=http
         --acme.onHostRule=true
         --acme.acmelogging=true
         --acme.onDemand=false
         --acme.email="xxx@gmail.com"
         --docker
         --docker.swarmMode
         --docker.domain=swarm.xxx.io
         --docker.watch
         --consul
         --consul.endpoint=consul:8500
         --consul.prefix=traefik
         --accesslogsfile=/dev/stdout
         --debug
      networks:
         - traefik
      deploy:
         placement:
            constraints:
              - node.role == manager
         restart_policy:
            condition: on-failure
      depends_on:
        - consul
    proxy:
      image: traefik:1.6.3-alpine
      depends_on:
        - traefik_init
        - consul
      command: >
        traefik
        --consul
        --consul.watch
        --consul.endpoint=consul:8500
        --consul.prefix=traefik
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock
      networks:
        - traefik
      ports:
        - 80:80
        - 443:443
        - 8080:8080
      deploy:
        labels:
          - "traefik.docker.network=infra_traefik"
          - "traefik.port=8080"
          - "traefik.tags=monitoring"
          - "traefik.backend.loadbalancer.stickiness=true"
          - "traefik.frontend.passHostHeader=true"
          - "traefik.frontend.rule=Host:proxy.swarm.xxx.io"
          - "traefik.frontend.auth.basic=admin:$$apr1$$hfqD9TtY$$oGSy9nS."
        mode: global
        restart_policy:
          condition: on-failure
        placement:
          constraints:
            - node.role == manager
        update_config:
          parallelism: 1
          delay: 10s
      volumes:
        - "/var/run/docker.sock:/var/run/docker.sock"
networks:
  traefik:
      driver: overlay
volumes:
  portainer-data:
    driver: local
  consul-data:
    driver: local
  traefik-data:
    driver: local

以下是proxy_init容器的日志显示:

infra_proxy_init.1.4rtllualg8od@swarm-manager-0    | 2018/06/12 15:41:35 Storing configuration: 
{
  "LifeCycle": {
    "RequestAcceptGraceTimeout": 0,
    "GraceTimeOut": 10000000000
  },
  "GraceTimeOut": 0,
  "Debug": true,
  "CheckNewVersion": true,
  "SendAnonymousUsage": false,
  "AccessLogsFile": "/dev/stdout",
  "AccessLog": null,
  "TraefikLogsFile": "",
  "TraefikLog": null,
  "Tracing": null,
  "LogLevel": "DEBUG",
  "EntryPoints": {
    "http": {
      "Address": ":80",
      "TLS": null,
      "Redirect": {
        "entryPoint": "https"
      },
      "Auth": null,
      "WhitelistSourceRange": null,
      "WhiteList": null,
      "Compress": false,
      "ProxyProtocol": null,
      "ForwardedHeaders": {
        "Insecure": true,
        "TrustedIPs": null
      }
    },
    "https": {
      "Address": ":443",
      "TLS": {
        "MinVersion": "",
        "CipherSuites": null,
        "Certificates": [],
        "ClientCAFiles": null,
        "ClientCA": {
          "Files": null,
          "Optional": false
        }
      },
      "Redirect": null,
      "Auth": null,
      "WhitelistSourceRange": null,
      "WhiteList": null,
      "Compress": false,
      "ProxyProtocol": null,
      "ForwardedHeaders": {
        "Insecure": true,
        "TrustedIPs": null
      }
    }
  },
  "Cluster": null,
  "Constraints": [],
  "ACME": {
    "Email": "xxx@gmail.com",
    "Domains": null,
    "Storage": "traefik/acme/account",
    "StorageFile": "",
    "OnDemand": false,
    "OnHostRule": true,
    "CAServer": "",
    "EntryPoint": "https",
    "DNSChallenge": null,
    "HTTPChallenge": {
      "EntryPoint": "http"
    },
    "DNSProvider": "",
    "DelayDontCheckDNS": 0,
    "ACMELogging": true,
    "TLSConfig": null
  },
  "DefaultEntryPoints": [
    "http",
    "https"
  ],
  "ProvidersThrottleDuration": 2000000000,
  "MaxIdleConnsPerHost": 200,
  "IdleTimeout": 0,
  "InsecureSkipVerify": false,
  "RootCAs": null,
  "Retry": null,
  "HealthCheck": {
    "Interval": 30000000000
  },
  "RespondingTimeouts": null,
  "ForwardingTimeouts": null,
  "AllowMinWeightZero": false,
  "Web": null,
  "Docker": {
    "Watch": true,
    "Filename": "",
    "Constraints": null,
    "Trace": false,
    "TemplateVersion": 0,
    "DebugLogGeneratedTemplate": false,
    "Endpoint": "unix:///var/run/docker.sock",
    "Domain": "swarm.xxx.io",
    "TLS": null,
    "ExposedByDefault": true,
    "UseBindPortIP": false,
    "SwarmMode": true
  },
  "File": null,
  "Marathon": null,
  "Consul": {
    "Watch": true,
    "Filename": "",
    "Constraints": [],
    "Trace": false,
    "TemplateVersion": 0,
    "DebugLogGeneratedTemplate": false,
    "Endpoint": "consul:8500",
    "Prefix": "traefik",
    "TLS": null,
    "Username": "",
    "Password": ""
  },
  "ConsulCatalog": null,
  "Etcd": null,
  "Zookeeper": null,
  "Boltdb": null,
  "Kubernetes": null,
  "Mesos": null,
  "Eureka": null,
  "ECS": null,
  "Rancher": null,
  "DynamoDB": null,
  "ServiceFabric": null,
  "Rest": null,
  "API": {
    "EntryPoint": "traefik",
    "Dashboard": true,
    "Debug": false,
    "CurrentConfigurations": null,
    "Statistics": null
  },
  "Metrics": null,
  "Ping": null
}

其次,我已经指定了该手册中的所有内容,所有的领事前缀等等,而Traefik说他无法找到前端和后端,也可以像{{1}中的任何内容一样}:

traefik/

以及来自consul容器的日志:

infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/backends/\": Key not found in store"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/frontends/\": Key not found in store"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Transaction 8714f767-ed5a-477f-ae46-6ebd0b4e15c2 begins"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/tls/\": Key not found in store"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Configuration received from provider consul: {}"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=info msg="Skipping same configuration for provider consul"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot get key traefik/alias Key not found in store, setting default traefik"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/backends/\": Key not found in store"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:54:59Z" level=debug msg="Datastore reload"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=error msg="Datastore sync error: Object lock value: expected 8714f767-ed5a-477f-ae46-6ebd0b4e15c2, got 068a8a6d-66a9-4d01-b44e-020a601c05da, retrying in 677.561632ms"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Datastore reload"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Datastore reload"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/frontends/\": Key not found in store"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot get key traefik/alias Key not found in store, setting default traefik"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/tls/\": Key not found in store"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/backends/\": Key not found in store"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/frontends/\": Key not found in store"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Configuration received from provider consul: {}"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:55:00Z" level=debug msg="Cannot list keys under \"traefik/tls/\": Key not found in store"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:55:00Z" level=debug msg="Configuration received from provider consul: {}"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:55:00Z" level=info msg="Skipping same configuration for provider consul"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=info msg="Skipping same configuration for provider consul"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot get key traefik/alias Key not found in store, setting default traefik"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/backends/\": Key not found in store"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:55:00Z" level=debug msg="Building ACME client..."
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:54:59Z" level=debug msg="Cannot list keys under \"traefik/frontends/\": Key not found in store"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:55:00Z" level=debug msg="Cannot list keys under \"traefik/tls/\": Key not found in store"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:55:00Z" level=debug msg="Configuration received from provider consul: {}"
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:55:00Z" level=info msg="Skipping same configuration for provider consul"
infra_proxy.0.vzv9q4rns6r7@swarm-manager-0    | time="2018-06-12T15:55:00Z" level=debug msg="Using HTTP Challenge provider."
infra_proxy.0.xu6e4zez242f@swarm-master    | time="2018-06-12T15:55:00Z" level=debug msg="Reset ACME account object."

1 个答案:

答案 0 :(得分:2)

问题与command有关,它必须是数组而不是多行字符串(>)。

另请注意,对于alpine版本(仅适用于alpine版本),您需要在traefik之前添加storeconfig

proxy_init:
  image: traefik:1.6.3-alpine
  command:
     - "traefik"
     - "storeconfig"
     - ...

-

无效:

command: >
    traefik
    --consul
    --consul.watch
    --consul.endpoint=consul:8500
    --consul.prefix=traefik

有效的:

command:
    - "traefik"
    - "--consul"
    - "--consul.watch"
    - "--consul.endpoint=consul:8500"
    - "--consul.prefix=traefik"

-

visualizer:
  image: dockersamples/visualizer:latest
  volumes:
    - "/var/run/docker.sock:/var/run/docker.sock"
  networks:
    - traefik
    - default
  ports:
    - "8001:8080"
  deploy:
    labels:
      - "traefik.port=8080"
      - "traefik.tags=monitoring"
      - "traefik.docker.network=infra_traefik"
      - "traefik.backend=visualizer"
      - "traefik.frontend.rule=Host:visualizer.swarm.xxx.io"
      - "traefik.frontend.auth.basic=admin:$$apr1$$dxw2H03E$$VWrfVhKQWyaRiZ4XsfWCK/"
    restart_policy:
      condition: on-failure
    replicas: 1
    placement:
      constraints:
        - node.labels.name == master
consul:
  image: consul
  command: agent -server -bootstrap-expect=1
  volumes:
    - consul-data:/consul/data
  environment:
    - CONSUL_LOCAL_CONFIG={"datacenter":"ams3","server":true}
    - CONSUL_BIND_INTERFACE=eth0
    - CONSUL_CLIENT_INTERFACE=eth0
  deploy:
    labels:
      - "traefik.enable=false"
    replicas: 1
    placement:
      constraints:
        - node.role == manager
    restart_policy:
     condition: on-failure
  networks:
    - traefik
proxy_init:
  image: traefik:1.6.3-alpine
  command:
     - "traefik"
     - "storeconfig"
     - "--api"
     - "--entrypoints=Name:http Address::80 Redirect.EntryPoint:https"
     - "--entrypoints=Name:https Address::443 TLS"
     - "--defaultentrypoints=http,https"
     - "--acme"
     - "--acme.storage=traefik/acme/account"
     - "--acme.entryPoint=https"
     - "--acme.httpChallenge.entryPoint=http"
     - "--acme.onHostRule=true"
     - "--acme.acmelogging=true"
     - "--acme.onDemand=false"
     - "--acme.email=xxx@gmail.com"
     - "--docker"
     - "--docker.swarmMode"
     - "--docker.domain=swarm.xxx.io"
     - "--docker.watch"
     - "--consul"
     - "--consul.endpoint=consul:8500"
     - "--consul.prefix=traefik"
     - "--accesslogsfile=/dev/stdout"
     - "--debug"
  networks:
     - traefik
  deploy:
     placement:
        constraints:
          - node.role == manager
     restart_policy:
        condition: on-failure
  depends_on:
    - consul
proxy:
  image: traefik:1.6.3-alpine
  depends_on:
    - traefik_init
    - consul
  command:
    - "traefik"
    - "--consul"
    - "--consul.watch"
    - "--consul.endpoint=consul:8500"
    - "--consul.prefix=traefik"
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
  networks:
    - traefik
  ports:
    - 80:80
    - 443:443
    - 8080:8080
  deploy:
    labels:
      - "traefik.docker.network=infra_traefik"
      - "traefik.port=8080"
      - "traefik.tags=monitoring"
      - "traefik.backend.loadbalancer.stickiness=true"
      - "traefik.frontend.passHostHeader=true"
      - "traefik.frontend.rule=Host:proxy.swarm.xxx.io"
      - "traefik.frontend.auth.basic=admin:$$apr1$$hfqD9TtY$$oGSy9nS."
    mode: global
    restart_policy:
      condition: on-failure
    placement:
      constraints:
        - node.role == manager
    update_config:
      parallelism: 1
      delay: 10s
  volumes:
    - "/var/run/docker.sock:/var/run/docker.sock"
networks:
  traefik:
    driver: overlay
volumes:
  portainer-data:
    driver: local
  consul-data:
    driver: local
  traefik-data:
    driver: local