背景

在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: v1kind: PersistentVolumemetadata:  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: v1kind: PersistentVolumeClaimmetadata:    name: pvc-cosspec:  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 pvNAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS   REASON    AGEpv-cos    1Gi        RWX            Retain           Available                                      5s$ kubectl get pvcNAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGEpvc-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桶看容器中是否能够拜访