docker+etcd 无法远程访问?

version: "3"
services:
  etcd:
    container_name: etcd-standalone-etcd
    image: quay.io/coreos/etcd:v3.5.0
    ports:
      - "2379:2379"

  etcd-browser:
    container_name: etcd-standalone-etcdv3-browser
    image: rustyx/etcdv3-browser:latest
    ports:
      - "8804:8081"
    environment:
      - ETCD=etcd:2379
      - EDITABLE=1
    depends_on:
      - etcd

拉了一个 etcd

发现一个问题,在宿主机以 127.0.0.1 ,可以直接访问

╰─➤  nc -zv 127.0.0.1 2379                  
Connection to 127.0.0.1 2379 port [tcp/*] succeeded!

宿主机的 ip 是 192.168.2.15

2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 08:bf:b8:3f:b6:f9 brd ff:ff:ff:ff:ff:ff
    altname enp6s0
    inet 192.168.2.15/25 brd 192.168.2.127 scope global noprefixroute eno1
       valid_lft forever preferred_lft forever
    inet6 fe80::1958:af81:7fc8:edfb/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

但是无法在宿主机上用 192.168.2.15 访问

╰─➤  nc -zv 192.168.2.15 2379                  
nc: connect to 192.168.2.15 port 2379 (tcp) failed: Connection refused

不理解

端口是确实被监听着的

╰─➤  lsof -i:2379
COMMAND       PID USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
docker-pr 1978883 root    4u  IPv4 215957244      0t0  TCP *:2379 (LISTEN)
docker-pr 1978893 root    4u  IPv6 216099355      0t0  TCP *:2379 (LISTEN)

看日志,也没毛病

─➤  docker logs --tail=300 etcd-standalone-etcd           
{"level":"info","ts":"2023-08-31T03:56:21.141Z","caller":"etcdmain/etcd.go:72","msg":"Running: ","args":["/usr/local/bin/etcd"]}
{"level":"warn","ts":"2023-08-31T03:56:21.141Z","caller":"etcdmain/etcd.go:104","msg":"'data-dir' was empty; using default","data-dir":"default.etcd"}
{"level":"info","ts":"2023-08-31T03:56:21.141Z","caller":"embed/etcd.go:131","msg":"configuring peer listeners","listen-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":"2023-08-31T03:56:21.141Z","caller":"embed/etcd.go:139","msg":"configuring client listeners","listen-client-urls":["http://localhost:2379"]}
{"level":"info","ts":"2023-08-31T03:56:21.141Z","caller":"embed/etcd.go:307","msg":"starting an etcd server","etcd-version":"3.5.0","git-sha":"946a5a6f2","go-version":"go1.16.3","go-os":"linux","go-arch":"amd64","max-cpu-set":24,"max-cpu-available":24,"member-initialized":false,"name":"default","data-dir":"default.etcd","wal-dir":"","wal-dir-dedicated":"","member-dir":"default.etcd/member","force-new-cluster":false,"heartbeat-interval":"100ms","election-timeout":"1s","initial-election-tick-advance":true,"snapshot-count":100000,"snapshot-catchup-entries":5000,"initial-advertise-peer-urls":["http://localhost:2380"],"listen-peer-urls":["http://localhost:2380"],"advertise-client-urls":["http://localhost:2379"],"listen-client-urls":["http://localhost:2379"],"listen-metrics-urls":[],"cors":["*"],"host-whitelist":["*"],"initial-cluster":"default=http://localhost:2380","initial-cluster-state":"new","initial-cluster-token":"etcd-cluster","quota-size-bytes":2147483648,"pre-vote":true,"initial-corrupt-check":false,"corrupt-check-time-interval":"0s","auto-compaction-mode":"periodic","auto-compaction-retention":"0s","auto-compaction-interval":"0s","discovery-url":"","discovery-proxy":"","downgrade-check-interval":"5s"}
{"level":"info","ts":"2023-08-31T03:56:21.146Z","caller":"etcdserver/backend.go:81","msg":"opened backend db","path":"default.etcd/member/snap/db","took":"5.122239ms"}
{"level":"info","ts":"2023-08-31T03:56:21.161Z","caller":"etcdserver/raft.go:448","msg":"starting local member","local-member-id":"8e9e05c52164694d","cluster-id":"cdf818194e3a8c32"}
{"level":"info","ts":"2023-08-31T03:56:21.161Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d switched to configuration voters=()"}
{"level":"info","ts":"2023-08-31T03:56:21.161Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became follower at term 0"}
{"level":"info","ts":"2023-08-31T03:56:21.161Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]"}
{"level":"info","ts":"2023-08-31T03:56:21.161Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became follower at term 1"}
{"level":"info","ts":"2023-08-31T03:56:21.161Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d switched to configuration voters=(10276657743932975437)"}
{"level":"warn","ts":"2023-08-31T03:56:21.172Z","caller":"auth/store.go:1220","msg":"simple token is not cryptographically signed"}
{"level":"info","ts":"2023-08-31T03:56:21.178Z","caller":"mvcc/kvstore.go:415","msg":"kvstore restored","current-rev":1}
{"level":"info","ts":"2023-08-31T03:56:21.183Z","caller":"etcdserver/quota.go:94","msg":"enabled backend quota with default value","quota-name":"v3-applier","quota-size-bytes":2147483648,"quota-size":"2.1 GB"}
{"level":"info","ts":"2023-08-31T03:56:21.189Z","caller":"etcdserver/server.go:843","msg":"starting etcd server","local-member-id":"8e9e05c52164694d","local-server-version":"3.5.0","cluster-version":"to_be_decided"}
{"level":"info","ts":"2023-08-31T03:56:21.189Z","caller":"etcdserver/server.go:728","msg":"started as single-node; fast-forwarding election ticks","local-member-id":"8e9e05c52164694d","forward-ticks":9,"forward-duration":"900ms","election-ticks":10,"election-timeout":"1s"}
{"level":"info","ts":"2023-08-31T03:56:21.190Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d switched to configuration voters=(10276657743932975437)"}
{"level":"info","ts":"2023-08-31T03:56:21.190Z","caller":"membership/cluster.go:393","msg":"added member","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","added-peer-id":"8e9e05c52164694d","added-peer-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":"2023-08-31T03:56:21.190Z","caller":"embed/etcd.go:276","msg":"now serving peer/client/metrics","local-member-id":"8e9e05c52164694d","initial-advertise-peer-urls":["http://localhost:2380"],"listen-peer-urls":["http://localhost:2380"],"advertise-client-urls":["http://localhost:2379"],"listen-client-urls":["http://localhost:2379"],"listen-metrics-urls":[]}
{"level":"info","ts":"2023-08-31T03:56:21.190Z","caller":"embed/etcd.go:580","msg":"serving peer traffic","address":"127.0.0.1:2380"}
{"level":"info","ts":"2023-08-31T03:56:21.190Z","caller":"embed/etcd.go:552","msg":"cmux::serve","address":"127.0.0.1:2380"}
{"level":"info","ts":"2023-08-31T03:56:22.061Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d is starting a new election at term 1"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became pre-candidate at term 1"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d received MsgPreVoteResp from 8e9e05c52164694d at term 1"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became candidate at term 2"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became leader at term 2"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","caller":"etcdserver/server.go:2476","msg":"setting up initial cluster version using v2 API","cluster-version":"3.5"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","caller":"etcdserver/server.go:2027","msg":"published local member to cluster through raft","local-member-id":"8e9e05c52164694d","local-member-attributes":"{Name:default ClientURLs:[http://localhost:2379]}","request-path":"/0/members/8e9e05c52164694d/attributes","cluster-id":"cdf818194e3a8c32","publish-timeout":"7s"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","caller":"embed/serve.go:98","msg":"ready to serve client requests"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","caller":"etcdmain/main.go:47","msg":"notifying init daemon"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","caller":"etcdmain/main.go:53","msg":"successfully notified init daemon"}
{"level":"info","ts":"2023-08-31T03:56:22.062Z","caller":"embed/serve.go:140","msg":"serving client traffic insecurely; this is strongly discouraged!","address":"127.0.0.1:2379"}
{"level":"info","ts":"2023-08-31T03:56:22.064Z","caller":"membership/cluster.go:531","msg":"set initial cluster version","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","cluster-version":"3.5"}
{"level":"info","ts":"2023-08-31T03:56:22.064Z","caller":"api/capability.go:75","msg":"enabled capabilities for version","cluster-version":"3.5"}
{"level":"info","ts":"2023-08-31T03:56:22.064Z","caller":"etcdserver/server.go:2500","msg":"cluster version is updated","cluster-version":"3.5"}

但是通过同样访问起的 mysql8 ,一切正常

╰─➤  cat docker-compose.yaml         
version: "3"

services:
  # mysqld-exporter:
  #     container_name: mysqld-exporter
  #     image: prom/mysqld-exporter
  #     environment:
  #         DATA_SOURCE_NAME: root:123456@(192.168.26.174:3306)/
  #     ports:
  #         - "9104:9104"

  mysql8:
    container_name: mysql8
    image: mysql:8
    restart: always
    ports:
      - "3307:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=wph8xbT8UeQtyVq
    volumes:
      - ./volumes/:/var/lib/mysql

mysql8 就一点毛病没有

╰─➤  nc -zv 192.168.2.15 3307
Connection to 192.168.2.15 3307 port [tcp/*] succeeded!

修改成下面这样直接报错

version: "3"
services:
  etcd:
    container_name: etcd-standalone-etcd
    image: quay.io/coreos/etcd:v3.5.0
    command:
      - "--listen-client-urls=http://0.0.0.0:2379"
      - "--advertise-client-urls=http://0.0.0.0:2379"
    ports:
      - "2379:2379"

  etcd-browser:
    container_name: etcd-standalone-etcdv3-browser
    image: rustyx/etcdv3-browser:latest
    ports:
      - "8804:8081"
    environment:
      - ETCD=etcd:2379
      - EDITABLE=1
    depends_on:
      - etcd
╰─➤  docker-compose up -d                          
Recreating etcd-standalone-etcd ... error

ERROR: for etcd-standalone-etcd  Cannot start service etcd: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "--listen-client-urls=http://0.0.0.0:2379": stat --listen-client-urls=http://0.0.0.0:2379: no such file or directory: unknown

ERROR: for etcd  Cannot start service etcd: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "--listen-client-urls=http://0.0.0.0:2379": stat --listen-client-urls=http://0.0.0.0:2379: no such file or directory: unknown
ERROR: Encountered errors while bringing up the project.
阅读 3.9k
2 个回答

参考了 milvus 对 etcd 的配置,我发现是可以了

参考: https://github.com/milvus-io/milvus/blob/master/deployments/docker/standalone/docker-compose.yml
version: "3"
services:
  etcd:
    container_name: etcd-standalone-etcd
    image: quay.io/coreos/etcd:v3.5.0
    command: etcd -advertise-client-urls=http://127.0.0.1:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd
    ports:
      - "2379:2379"

  etcd-browser:
    container_name: etcd-standalone-etcdv3-browser
    image: rustyx/etcdv3-browser:latest
    ports:
      - "8804:8081"
    environment:
      - ETCD=etcd:2379
      - EDITABLE=1
    depends_on:
      - etcd

可以看到端口已经通了

╰─➤  nc -zv 192.168.2.15 2379
Connection to 192.168.2.15 2379 port [tcp/*] succeeded!

修改etcd的监听地址:

etcd:
  container_name: etcd-standalone-etcd
  image: quay.io/coreos/etcd:v3.5.0
  command:
    - "--listen-client-urls=http://0.0.0.0:2379"
    - "--advertise-client-urls=http://0.0.0.0:2379"
  ports:
    - "2379:2379"

重新启动:

docker-compose down
docker-compose up -d
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏