学习笔记TF064:TensorFlow Kubernetes

AlphaGo,每个实验1000个节点,每个节点4个GPU,4000 GPU。Siri,每个实验2个节点,8个GPU。AI研究,依赖海量数据计算,离性能计算资源。更大集群运行模型,把周级训练时间缩短到天级小时级。Kubernetes,应用最广泛容器集群管理工具,分布式TensorFlow监控、调度生命周期管理。容器集群自动化部署、扩容、运维开源平台,提供任务调度、监控、失败重启。TensorFlow、Kubernetes都是谷歌公司开源。https://kubernetes.io/ 。谷歌云平台化解决方案。https://cloud.google.com/

分布式TensorFlow在Kubernetes运行。

部署、运行。安装Kubernetes。Minikube创建本地Kubernetes集群。Mac 先安装VirtualBox虚拟机。https://www.virtualbox.org/ 。Minikube Go语言编写,发布形式独立二进制文件,下载入到对应目录。命令:

curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.14.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

客户端kubectl,kubectl命令行与集群交互。安装:

curl -Lo kubectl http://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/darwin/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/

Minikube启动Kubernetes集群:

minikube start

Docker Hub最新镜像tensorflow/tensorflow(1.0版本) https://hub.docker.com/r/tens… 。配置参数服务器部署(deployment)文件,命名tf-ps-deployment.json:

{
  "apiVersion": "extensions/v1beta1",
  "kind": "Deployment",
  "metadata": {
    "name": "tensorflow-ps2"
  },
  "spec": {
    "replicas": 2,
    "template": {
      "metadata": {
        "labels": {
          "name": "tensorflow-ps2",
          "role": "ps"
        }
      }
    },
    "spec": {
      "containers": [
        {
          "name": "ps",
          "image": "tensorflow/tensorflow",
          "ports": [
            {
              "containerPort": 2222
            }
          ]
        }
      ]
    }
  }
}

配置参数服务器服务(Service)文件,命名tf-ps-service.json:

{
  "apiVersion": "v1",
  "kind": "Service",
  "spec": {
    "ports": [
      {
        "port": 2222,
        "targetPort": 2222
      }
    ],
    "selector": {
        "name": "tensorflow-ps2"
    }
  },
  "metadata": {
    "labels": {
      "name": "tensorflow",
      "role": "service"
    }
  },
  "name": "tensorflow-ps2-service"
}

配置计算服务器部置文件,命名tf-worker-deployment.json:

{
  "apiVersion": "extensions/v1beta1",
  "kind": "Deployment",
  "metadata": {
    "name": "tensorflow-worker2"
  },
  "spec": {
    "replicas": 2,
    "template": {
      "metadata": {
        "labels": {
          "name": "tensorflow-worker2",
          "role": "worker"
        }
      }
    },
    "spec": {
      "containers": [
        {
          "name": "worker",
          "image": "tensorflow/tensorflow",
          "ports": [
            {
              "containerPort": 2222
            }
          ]
        }
      ]
    }
  }
}

配置计算服务器服务文件,命名tf-worker-servic.json:

{
  "apiVersion": "v1",
  "kind": "Service",
  "spec": {
    "ports": [
      {
        "port": 2222,
        "targetPort": 2222
      }
    ],
    "selector": {
        "name": "tensorflow-worker2"
    }
  },
  "metadata": {
    "labels": {
      "name": "tensorflow-worker2",
      "role": "service"
    }
  },
  "name": "tensorflow-wk2-service"
}

执行命令:

kubectl create -f tf-ps-deployment.json
kubectl create -f tf-ps-service.json
kubectl create -f tf-worker-deployment.json
kubectl create -f tf-worker-service.json

运行 kubectl get pod,查看参数服务器和计算服务器全部创建完成。
进入每个服务器(Pod),部署mnist_replica.py文件。运行命令查看ps_host、worker_host IP地址。

kubectl describe service tensorflow-ps2-service
kubectl describe service tensorflow-wk2-service

打开4个终端,分别进入4个Pod。

kubectl exec -ti tensorflow-ps2-3073558082-3b08h /bin/bash
kubectl exec -ti tensorflow-ps2-3073558082-4x3j2 /bin/bash
kubectl exec -ti tensorflow-worker2-3070479207-k6z8f /bin/bash
kubectl exec -ti tensorflow-worker2-3070479207-6hvsk /bin/bash

mnist_replica.py部署到4个Pod。

curl https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/dist_test/python/mnist_replica.py -o mnist_replica.py

在参数服务器容器执行:

python mnist_replica.py --ps_hosts=172.17.0.16:2222,172.17.0.17:2222 --worker_bosts=172.17.0.3:2222,172.17.0.8:2222 --job_name="ps" --task_index=0
python mnist_replica.py --ps_hosts=172.17.0.16:2222,172.17.0.17:2222 --worker_bosts=172.17.0.3:2222,172.17.0.8:2222 --job_name="ps" --task_index=1

在计算服务器容器执行:

python mnist_replica.py --ps_hosts=172.17.0.16:2222,172.17.0.17:2222 --worker_bosts=172.17.0.3:2222,172.17.0.8:2222 --job_name="worker" --task_index=0
python mnist_replica.py --ps_hosts=172.17.0.16:2222,172.17.0.17:2222 --worker_bosts=172.17.0.3:2222,172.17.0.8:2222 --job_name="worker" --task_index=1

把需要执行的源代码入训练数据、测试数据放在持久卷(persistent volume),在多个Pod间共享,避免在每一个Pod分别部署。
TensorFlow GPU Docker集群部署,Nvidia提供nvidia-docker方式,利用宿主机GPU设备,映射到容器。https://github.com/NVIDIA/nvi…

训练好模型,打包制作环境独立镜像,方便测试人员部署一致环境,对不同版本模型做标记、比较不同模型准确率,从整体降低测试、部署上线工作复杂性。

参考资料:
《TensorFlow技术解析与实战》

欢迎推荐上海机器学习工作机会,我的微信:qingxingfengzi

    原文作者:利炳根
    原文地址: https://segmentfault.com/a/1190000011969646
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞