You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure云服务中查看Kubernetes持久化卷及容器输出目录内容?

解决方案:访问Azure容器集群中容器的/src/data/目录内容

我来帮你解决这个问题,针对你提到的两种方案,我整理了具体的实现和查看方法:

一、使用Persistent Volume(PV/PVC)时查看内容的方法

1. 直接进入运行中的Pod查看

这是最直接的方式,不需要额外资源:

  • 先找到你的应用Pod名称:
    kubectl get pods
    
  • 进入Pod的容器内部,查看目标目录:
    # 如果容器有bash环境
    kubectl exec -it <你的Pod名称> -- /bin/bash
    # 进入后执行命令查看
    ls /src/data/
    cat /src/data/<具体文件名>
    
    # 如果容器没有bash,换成sh试试
    kubectl exec -it <你的Pod名称> -- /bin/sh
    

2. 用临时Pod挂载同一个PVC查看

如果不想影响运行中的应用,或者原Pod无法进入,可以创建一个临时Pod挂载相同的PVC:

  • 先获取你的PVC名称:
    kubectl get pvc
    
  • 创建临时Pod配置文件(比如temp-viewer.yaml):
    apiVersion: v1
    kind: Pod
    metadata:
      name: temp-data-viewer
    spec:
      containers:
      - name: busybox
        image: busybox:latest
        command: ["sleep", "3600"] # 让Pod保持运行1小时
        volumeMounts:
        - name: data-volume
          mountPath: /src/data
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: <你的PVC名称>
    
  • 部署临时Pod:
    kubectl apply -f temp-viewer.yaml
    
  • 进入临时Pod查看内容:
    kubectl exec -it temp-data-viewer -- /bin/sh
    ls /src/data/
    
  • 用完记得删除临时Pod,避免资源浪费:
    kubectl delete pod temp-data-viewer
    

3. 通过Azure Portal关联磁盘查看(针对Azure Disk类型PV)

如果你的PV是Azure托管磁盘,可以通过临时VM挂载查看:

  • 在Azure Portal找到PVC对应的Azure Disk资源(从PVC详情页的“关联资源”能找到)
  • 创建一个临时Azure VM,将该磁盘挂载到VM上
  • 登录VM后,打开文件管理器或终端就能查看磁盘里的/src/data对应内容
  • 查看完成后,记得卸载磁盘并删除临时VM,避免产生不必要的费用

二、使用Azure Blob Storage挂载的可行实现方法

你之前没找到可行方法,其实可以通过Azure文件共享(基于Blob存储)挂载到容器,这样既能让容器写入数据,又能方便地查看内容:

1. 准备Azure存储资源

  • 在Azure Portal创建一个存储账户(推荐Standard_LRS冗余类型,成本较低)
  • 在存储账户内创建一个文件共享(注意是文件共享,不是普通的Blob容器,azurefile驱动支持这种类型)

2. 创建Kubernetes凭证Secret

获取存储账户的访问密钥(在存储账户的“访问密钥”页面复制),然后创建Secret:

kubectl create secret generic azure-storage-secret \
  --from-literal=azurestorageaccountname=<你的存储账户名> \
  --from-literal=azurestorageaccountkey=<你的存储账户密钥>

3. 挂载到你的应用Pod/Deployment

方式一:通过PVC挂载(推荐,更灵活)

创建PVC配置文件(azure-blob-pvc.yaml):

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: azure-blob-pvc
spec:
  accessModes:
    - ReadWriteMany # 支持多个Pod同时读写
  storageClassName: azurefile
  resources:
    requests:
      storage: 10Gi # 这里的容量是声明值,实际由存储账户配额决定

部署PVC:

kubectl apply -f azure-blob-pvc.yaml

然后在你的Deployment中挂载这个PVC:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: your-app-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: your-app
  template:
    metadata:
      labels:
        app: your-app
    spec:
      containers:
      - name: your-app-container
        image: <你的容器镜像地址>
        volumeMounts:
        - name: data-volume
          mountPath: /src/data # 和容器内的目录对应
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: azure-blob-pvc

方式二:直接在Pod中挂载(跳过PVC)

如果不需要PVC的灵活性,也可以直接在Pod配置中挂载:

apiVersion: v1
kind: Pod
metadata:
  name: your-app-pod
spec:
  containers:
  - name: your-app-container
    image: <你的容器镜像地址>
    volumeMounts:
    - name: data-volume
      mountPath: /src/data
  volumes:
  - name: data-volume
    azureFile:
      secretName: azure-storage-secret
      shareName: <你的文件共享名称>
      readOnly: false # 允许容器写入数据

4. 查看Blob存储中的内容

  • 直接在Azure Portal进入存储账户的“文件共享”,就能看到容器写入的所有文件,支持在线预览、下载
  • 也可以使用Azure Storage Explorer工具,连接到你的存储账户,可视化查看和管理文件共享里的内容

内容的提问来源于stack exchange,提问作者zedong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:31:06