azure - 通过 Kubernetes 在 Docker 容器中挂载 Azure 文件存储

标签 azure docker kubernetes smb

我正在尝试使用此处找到的方法将我的 Azure 文件存储安装到容器:https://github.com/kubernetes/kubernetes/tree/master/examples/volumes/azure_file

创建 pod 后,我收到错误:“输出:安装错误:无法解析 [我的文件存储的编码名称].file.core.windows.net 的地址:未知错误”

我已确认我的文件存储资源和托管 Pod 的 VM 位于同一 Azure 位置(美国东部)。我可以使用上述错误中的相同地址在托管 Pod 的虚拟机上手动挂载此共享。我的容器中是否可能缺少 Git Hub 教程中未解释的某种配置?

我尝试在不指定卷的情况下创建容器,并且能够从容器内 ping 通文件存储的地址,因此我不确定无法解析地址错误来自何处。

最佳答案

我无法重现您的错误,但我们可以按照这些步骤将 Azure 文件共享挂载到 k8s 容器。
1.通过Azure新门户创建k8s。
2.SSH k8s master,创建secret,通过k8s文件创建secret:

在这个yaml文件中,我们应该在其中写入存储帐户和 key ,并且我们应该base64编码Azure存储帐户和 key ,如下所示:

root@k8s-master-3CC6E803-0:~# echo -n jasonshare321 | base64
amFzb25zaGFyZTMyMQ==
root@k8s-master-3CC6E803-0:~# echo -n Bnbh0fjykD+b/EveNoR/elOp118+0vmLsbQqVGC3H0W23mSfbH9WfV1A60Qw3CAZ70Tm4Wgpse1LEtgSJF27cQ== | base64
Qm5iaDBmanlrRCtiL0V2ZU5vUi9lbE9wMTE4KzB2bUxzYlFxVkdDM0gwVzIzbVNmYkg5V2ZWMUE2MFF3M0NBWjcwVG00V2dwc2UxTEV0Z1NKRjI3Y1E9PQ==

然后创建azure-secret:

root@k8s-master-3CC6E803-0:~# mkdir /azure_file
root@k8s-master-3CC6E803-0:~# cd /azure_file/
root@k8s-master-3CC6E803-0:/azure_file# touch azure-secret.yaml
root@k8s-master-3CC6E803-0:/azure_file# vi azure-secret.yaml 

这是 azure 的 secret :

root@k8s-master-3CC6E803-0:/azure_file# cat azure-secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: azure-secret
type: Opaque
data:
  azurestorageaccountname: amFzb25zaGFyZTMyMQ== 
  azurestorageaccountkey: Qm5iaDBmanlrRCtiL0V2ZU5vUi9lbE9wMTE4KzB2bUxzYlFxVkdDM0gwVzIzbVNmYkg5V2ZWMUE2MFF3M0NBWjcwVG00V2dwc2UxTEV0Z1NKRjI3Y1E9PQ==

然后使用 kubectl 创建 Secret,如下所示:

root@k8s-master-3CC6E803-0:/azure_file# kubectl create -f /azure_file/azure-secret.yaml 
secret "azure-secret" created
root@k8s-master-3CC6E803-0:/azure_file# kubectl get secret
NAME                  TYPE                                  DATA      AGE
azure-secret          Opaque                                2         11s
default-token-07cd5   kubernetes.io/service-account-token   3         35m

3.创建pod: 创建 azure.yaml:

root@k8s-master-3CC6E803-0:/azure_file# touch azure.yaml
root@k8s-master-3CC6E803-0:/azure_file# vi azure.yaml 
apiVersion: v1
kind: Pod
metadata:
 name: nginx
spec:
 containers:
  - image: nginx
    name: nginx
    volumeMounts:
      - name: azure
        mountPath: /mnt/azure
 volumes:
      - name: azure
        azureFile:
          secretName: azure-secret
          shareName: testfileshare
          readOnly: false

使用此文件创建 Pod:

root@k8s-master-3CC6E803-0:/azure_file# kubectl create -f /azure_file/azure.yaml 
pod "nginx" created
root@k8s-master-3CC6E803-0:/azure_file# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
nginx     1/1       Running   0          17s

现在,pod创建完成了,我们可以使用这个脚本来检查文件,如下所示:

root@k8s-master-3CC6E803-0:/azure_file# kubectl exec -it nginx bash
root@nginx:/# ls                                                                                                         
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@nginx:/# cd /mnt
root@nginx:/mnt# ls
azure
root@nginx:/mnt# cd azure
root@nginx:/mnt/azure# ls
root@nginx:/mnt/azure# df -Th
Filesystem                                          Type     Size  Used Avail Use% Mounted on
overlay                                             overlay   30G  3.3G   26G  12% /
tmpfs                                               tmpfs    1.7G     0  1.7G   0% /dev
tmpfs                                               tmpfs    1.7G     0  1.7G   0% /sys/fs/cgroup
/dev/sda1                                           ext4      30G  3.3G   26G  12% /etc/hosts
//jasonshare321.file.core.windows.net/testfileshare cifs      50G     0   50G   0% /mnt/azure
shm                                                 tmpfs     64M     0   64M   0% /dev/shm
tmpfs                                               tmpfs    1.7G   12K  1.7G   1% /run/secrets/kubernetes.io/serviceaccount

注意:
1.我们应该base64编码Azure存储帐户和 key 。
2.将正确的文件共享名称写入azure.yaml:enter image description here

关于azure - 通过 Kubernetes 在 Docker 容器中挂载 Azure 文件存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707379/

相关文章:

wcf - 将具有 Web 角色的 WCF 服务部署到非 Azure 托管

asp.net-mvc-3 - 专门为 Azure 编写代码?

docker - Kubernetes暴露多个Pod

Docker: "You don' 在/var/cache/apt/archives/中有足够的可用空间”

nginx - 在istio入口网关中启用端口

kubernetes - ArgoCD 无法使用 2FA 连接到 github 帐户

kubernetes - Istio - 无法通过 https 访问外部 IP,且 outboundTrafficPolicy.mode 设置为 REGISTRY_ONLY

powershell 中的 Azure DevOps 变量组

azure - 没有为 '/api/continuouswebjobs/WebJobsName/run' 注册路由

java - 从 'RUN ./mvnw dependency:go-offline -B' 为 Spring Boot 应用程序构建 docker 镜像时,无法运行 "openjdk:8-jdk-alpine"