在AWS ECS中,docker容器的主机名是容器运行时id。有没有办法在给定容器运行时间id的情况下,获取容器运行时所在的任务ARN?
发布于 2021-06-19 17:19:02
没有任何特定的AWS命令可以从容器运行时id获取任务ARN。但这可以使用aws ecs的list-tasks和describe-tasks命令来实现,前提是您之前已经知道集群和服务名称。
下面是你如何实现这一点的想法:
tasksList=`aws ecs list-tasks --cluster mycluster --service-name myservice | awk '/mycluster/ {print}' | sed 's/,$//' | awk '{ sub(/^[ \t]+/, ""); print }' | awk '{printf "%s ",$0} END {print ""}'`
taskDesc="aws ecs describe-tasks --cluster mycluster --tasks ${tasksList}"
eval $taskDesc | awk '/taskArn|runtimeId/ {print $0}' | awk '{ sub(/^[ \t]+/, ""); print }' | awk '!visited[$0]++' | awk '/taskArn/ {$0=$0"->"} 1'这将为您提供类似以下内容的输出:
"taskArn": "arn:aws:ecs:<aws_region>:<account_id>:task/mycluster/043de9ab06bb41d29e97576f1f1d1d33",->
"runtimeId": "191c90bae67844124ff2d079f4de997dc8cb9e3c93cd451d931c806283ea527d",
"runtimeId": "61e6fa6e1cbb5039e1c4df31d5c522b9439c22fbeff3c9cc1fb4429ffbb5a94d",
"taskArn": "arn:aws:ecs:<aws_region>:<account_id>:task/mycluster/14478901e8364466b8fd8236d6a66c5e",->
"runtimeId": "b679c5139f019f526c4301c8cc511723abd6aed3fa8cf9c397147d33275a860c",
"runtimeId": "0e727f660616df970b2cf767ea389bee97cd748ea9e09266cb5ee651ad2d2971",
"runtimeId": "ee7fb30715d9ff325eebcfacdde978179f07b1e3bf91a0dac92abd3b54970307",
"taskArn": "arn:aws:ecs:<aws_region>:<account_id>:task/mycluster/23d037af17f54a59afe50f810afdecf0",->
"runtimeId": "67d4ebea82e6e6622171816e1aef53489070aea5b03d5942e8e41c2dc4f49fd3",在每个taskArn行下面,有多个runtimeId行,指定在该任务下运行的容器的容器运行时ids。
希望这能有所帮助。祝你脚本愉快。
https://stackoverflow.com/questions/68045264
复制相似问题