Skip to content

k8s 运行分布式 tensorflow 实验 #5

@vienlee

Description

@vienlee

k8s 运行分布式 tensorflow 实验

实验基于 @wangkuiyi 给出的 GitHub 地址 https://github.com/amygdala/tensorflow-workshop/tree/master/workshop_sections/distributed_tensorflow

我们目标需要在 k8s 上运行 tensorflow + ceph, 先通过这个实验流程来熟悉 tensorflow 在 k8s 上可行性和流程, 后续使用 ceph 存储来代替 NFS. 下面会记录实验流程和注意事项.

准备工作

  • kubectl 能够正确连接 k8s 集群
  • Clone 这个仓库(amygdala/tensorflow-workshop) 到本地

Step1: 运行 nfs_server 在 k8s 上

项目文档中需要我们先创建一个 GCE Persistent Disk, 这里我们使用 k8s 集群的 Node 空间作为 NFS Server 的磁盘

# 测试 kubectl 和 k8s 集群连接正常
kubectl get node
kubectl get pod --namespace=kube-system

# 进入下面指定目录, 我克隆仓库在 /opt 
cd /opt/tensorflow-workshop-master/workshop_sections/distributed_tensorflow/k8s-configs

# 编辑 nfs-server-local.yaml, 删除 PersistentVolume 和 PersistentVolumeClaim 配置块, 因为我们没有使用 GCE 的磁盘
vim nfs-server-local.yaml
--------
kind: Service
apiVersion: v1
metadata:
  name: tf-nfs
spec:
  ports:
    - name: nfs
      port: 2049
    - name: mountd
      port: 20048
    - name: rpcbind
      port: 111
  selector:
    role: tf-nfs

---
apiVersion: v1
kind: ReplicationController
metadata:
  name: tf-nfs
spec:
  replicas: 1
  selector:
    role: tf-nfs
  template:
    metadata:
      labels:
        role: tf-nfs
    spec:
      containers:
      - name: tf-nfs
        image: gcr.io/google-samples/nfs-server:1.1
        ports:
          - name: nfs
            containerPort: 2049
          - name: mountd
            containerPort: 20048
          - name: rpcbind
            containerPort: 111
        securityContext:
          privileged: true
        volumeMounts:
          - mountPath: /exports
            name: mypvc
      volumes:
        - name: mypvc
          hostPath:
            path: /var/nfs-server
--------
# 创建 nfs-server
kubectl create -f nfs-server-local.yaml
# 查看 pod 创建过程日志, 如有报错, 会在里边输出
kubectl describe pod tf-nfs

Step2: 创建 nfs-pvc(PersistentVolumeClaim)

持久卷(PersistentVolumeClaim)持久化的存储(诸如gcePersistentiscsi卷)而且还无须知道特定云环境的细节,在pod中使用persistentVolumeClaim就可以挂载。

关于PersistentVolumeClaim的细节https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/user-guide/persistent-volumes.md

创建 nfs 的持久卷

# 获得上面 tf-nfs 的 cluster ip 地址
kubectl get service tf-nfs
# 修改 nfs-pvc.yaml 中 server 地址为 上面的获得 
vim nfs-pvc.yaml
--------
server: 10.3.0.37
# 创建 pv 和 pvc
kubectl create -f nfs-pvc.yaml

注意: 使用 NFS, 需要为集群中 Node 装上 nfs client, 如果是 coreos , 打开 rpc-statd.service 服务即可.

Step3: 执行导入数据 Job

按部就班, 确保前面的流程都没有问题

kubectl create -f k8s-configs/load_data.yaml

Step4: 创建 Tensorboard Server

kubectl create -f k8s-configs/tensorboard.yaml
# 可以通过集群中任意 Node IP 加下面 NodePort 登录到 WEBUI(如: 10.10.10.203:31175)
kubectl describe services tensorboard
Name:           tensorboard
Namespace:      default
Labels:         <none>
Selector:       tensorflow=tensorboard
Type:           LoadBalancer
IP:         10.3.0.92
Port:           <unset> 80/TCP
NodePort:       <unset> 31175/TCP
Endpoints:      10.1.62.2:6006
Session Affinity:   None
No events.

Step5: 启动 Tensorflow Job

任务会拉取 gcr.io/google-samples/tf-worker-example:latest 这个镜像(740MB), 每个 Node 都会拉取, 速度很慢会导致任务失败.

这边解决方法是上传到 163 的 docker hub 上, 得到 hub.c.163.com/vienlee/tf-worker-example:latest , 替换 tf-cluster.yaml 中的 gcr.io/google-samples/tf-worker-example:latest 地址为hub.c.163.com/vienlee/tf-worker-example:latest 即可.

之所以没有用私有仓库, 因为当前没有搭建可用的私有仓库, 而且还需要支持 https(否则需要修改每个 Node 的 docker 配置增加私有仓库地址)

kubectl create -f tf-cluster.yaml
# 查看每个 pod 的状态
kubectl get pod
# 查看 pod 创建日志是否正常
kubectl describe pod <podname>

等待所有 Pod 创建成功, 状态为 Runing.

总结

通过实验证明 tensorflow 可以正常在 k8s 上运行, 后续流程我们可以提前创建 pv(使用 cephfs 存储) 和 pvc, 把训练数据导入到 pv 中. 然后编译我们自己的任务镜像, 编排 tf-cluster.yaml 执行任务.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions