Kubernetes将本地文件添加到Pod

lo8azlld  于 2022-10-23  发布在  Kubernetes
关注(0)|答案(5)|浏览(651)

我正在尝试在Kubernetes中构建一个Pod,将文件从我的本地系统挂载到Pod,方法类似于挂载docker-compose文件中的卷
我尝试了以下操作,试图将本地文件夹./test和文件挂载到/blah/文件夹下的Pod中。然而,库伯内斯抱怨说,MountVolume.SetUp failed for volume "config-volume" : hostPath type check failed: ./test/ is not a directory
这是我的yaml文件。我是不是遗漏了什么?

kind: Service
metadata:
  name: vol-test
  labels:
    app: vol-test
spec:
  type: NodePort
  ports:
    - port: 8200
      nodePort: 30008
  selector:
    app: vol-test

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: vol-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: vol-test
  template:
    metadata:
      labels:
        app: vol-test
    spec:
      containers:
        - name: vol-test
          image: nginx
          imagePullPolicy: "IfNotPresent"
          volumeMounts:
            - name: config-volume
              mountPath: /blah/
          ports:
            - containerPort: 8200
      volumes:
        - name: config-volume
          hostPath:
            path: ./test/
            type: Directory
vkc1a9a2

vkc1a9a21#

如果您只想将文件或目录传递到Pod以读取配置值(我从您选择的卷挂载config-volume中假定了这一点),并且不需要更新文件/目录,那么您只需将文件放在如下所示的ConfigMap中:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-router-config
data:
  nginx.conf: |
    worker_processes 2;
    user nginx;
    events {
        worker_connections  1024;
    }

    http {
        include       mime.types;
        charset       utf-8;
        client_max_body_size 8M;

        server {
            server_name _;
            listen 80 default_server;

            location / {
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Proto https;
                proxy_pass http://drupal:80/ ;
                proxy_redirect default;
            }

            location /api/ {
                proxy_pass http://api-gateway:8080/ ;
                proxy_redirect default;
            }
        }
    }

或者,您可以从运行kubectl命令并执行的位置导入文件内容(假设文件名为nginx.conf):

kubectl create configmap nginx-router-config --from-file=nginx.conf

然后,您可以通过将volumesvolumeMount添加到部署规范来装载文件:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-router
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-router-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
        - name: nginx-router-config
          configMap:
            name: nginx-router-config
            items:
              - key: nginx.conf
                path: nginx.conf

如果您实际上希望拥有对文件的读写访问权限,那么您可以按照另一个答案的建议使用PersistentVolume和PersistentVolumeClaim,尽管如果您有多个工作节点,我不建议使用hostPath

xzv2uavs

xzv2uavs2#

你可以这样做

kind: PersistentVolume
apiVersion: v1
metadata:
  name: config-volume-pv
  labels:
    type: local
spec:
  storageClassName: generic
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/path/to/volume"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  name: config-volume-pvc
spec:
  storageClassName: generic
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

---
kind: Service
metadata:
  name: vol-test
  labels:
    app: vol-test
spec:
  type: NodePort
  ports:
    - port: 8200
      nodePort: 30008
  selector:
    app: vol-test

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: vol-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: vol-test
  template:
    metadata:
      labels:
        app: vol-test
    spec:
      containers:
        - name: vol-test
          image: nginx
          imagePullPolicy: "IfNotPresent"
          volumeMounts:
          - mountPath: /blah/
            name: ng-data
          ports:
            - containerPort: 8200
      volumes:
      - name: ng-data
        persistentVolumeClaim:
          claimName: config-volume-pvc
cvxl0en2

cvxl0en23#

如果您想要从主机添加本地文件,则可以执行以下操作

apiVersion: v1
kind: Pod
metadata:
  name: test-webserver
spec:
  containers:
  - name: test-webserver
    image: k8s.gcr.io/test-webserver:latest
    volumeMounts:
    - mountPath: /var/local/aaa/1.txt
      name: myfile
      readOnly: true
  volumes:
  - name: myfile
    hostPath:
      path: /var/local/aaa/1.txt
      type: File
pwuypxnk

pwuypxnk4#

服务、部署等工作负载资源无法将卷装载到Pod中不存在的文件夹。
确保在挂载Pod之前已在Pod中创建了该文件夹。如果您正在创建自己的图像,最好的方法是在docker文件中定义This,或者使用脚本在Pod的开头创建所需的文件夹。

ikfrs5lh

ikfrs5lh5#

kubectl cp /my/local/file namespace/podname:/tmp/file

相关问题