ZBLOG

k8s如何在pod重启的时候通过fstab把brick mount起来的

  1. 在k8s的yaml文件中,添加volumes和volumeMounts字段。
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
    - name: example-container
      image: nginx
      volumeMounts:
        - name: brick-mount
          mountPath: /mnt/brick
  volumes:
    - name: brick-mount
      hostPath:
        path: /mnt/brick # 这里是挂载的本地路径,可以根据实际情况修改。
  1. 在host上创建一个mount脚本

在宿主机上创建一个脚本文件(例如:/usr/local/bin/mount_brick.sh),用来mount指定的brick。这个脚本要确保能够正确地mount指定的brick,并且以root用户运行。

#!/bin/bash

set -e

# 挂载指定的brick到/mnt/brick目录下。
mount /dev/sdb1 /mnt/brick 
  1. 将脚本文件复制到pod中

使用ConfigMap将脚本文件复制到Pod中。

apiVersion: v1
kind: ConfigMap
metadata:
  name: mount-script-configmap
data:
  mount_brick.sh : |
    #!/bin/bash
    
    set -e
    
    # 挂载指定的brick到/mnt/brick目录下。
    mount /dev/sdb1 /mnt/brick 

---
apiVersion: v1
kind: Pod 
metadata:
  name: example-pod 
spec:
  containers:
    - name: example-container 
      image: nginx 
      volumeMounts:
        - name: brick-mount 
          mountPath: /mnt/brick
  volumes:
    - name: brick-mount 
      hostPath:
        path: /mnt/brick
    - name: script-volume
      configMap:
        name: mount-script-configmap
  initContainers:
    - name: init-mounter-container
      image: alpine # 这里可以使用任何支持mount的容器镜像。
      command: ["/bin/sh"]
      args: ["-c", "cp /scripts/mount_brick.sh /mnt"]
      volumeMounts:
        - name: script-volume
          mountPath: "/scripts"
        - name : brick-mount 
          mountPath : /mnt/brick # 这里需要和Pod中的volumeMounts保持一致。
  1. 在Pod重启时运行脚本

在pod中添加initContainers字段,并在其中运行之前复制到pod中的脚本。

apiVersion: v1
kind: Pod 
metadata:
  name: example-pod 
spec:
  containers:
    - name: example-container 
      image: nginx 
      volumeMounts:
        - name : brick-mount 
          mountPath : /mnt/brick # 这里需要和Pod中的volumes保持一致。
  volumes:
    - name : brick-mount 
      hostPath :
        path : /mnt/brick # 这里是挂载的本地路径,可以根据实际情况修改。
    - name : script-volume
      configMap :
        name : mount-script-configmap    
  initContainers :
    - name : init-mounter-container
      image : alpine # 这里可以使用任何支持mount的容器镜像。
      command : ["/bin/sh"]
      args : ["-c", "cp /scripts/mount_brick.sh /mnt && chmod +x /mnt/mount_brick.sh"]
      volumeMounts :
        - name : script-volume
          mountPath : "/scripts"
        - name : brick-mount 
          mountPath : /mnt/brick # 这里需要和Pod中的volumes保持一致。
  dnsPolicy: Default
  restartPolicy: Always

这样,当Pod重启时,initContainers会运行脚本,并将指定的brick挂载到/mnt/brick目录下。如果需要卸载brick,可以在脚本中添加umount命令。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5493

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?