2017-01-19 30 views
0

我用群模式,我想获得的服务的VirtualIP在特定网络上:6yhxlmtq69zaojpy57xdvaeiw 结果应该是:10.0.0.3/24泊坞检查格式中检索特定网络的IP

[[email protected] 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:[email protected]: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正确的格式?

+0

看到我的答案在http://stackoverflow.com/questions/30342796/how-to-get-env-variable-when-doing-docker-inspect/30353018#30353018你应该能够提取相关部分 – user2915097

+0

什么给docker服务检查spark-master -f {{(index(.Endpoint.VirtualIPs)0).4w6z3pc3zspunw4n6199594ve}} – user2915097

+0

我误读了,更好的尝试'docker service inspect spark-master -f {{ (.Endpoint.VirtualIPs)1).6yhxlmtq69zaojpy57xdvaeiw}}' – user2915097

回答

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

和我玩这个我可以得到更多补偿lex:'''docker service inspect spark-master -f'{{$ net:=(index.Spec.TaskTemplate.Networks 0).Target}} {{range $ i,$ value:= .Endpoint.VirtualIPs}} {{if eq $ value.NetworkID $ net}} {{$ value.Addr}} {{end}} {{end}}'''' – rolele