使用k8s部署docker,如何挂载目录?

针对这个问题,分两步解决:
1.k8s指定节点部署,目的是为了让docker容器始终部署在一台宿主机上。
  后面我挂载目录的话,只要挂载到这台宿主机上就可以了。
2.在yaml文件中配置挂载目录。
  一开始我想的是在Dockerfile文件中配置,后来改了思路,认为在yaml文件中比较合理。至于Dockerfile中行不行,我没做测试。

我的这个思路可能不专业,但解决了我的问题。不喜勿喷哈。下面我贴出来yaml文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: xxxxx-mes-backgroundtask-qa
  labels:
    app: xxxxx-mes-backgroundtask-qa
spec:
  replicas: 1
  minReadySeconds: 60
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  selector:
    matchLabels:
      app: xxxxx-mes-backgroundtask-qa
  template:
    metadata:
      labels:
        app: xxxxx-mes-backgroundtask-qa
    spec:
      nodeName: node2
      containers:
      - name: xxxxx-mes-backgroundtask-qa
        image: registry.cn-hangzhou.aliyuncs.com/xxxxxxx/xxxxx.mes.backgroundtask.qa:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 21021
        volumeMounts:
        - mountPath: /app/App_Data
          name: logs
      volumes:
      - name: logs
        hostPath:
          path: /soft/logs
---
apiVersion: v1
kind: Service
metadata:
  name: xxxxx-mes-backgroundtask-qa-service
spec:
  type: NodePort
  ports:
    - port: 21021
      targetPort: 21021
      nodePort: 30054
  selector:
    app: xxxxx-mes-backgroundtask-qa

解说:

1.指定nodeName,就可以实现docker始终部署在一台宿主机上。nodeName可以是宿主机的IP,也可以是宿主机的节点名称

2.配置volumeMounts,指定docker容器需要挂载的目录和别名。/app/App_Data为docker容器中的目录

3.配置volumes,指定在宿主机上挂载的目录,并指定name为volumeMounts中设置的name。/soft/logs为宿主机中的目录

参考网址:https://blog.csdn.net/zhangge3663/article/details/107838613。这篇文章还讲了另外一种方式:网络文件系统nfs,可以看下。

posted @ 2022-07-12 14:27  屌丝大叔的笔记  阅读(4582)  评论(0编辑  收藏  举报