在Kubernetes上使用Spark写入输出时出现chmod错误 [英] chmod error while writing outputs with Spark on Kubernetes
问题描述
我正在使用AKS(Azure Kubernetes Service,Azure Kubernetes Service)来设置Spark集群,以便使用Kubernetes进行资源管理。我正在使用Spark-Submit以集群模式向K8提交PSPARK应用程序,我已经成功地让应用程序正常运行。
我设置了Azure文件共享来存储应用程序脚本和Persistent Volume,并设置了一个指向此文件共享的Persistent Volume声明,以允许Spark从Kubernetes访问脚本。这对于不编写任何输出的应用程序来说工作得很好,比如Spark源代码中给出的pi.py示例,但是在这种设置下编写任何类型的输出都会失败。我尝试运行脚本以获取字数统计和行
wordCounts.saveAsTextFile(f"./output/counts")
在wordCounts为RDD的情况下导致异常。
Traceback (most recent call last):
File "/opt/spark/work-dir/wordcount2.py", line 14, in <module>
wordCounts.saveAsTextFile(f"./output/counts")
File "/opt/spark/python/lib/pyspark.zip/pyspark/rdd.py", line 1570, in saveAsTextFile
File "/opt/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
File "/opt/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o65.saveAsTextFile.
: ExitCodeException exitCode=1: chmod: changing permissions of '/opt/spark/work-dir/output/counts': Operation not permitted
已经从Spark应用程序创建了目录&[2-0],因此它似乎具有所需的权限,但Spark尝试在内部执行的后续chmod
失败。我还不能找出导致这种情况的原因以及我的命令中缺少了什么确切的配置。如有任何帮助,将不胜感激。
我使用的kubectl
版本是
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.1", GitCommit:"632ed300f2c34f6d6d15ca4cef3d3c7073412212", GitTreeState:"clean", BuildDate:"2021-08-19T15:45:37Z", GoVersion:"go1.16.7", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"881d4a5a3c0f4036c714cfb601b377c4c72de543", GitTreeState:"clean", BuildDate:"2021-10-21T05:13:01Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
Spark版本是2.4.5,我使用的命令是
<SPARK_PATH>/bin/spark-submit --master k8s://<HOST>:443
--deploy-mode cluster
--name spark-pi3
--conf spark.executor.instances=2
--conf spark.kubernetes.authenticate.driver.serviceAccountName=spark
--conf spark.kubernetes.container.image=docker.io/datamechanics/spark:2.4.5-hadoop-3.1.0-java-8-scala-2.11-python-3.7-dm14
--conf spark.kubernetes.driver.volumes.persistentVolumeClaim.azure-fileshare-pvc.options.claimName=azure-fileshare-pvc
--conf spark.kubernetes.driver.volumes.persistentVolumeClaim.azure-fileshare-pvc.mount.path=/opt/spark/work-dir
--conf spark.kubernetes.executor.volumes.persistentVolumeClaim.azure-fileshare-pvc.options.claimName=azure-fileshare-pvc
--conf spark.kubernetes.executor.volumes.persistentVolumeClaim.azure-fileshare-pvc.mount.path=/opt/spark/work-dir
--verbose /opt/spark/work-dir/wordcount2.py
光伏和聚氯乙烯是非常基本的。Pv yml为:
apiVersion: v1
kind: PersistentVolume
metadata:
name: azure-fileshare-pv
labels:
usage: azure-fileshare-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
azureFile:
secretName: azure-storage-secret
shareName: dssparktestfs
readOnly: false
secretNamespace: spark-operator
PVC YML为:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: azure-fileshare-pvc
# Set this annotation to NOT let Kubernetes automatically create
# a persistent volume for this volume claim.
annotations:
volume.beta.kubernetes.io/storage-class: ""
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
selector:
# To make sure we match the claim with the exact volume, match the label
matchLabels:
usage: azure-fileshare-pv
如果需要更多信息,请让我知道。
推荐答案
所有者和用户是超级用户。
看起来您已将卷挂载为根用户。您的问题:
chmod: changing permissions of '/opt/spark/work-dir/output/counts': Operation not permitted
是因为您正在尝试更改您不是其所有者的文件的权限。所以您需要首先更改文件的所有者。
最简单的解决方案是在要访问的资源上chown
。然而,这通常是不可行的,因为它可能会导致权限提升,并且映像本身可能会阻止这种可能性。在这种情况下,您可以创建security context。安全上下文定义Pod或Container的权限和访问控制设置。安全上下文设置包括但不限于:
自主访问控制:访问对象(如文件)的权限基于user ID (UID) and group ID (GID)。
Security Enhanced Linux (SELinux):为对象分配了安全标签。
以特权或非特权身份运行。
Linux Capabilities:为进程提供一些权限,但不是根用户的所有权限。 >;
AppArmor:使用程序配置文件限制单个程序的功能。
Seccomp:筛选进程的系统调用。
AllowPrivilegeEscalation:控制进程是否可以获得比其父进程更多的权限。此bool直接控制是否在容器进程上设置
no_new_privs
标志。当容器满足以下条件时,AllowPrivilegeEscalation始终为True:1)以特权身份运行或2)具有CAP_SYS_ADMIN
。ReadOnlyRootFilessystem:将容器的根文件系统装载为只读。
以上项目符号不是一组完整的安全上下文设置--有关完整列表,请参阅SecurityContext。
有关Linux安全机制的详细信息,请参阅Overview of Linux Kernel Security Features
您可以Configure volume permission and ownership change policy for Pods。
默认情况下,在挂载每个卷时,Kubernetes会递归更改每个卷内容的所有权和权限,以匹配Pod‘ssecurityContext
中指定的fsGroup
。对于大容量,检查和更改所有权和权限可能会花费大量时间,从而降低Pod的启动速度。您可以使用securityContext
中的fsGroupChangePolicy
字段来控制Kubernetes检查和管理卷的所有权和权限的方式。
举个例子:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
fsGroupChangePolicy: "OnRootMismatch"
这篇关于在Kubernetes上使用Spark写入输出时出现chmod错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!