Docker检查格式以检索特定网络的IP

时间:2017-01-19 09:16:19

标签: docker docker-swarm

我使用swarm模式,我想在特定网络上获取服务的VirtualIP:6yhxlmtq69zaojpy57xdvaeiw 结果应为:10.0.0.3/24

[root@managers vagrant]# docker service inspect tqy1id7zsg9s
[
    {
        "ID": "tqy1id7zsg9s7rftu8uu51g2l",
        "Version": {
            "Index": 72
        },
        "CreatedAt": "2017-01-19T07:56:13.30985464Z",
        "UpdatedAt": "2017-01-19T07:56:13.312923681Z",
        "Spec": {
            "Name": "spark-master",
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "fsoppelsa/spark-master:latest@sha256:7aee2f405b    546584b75bc1ec6ec3bfb5744de52788058c3455a1b43817d6af84",
                    "Labels": {
                        "spark-master": ""
                    },
                    "Env": [
                        "SPARK_MASTER_IP=0.0.0.0"
                    ],
                    "Mounts": [
                        {
                            "Type": "volume",
                            "Source": "spark",
                            "Target": "/data",
                            "VolumeOptions": {
                                "DriverConfig": {
                                    "Name": "local"
                                }
                            }
                        }
                    ],
                    "DNSConfig": {}
                },
                "Resources": {
                    "Limits": {},
                    "Reservations": {}
                },
                "RestartPolicy": {
                    "Condition": "any",
                    "MaxAttempts": 0
                },
                "Placement": {
                    "Constraints": [
                        "node.labels.type == sparkmaster"
                    ]
                },
                "Networks": [
                    {
                        "Target": "6yhxlmtq69zaojpy57xdvaeiw"
                    }
                ],
                "ForceUpdate": 0
            },
            "Mode": {
                "Replicated": {
                    "Replicas": 1
                }
            },
            "UpdateConfig": {
                "Parallelism": 1,
                "FailureAction": "pause",
                "MaxFailureRatio": 0
            },
            "Networks": [
                {
                    "Target": "spark"
                }
            ],
            "EndpointSpec": {
                "Mode": "vip",
                "Ports": [
                    {
                        "Protocol": "tcp",
                        "TargetPort": 8080,
                        "PublishedPort": 8080,
                        "PublishMode": "ingress"
                    }
                ]
            }
        },
        "Endpoint": {
            "Spec": {
                "Mode": "vip",
                "Ports": [
                    {
                        "Protocol": "tcp",
                        "TargetPort": 8080,
                        "PublishedPort": 8080,
                        "PublishMode": "ingress"
                    }
                ]
            },
            "Ports": [
                {
                    "Protocol": "tcp",
                    "TargetPort": 8080,
                    "PublishedPort": 8080,
                    "PublishMode": "ingress"
                }
            ],
            "VirtualIPs": [
                {
                    "NetworkID": "4w6z3pc3zspunw4n6199594ve",
                    "Addr": "10.255.0.9/16"
                },
                {
                    "NetworkID": "6yhxlmtq69zaojpy57xdvaeiw",
                    "Addr": "10.0.0.3/24"
                }
            ]
        },
        "UpdateStatus": {
            "StartedAt": "0001-01-01T00:00:00Z",
            "CompletedAt": "0001-01-01T00:00:00Z"
        }
    }
]

使用此命令我得到:

docker service inspect spark-master -f {{.Endpoint.VirtualIPs}}
[{4w6z3pc3zspunw4n6199594ve 10.255.0.9/16} {6yhxlmtq69zaojpy57xdvaeiw 10.0.0.3/24}]

如何指定正确的格式来获取6yhxlmtq69zaojpy57xdvaeiw网络的VirtualIp?

1 个答案:

答案 0 :(得分:0)

docker service inspect spark-master -f '{{range $i, $value := .Endpoint.VirtualIPs}} {{if eq $value.NetworkID "6yhxlmtq69zaojpy57xdvaeiw" }}{{$value.Addr}}{{end}}{{end}}'