从主机访问

时间:2018-08-29 11:34:23

标签: docker redis docker-compose containers

我想创建6个Redis Docker容器并将其设置为从Windows主机作为群集工作。为此,首先,我创建了一个redis配置文件:

port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

和Dockerfile:

FROM redis
COPY clusterconfig.conf /usr/local/redis.conf
CMD ["redis-server", "/usr/local/redis.conf"]

此后,创建了docker-compose.yml(删除了一些行以减少行数。完整文件为here。)

version: "3.1"
services:
  redis-master-1:
    build: ./
    networks:
      redisnet:
        ipv4_address: 10.0.0.2
    command: sh -c "redis-server /usr/local/redis.conf --port 7000 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000"
  redis-master-2:
    build: ./
    networks:
      redisnet:
        ipv4_address: 10.0.0.3
    depends_on:
      - redis-master-1
    command: sh -c "redis-server /usr/local/redis.conf --port 7001 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000"
  // Goes like this and created 4 more redis containers
  redis-setup:
    image: ruby
    command: sh -c "gem install redis && wget http://download.redis.io/redis-stable/src/redis-trib.rb && yes yes | ruby redis-trib.rb create --replicas 1 10.0.0.2:7000 10.0.0.3:7001 10.0.0.4:7002 10.0.0.5:7003 10.0.0.6:7004 10.0.0.7:7005 && while sleep 3600; do :; done"
    networks:
      redisnet:
        ipv4_address: 10.0.0.8
    depends_on:
      - redis-master-1
      - redis-master-2
      - redis-master-3
      - redis-slave-1
      - redis-slave-2
      - redis-slave-3
networks:
  redisnet:
    driver: bridge
    ipam:
      config:
        - subnet: 10.0.0.0/16

现在,当我执行docker-compose up命令时,6 Redis实例将按预期开始在群集模式下运行。现在如何从主机访问我的Redis实例?

如果您要检查所有文件并想尝试一下,请看这里:https://github.com/erayalakese/redis-cluster-setup

您可能需要更多信息

docker network ls
NETWORK ID          NAME                                    DRIVER              SCOPE
4da303d641aa        bridge                                  bridge              local
9e2c638d95dd        docker-compose-redis-cluster_redisnet   bridge              local
46802cf228a5        docker-redis-cluster_default            bridge              local
b217669ef5ea        host                                    host                local
0c10d151be33        none                                    null                local
c2d896128f38        red_cluster                             bridge              local
167fd1194f28        wordpressmount_default                  bridge              local


docker inspect docker-compose-redis-cluster_redisnet
[
    {
        "Name": "docker-compose-redis-cluster_redisnet",
        "Id": "9e2c638d95dd17b631425e55a17aa913d80b723f26a73e32ecce62952707d0a3",
        "Created": "2018-08-29T08:53:19.4313459Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "10.0.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "00c31aec7f84814b1591db5b374dc78b580ae54a3f4cf1eee333e03abea31398": {
                "Name": "docker-compose-redis-cluster_redis-slave-2_1",
                "EndpointID": "6e4a3badd31f45f0989d8b01450cef4bf4ed4a0397cc7c2b337271a5fdcb46cd",
                "MacAddress": "02:42:0a:00:00:06",
                "IPv4Address": "10.0.0.6/16",
                "IPv6Address": ""
            },
            "30a80a3b713d92cccc5707f782ca22fc303c727d0c829bc92949e7d36dfa9172": {
                "Name": "docker-compose-redis-cluster_redis-slave-1_1",
                "EndpointID": "e58f96264789fff4e594f4c273ef43d157bce51fe0cc57da6f795891dd3b69d5",
                "MacAddress": "02:42:0a:00:00:05",
                "IPv4Address": "10.0.0.5/16",
                "IPv6Address": ""
            },
            "77b9e91cf0ea532c000b016f6f31398717875293f41568695100d229aa9d9158": {
                "Name": "docker-compose-redis-cluster_redis-master-1_1",
                "EndpointID": "70cf615111886b7ecbecdd48ec3af60ae559fc6261c8ea7c3e951ad8aa377134",
                "MacAddress": "02:42:0a:00:00:02",
                "IPv4Address": "10.0.0.2/16",
                "IPv6Address": ""
            },
            "c3e1c4f751b328e17d053d7ce8ffe9fb5fedee2f66cc723dce7e221050c427e1": {
                "Name": "docker-compose-redis-cluster_redis-setup_1",
                "EndpointID": "a964749853ca9682adbf4aa129d01d4a957d5128837fd1c3d2eab91b5dd403b5",
                "MacAddress": "02:42:0a:00:00:08",
                "IPv4Address": "10.0.0.8/16",
                "IPv6Address": ""
            },
            "fbebf359ef7ba5b3647dfdd27ead590eeb0dd241151a0114eaac15ff544e919a": {
                "Name": "docker-compose-redis-cluster_redis-slave-3_1",
                "EndpointID": "9e70e4846d38f1e004868f7c06b019dcd0c5ad7156800b7a891e96fc3cd521f4",
                "MacAddress": "02:42:0a:00:00:07",
                "IPv4Address": "10.0.0.7/16",
                "IPv6Address": ""
            },
            "fda9df6bf462b1dee12a67765fecb871fcc85c3292967fee63614a6e5dbdfaba": {
                "Name": "docker-compose-redis-cluster_redis-master-3_1",
                "EndpointID": "7a4121c8f6dc7d08114be318d9ea227070aba15fcdaecd96c187b04b81c524d6",
                "MacAddress": "02:42:0a:00:00:04",
                "IPv4Address": "10.0.0.4/16",
                "IPv6Address": ""
            },
            "ffc6a99e5be4b1e1d77519f86844078f6c59b7fe2d0319d54886bc1863a50b48": {
                "Name": "docker-compose-redis-cluster_redis-master-2_1",
                "EndpointID": "bfbbca733440a0c4b5fa784f51b431b90502dd3f450beb5b0fa54a742045119b",
                "MacAddress": "02:42:0a:00:00:03",
                "IPv4Address": "10.0.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "redisnet",
            "com.docker.compose.project": "docker-compose-redis-cluster",
            "com.docker.compose.version": "1.22.0"
        }
    }
]

1 个答案:

答案 0 :(得分:0)

根据完整的docker-compose.yaml文件(配置中的一些示例):

services:
  redis-master-1:
    build: ./
    ports:
      - 7000:7000
    networks:
      redisnet:
        ipv4_address: 10.0.0.2
    command: sh -c "redis-server /usr/local/redis.conf --port 7000 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000"

您可以通过redis上指定的端口访问任何localhost容器。

例如,对于上述redis-master-1容器,您需要使用localhost:7000才能到达容器,等等。