腾讯云TKE-PV使用COS存储案例:容器目录权限问题

背景

在TKE的集群中创建工作负载并把某一个对应的cos桶的根目录挂载到/data目录,在镜像构建的时候有把/data目录设置权限为755,但是运行容器后成功挂载cos桶的根目录到/data/目录,发现用非root账号却无法访问/data下面的文件,镜像的启动用户是非root用户,查看容器内/data目录权限变成了700。

为什么设置的目录权限是755,挂载到COS后就变成了700权限呢?

原因分析

测试启动2个nginx工作负载,一个负载将目录/etc/nginx/conf.d挂载到cos桶上,另一个工作负载正常运行不挂载,然后发现确实挂载cos后,默认会把目录权限变成700。

TKE中使用cos本质上是使用的Cosfs,腾讯云官方文档 COSFS 工具使用里面可以查到这样一个参数-oallow_other 。如果要允许其他用户访问挂在文件夹, 可以在允许COSFS的时候指定该参数。

COSFS 工具:https://cloud.tencent.com/document/product/436/6883?from=10680

解决方案

TKE中如何配置-oallow_other参数?

在使用cos桶进行挂载的时候在pv创建界面是可以进行参数设置的,但是由于我们习惯在控制台直接创建pvc关联pv,然后pv会自动创建导致很多人没有去关注这个cos的参数。

方式一:手动创建

通过编写yaml文件来创建pv,pvc

以下内容来源:https://github.com/TencentCloud/kubernetes-csi-tencentcloud/blob/master/docs/README_COSFS.md

pv-cos.yaml(需要关注volumeAttributes 下的additional_args 属性添加了-oallow_other)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: "pv-cos"
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 1Gi
  csi:
    driver: com.tencent.cloud.csi.cosfs
    # Specify a unique volumeHandle like bucket name.(this value must different from other pv's volumeHandle)
    volumeHandle: xxx
    volumeAttributes:
      # Replaced by the url of your region.
      url: "http://cos.ap-guangzhou.myqcloud.com"
      # Replaced by the bucket name you want to use.
      bucket: "testbucket-1010101010"
      # You can specify sub-directory of bucket in cosfs command in here.
      # path: "/my-dir"
      # You can specify any other options used by the cosfs command in here.
      additional_args: "-oallow_other"
    nodePublishSecretRef:
      # Replaced by the name and namespace of your secret.
      name: cos-secret
      namespace: kube-system

pvc-cos.yaml(存储大小自行修改)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: pvc-cos
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  # You can specify the pv name manually or just let kubernetes to bind the pv and pvc.
  # volumeName: pv-cos
  # Currently cos only supports static provisioning, the StorageClass name should be empty.
  storageClassName: ""

kubectl apply -f pv-cos.yaml pvc-cos.yaml

$ kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS   REASON    AGE
pv-cos    1Gi        RWX            Retain           Available                                      5s
$ kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-cos   Bound     pv-cos    1Gi        RWX

方式二:控制台创建

1、创建pv

在挂载选项填入-oallow_other这个参数,想填写多个参数空格分隔,cos提供的参数配置选项可以参考:https://cloud.tencent.com/document/product/436/6883#.E5.B8.B8.E7.94.A8.E6.8C.82.E8.BD.BD.E9.80.89.E9.A1.B9

2、创建pvc并关联pv

3、在工作负载中使用pvc

4、验证对应的目录权限是否正确

进入容器中查看/etc/nginx/conf.d的目录不再是700,创建一个test文件,也挂载到了cos桶

5、上传一个文件到cos桶看容器中是否可以访问


posted @ 2021-01-05 12:29  云存储小天使  阅读(391)  评论(0编辑  收藏  举报