我正在尝试在kubernetes中构建一个Pod,该Pod从本地系统中将文件安装到Pod,类似于在docker-compose
文件中加载卷
我尝试了以下操作,试图将本地文件夹./test
和文件安装到/blah/
文件夹下的pod中。但是kubernetes抱怨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
答案 0 :(得分:3)
如果您只是想将文件或目录传递给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
然后,您可以通过在部署规范中添加volumes
和volumeMount
来挂载文件:
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
工作节点。
答案 1 :(得分:0)
您可以这样做
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
答案 2 :(得分:0)
诸如“服务”,“部署”之类的工作负载资源无法将卷装载到Pod中不存在的文件夹中。
确保在挂载之前已在窗格中创建文件夹。最好的方法是,如果您要创建自己的映像,则在docker文件中定义一个this,或者在pod的开头具有一个脚本来创建所需的文件夹。
答案 3 :(得分:0)
如果您想从主机添加本地文件,那么以下将起作用
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