如何在 kubernetes 中浏览持久卷并使用 GUI 编辑文件?

Nom*_*nom 6 kubernetes persistent-volumes kubernetes-helm

I have a Bitnami WordPress helm release, I need to browse the files and edit some from the persistent volumes it created. Ideally I'd be able to browse the files using a GUI (like Nautilus/GNOME files) and edit the files using VS Code for ease of use.

Should I edit the files from within a mounted container? How can I do that? Are there other ways to go about this?

ane*_*yte 15

首先我应该提到以下所有内容都是针对开发环境的。不要考虑在生产中对容器应用程序进行手动更改。对于生产,您应该确保自动应用所有必要的更改。

从远程计算机复制文件并返回

# Copy TO local machine
kubectl cp <namespace_name>/<pod_name>:<remote_file_path> <local_file_path> -c <container_name>

# Copy FROM local machine
kubectl cp <local_file_path> <namespace_name>/<pod_name>:<remote_file_path> -c <container_name>

# More examples
kubectl cp --help
Run Code Online (Sandbox Code Playgroud)

-c <container_name>如果 pod 中只有一个容器,则可以省略。

如果您不确定使用哪个<namespace_name><pod_name>,您可以尝试使用 来确定kubectl get pods --all-namespaces

如果您不确定应该输入哪个远程路径,您可以尝试在远程容器中使用lsand :pwd

kubectl exec -n <namespace_name> <pod_name> -c <container_name> ls <remote_path>` 
# e.g.
kubectl exec -n my_namespace my_pod -c wordpress ls /var/www
kubectl exec -n my_namespace my_pod -c wordpress pwd
Run Code Online (Sandbox Code Playgroud)

使用控制台编辑器远程编辑文件

如果您的容器映像中有 shell 并且主进程由root用户启动,则可以使用。使用以下命令启动控制台会话:

kubectl exec -it -n <namespace_name> <pod_name> -c <container_name> sh
Run Code Online (Sandbox Code Playgroud)

安装一些控制台文本编辑器(vim、nano 等)并使用它。如何安装取决于使用什么 Linux 作为映像的基础,您可以使用cat /etc/os-release.

使用 GUI 进行编辑

我认为使用 GUI 的一种方法是jupyter在应用程序旁边启动一个基于 Web 的文本编辑器(如 )。您需要修改 Deployment(或 StatefulSet、DaemonSet 等),然后才能启动新容器的代理。

jupyter以下是如何添加到 pod 的示例:

  1. 通过添加 jupyter 容器修改部署:
- name: jupyter
  image: jupyter/base-notebook
  securityContext:
    runAsUser:  # insert UID that uses your app
  args:
  - jupyter
  - notebook
  - --ip=0.0.0.0
  - --allow-root # if the UID is 0
  workingDir: /data
  ports:
  - containerPort: 8888
  volumeMounts:
    - mountPath: /data
      name: # insert name of the volume with files you want to modify
Run Code Online (Sandbox Code Playgroud)
  1. 用于kubectl port-forward与 pod 建立连接:
kubectl port-forward -n <namespace> <pod_name> 8888:8888
Run Code Online (Sandbox Code Playgroud)

之后就可以访问 jupyter 了localhost:8888。请参阅 jupyter 日志获取访问密钥 ( kubectl logs -n <namespace> <pod_name> -c jupyter)。