将本地ssd组合到kubernetes中的节点

时间:2019-02-18 16:47:19

标签: kubernetes google-kubernetes-engine

我的容器所需的数据太大,无法放在一个本地SSD上。我还需要从容器中将SSD作为一个文件系统访问。因此,我需要附加多个。如何合并它们并使它们在容器中显示为一个文件系统?该链接分享了如何将SSD https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/local-ssd挂载到挂载路径。我不确定您将如何合并多个。

3 个答案:

答案 0 :(得分:2)

警告 这是实验性的,不打算用于没有 知道您在做什么,并且仅在gke 1.16.x版上进行了测试。

该方法包括daemonset,使用configmapnsenter(带有等待技巧)用于主机名称空间和特权访问,以便您可以管理设备。专门针对GKE本地SSD,我们可以卸载这些设备,然后对其进行raid0。 InitContainer适用于肮脏的工作,因为这种类型的任务对于您需要标记为完成,然后终止特权容器访问(甚至Pod)的事情来说似乎最为明显。就是这样。

该示例假定有16个SSD,但是,您需要根据需要调整硬编码值。另外,请确保您的操作系统映像要求,我使用Ubuntu。另外,请确保您使用的GKE版本在sd [b]

处开始本地ssd。

ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: local-ssds-setup
  namespace: search
data:
    setup.sh: |
        #!/bin/bash
        # returns exit codes: 0 = found, 1 = not found
        isMounted() { findmnt -rno SOURCE,TARGET "$1" >/dev/null;} #path or device

        # existing disks & mounts
        SSDS=(/dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi /dev/sdj /dev/sdk /dev/sdl /dev/sdm /dev/sdn /dev/sdo /dev/sdp /dev/sdq)

        # install mdadm utility
        apt-get -y update && apt-get -y install mdadm --no-install-recommends
        apt-get autoremove

        # OPTIONAL: determine what to do with existing, I wipe it here
        if [ -b "/dev/md0" ]
            then
            echo "raid array already created"

            if isMounted "/dev/md0"; then
                echo "already mounted - unmounting"
                umount /dev/md0 &> /dev/null || echo "soft error - assumed device was mounted"
            fi

            mdadm --stop /dev/md0
            mdadm --zero-superblock "${SSDS[@]}"                
        fi

        # unmount disks from host filesystem
        for i in {0..15}
        do 
            umount "${SSDS[i]}" &> /dev/null || echo "${SSDS[i]} already unmounted"
        done

        if isMounted "/dev/sdb";
            then 
            echo ""
            echo "unmount failure - prevent raid0" 1>&2
            exit 1
        fi

        # raid0 array
        yes | mdadm --create /dev/md0 --force --level=0 --raid-devices=16 "${SSDS[@]}"

        echo "raid array created"

        # format 
        mkfs.ext4 -F /dev/md0

        # mount, change /mnt/ssd-array to whatever
        mkdir -p /mnt/ssd-array
        mount /dev/md0 /mnt/ssd-array
        chmod a+w /mnt/ssd-array

    wait.sh: |
        #!/bin/bash
        while sudo fuser /var/{lib/{dpkg,apt/lists},cache/apt/archives}/lock >/dev/null 2>&1; do sleep 1; done

DeamonSet吊舱规范

spec:
      hostPID: true
      nodeSelector:
        cloud.google.com/gke-local-ssd: "true"
      volumes:
      - name: setup-script
        configMap:
          name: local-ssds-setup
      - name: host-mount
        hostPath:
          path: /tmp/setup
      initContainers:
      - name: local-ssds-init
        image: marketplace.gcr.io/google/ubuntu1804
        securityContext:
          privileged: true
        volumeMounts:
        - name: setup-script
          mountPath: /tmp
        - name: host-mount
          mountPath: /host
        command:
          - /bin/bash
          - -c
          - |
            set -e
            set -x

            # Copy setup script to the host
            cp /tmp/setup.sh /host

            # Copy wait script to the host 
            cp /tmp/wait.sh /host

            # Wait for updates to complete
            /usr/bin/nsenter -m/proc/1/ns/mnt -- chmod u+x /tmp/setup/wait.sh

            # Give execute priv to script
            /usr/bin/nsenter -m/proc/1/ns/mnt -- chmod u+x /tmp/setup/setup.sh

            # Wait for Node updates to complete
            /usr/bin/nsenter -m/proc/1/ns/mnt /tmp/setup/wait.sh

            # If the /tmp folder is mounted on the host then it can run the script
            /usr/bin/nsenter -m/proc/1/ns/mnt /tmp/setup/setup.sh
      containers:
      - image: "gcr.io/google-containers/pause:2.0"
        name: pause

答案 1 :(得分:1)

对于高性能用例,请使用 Ephemeral storage on local SSDs GKE 功能。所有本地 SSD 都将配置为(条带化的)raid0 阵列并安装到 pod 中。

快速总结:

  1. 使用以下选项创建节点池或集群:--ephemeral-storage local-ssd-count=X
  2. 使用 cloud.google.com/gke-ephemeral-storage-local-ssd 安排节点。
  3. 添加一个 emptyDir 卷。
  4. 使用 volumeMounts 安装它。

这是我如何将它与 DaemonSet 一起使用:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      nodeSelector:
        cloud.google.com/gke-ephemeral-storage-local-ssd: "true"
      volumes:
      - name: localssd
        emptyDir: {}
      containers:
        - name: myapp
          image: <IMAGE>
          volumeMounts:
            - mountPath: /scratch
              name: localssd

答案 2 :(得分:0)

您可以使用DaemonSet yaml文件来部署Pod,该Pod将在启动时运行,假设已经创建了具有2个本地SSD的集群(此Pod将负责创建Raid0磁盘)

kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
  name: ssd-startup-script
  labels:
    app: ssd-startup-script
spec:
  template:
    metadata:
      labels:
        app: ssd-startup-script
   spec:
     hostPID: true
     containers:
     - name: ssd-startup-script
    image: gcr.io/google-containers/startup-script:v1
    imagePullPolicy: Always
    securityContext:
      privileged: true
    env:
    - name: STARTUP_SCRIPT
      value: |
        #!/bin/bash
        sudo curl -s https://get.docker.com/ | sh
        echo Done

在上例中可以访问磁盘阵列的Pod是“ / mnt / disks / ssd-array”

apiVersion: v1
kind: Pod
metadata:
 name: test-pod
spec:
 containers:
- name: test-container
  image: ubuntu
  volumeMounts:
  - mountPath: /mnt/disks/ssd-array
   name: ssd-array
  args:
  - sleep
  - "1000"
nodeSelector:
 cloud.google.com/gke-local-ssd: "true"
tolerations:
- key: "local-ssd"
 operator: "Exists"
 effect: "NoSchedule"
volumes:
- name: ssd-array
  hostPath:
   path: /mnt/disks/ssd-array

在部署测试盒之后,将SSH从您的云外壳或任何实例连接到该盒。

然后运行:

  kubectl exec -it test-pod -- /bin/bash

之后,您应该可以在ssd-array磁盘中看到创建的文件。

cat test-file.txt