PermissionError:[Errno 1]使用os.symlink时不允许执行以下操作:“file.txt”>“symlink.txt”

2024-04-27 00:11:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用helm charts在本地集群minikube中部署kubernetes应用程序。我能够挂载/home/$USER/log目录,并通过使用shell命令在挂载目录中创建和修改文件进行验证

#touch /log/a
# ls
a  delete.cpp  dm

但当我使用python创建符号链接时,它失败了

>>> import os
>>> os.symlink("delete.cpp", "b")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 1] Operation not permitted: 'delete.cpp' -> 'b'

知道为什么symlink不工作吗
我可以在不同的目录中使用相同的代码
要在我使用的minikube中装载主机目录

minikube mount ~/log:/log 

我的部署脚本如下所示

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  template: 
    metadata:
      labels:
        app: my-app
    spec:
      volumes:
      - name: log-dir
        hostPath:
            path: /log
      containers:
      - name: my-app
        image: my-image
        imagePullPolicy: never #It's local image
        volumeMounts:
        - name: log-dir
          mountPath: /log
        command: [ "/bin/bash", "-ce", "./my_app_executing_symlink" ]

Tags: nameimage目录logapposmy部署
2条回答

如果您使用的是minikube,那么可以使用^{} persistent volumethatsupports hostPath在单节点集群上进行开发和测试

用法示例:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-volume
  labels:
    type: local
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/log"
 -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: example-volume
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
 -
apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  volumes:
  - name: pv0001
    persistentVolumeClaim:
      claimName: example-volume
  containers:
    - name: my-app
      image: alpine
      command: [ "/bin/sh", "-c", "sleep 10000" ]
      volumeMounts:
        - name: pv0001
          mountPath: /log

成功部署后,您将能够在/log目录中创建符号链接:

$ kubectl exec -it my-app   /bin/sh
/log # touch a
/log # ln -s a pd
-rw-r r     1 root     root             0 Nov 25 17:49 a
lrwxrwxrwx    1 root     root             1 Nov 25 17:49 pd -> a

如文件中所述:

minikube is configured to persist files stored under the following directories, which are made in the Minikube VM (or on your localhost if running on bare metal). You may lose data from other directories on reboots.

  • /资料
  • /var/lib/minikube
  • /var/lib/docker
  • /tmp/hostpath_pv
  • /tmp/主机路径供应器

According to the Linux manpage on ^{}, you'd get that error when the file system doesn't support symlinks.

   EPERM  The filesystem containing linkpath does not support the
          creation of symbolic links.

minikube挂载的情况下,这听起来当然是可能的

相关问题 更多 >