- 首先,创建一个新的 Pod 安全策略:
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: allow-glusterfs
spec:
privileged: false
seLinux:
rule: RunAsAny
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
volumes:
- glusterfs
- 将该策略添加到 Kubernetes 集群中:
$ kubectl create -f allow-glusterfs.yaml
- 在使用 glusterfs 卷的 Pod 的 YAML 文件中添加如下安全上下文配置:
apiVersion: v1
kind: Pod
metadata:
name: my-pod-with-glusterfs-volume
spec:
securityContext:
runAsNonRoot: true # 如果容器用户不是 root 用户,则需要设置为 true。
fsGroup: <gid> # 设置卷所属的 gid,如果未设置则默认为 root 组。
seLinuxOptions:
level: s0:c123,c456 # 设置 SELinux 上下文。
user: system_u # 设置 SELinux 用户(可选)。
role: system_r # 设置 SELinux 角色(可选)。
type: mysqld_t # 设置 SELinux 类型(可选)。
containers:
- name: my-container-with-glusterfs-volume
imagePullPolicy: IfNotPresent
image:<image_name>
volumeMounts:
- name:<volume_name>
mountPath:<mount_path>
readOnly:true/false
ports:
- containerPort:<port_number>
volumes:
- name: <volume_name>
glusterfs:
endpoints: "<glusterfs_endpoint>"
path: "<glusterfs_volume_path>"
readOnly:true/false
其中,<gid>
是 glusterfs 卷所属的 gid;<glusterfs_endpoint>
是 GlusterFS 集群的 IP 地址和端口号;<glusterfs_volume_path>
是 GlusterFS 卷的路径;<mount_path>
是卷挂载到容器内部的路径。还可以设置 readOnly
参数来指定卷是否为只读模式。
- 创建使用 glusterfs 卷的 Pod:
$ kubectl create -f my-pod-with-glusterfs-volume.yaml
这样,就可以成功地使用 glusterfs 卷了。