k8s挂载目录,Kubernetes volumeMount文件夹和文件权限?

在尝试将配置文件从主机路径挂载到Kubernetes容器时,作者遇到了问题。该操作在Minikube和VirtualBox共享文件夹中成功,但在Linux上(特别是AWS EKS)失败。问题在于文件需要存在于每个EKS节点实例上。解决方案是使用initContainer,通过在每个EC2实例上下载配置文件来解决文件权限和位置的问题。
摘要由CSDN通过智能技术生成

Trying to mount config files from a hostPath to a kubernetes container. This works using minikube and VirtualBox shared folder, but I am unable to make this work on Linux.

I making use of AWS EKS and the following architecture https://aws.amazon.com/quickstart/architecture/amazon-eks/. I think my problem is that the files need to live on each of the EKS Node instances.

Here is the architecture diagram:

37ae4b47b1e8dcd5a3a1f0ef0cb4de7f.png

Below is the Deployment file.

apiVersion: apps/v1

kind: Deployment

metadata:

name: openhim-core-deployment

spec:

replicas: 1

selector:

matchLabels:

component: openhim-core

template:

metadata:

labels:

component: o

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java代码示例,可以连接到Kubernetes集群,启动模型评估任务,并将结果输出到MinIO中: ``` import io.minio.MinioClient; import io.minio.errors.*; import io.minio.messages.Item; import io.swagger.client.ApiClient; import io.swagger.client.ApiException; import io.swagger.client.Configuration; import io.swagger.client.api.DefaultApi; import io.swagger.client.model.V1ObjectMeta; import io.swagger.client.model.V1Pod; import io.swagger.client.model.V1PodSpec; import io.swagger.client.model.V1PodTemplateSpec; import io.swagger.client.model.V1Container; import io.swagger.client.model.V1ContainerPort; import io.swagger.client.model.V1EnvVar; import io.swagger.client.model.V1EnvVarSource; import io.swagger.client.model.V1SecretKeySelector; import io.swagger.client.model.V1Volume; import io.swagger.client.model.V1VolumeMount; import java.io.IOException; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; public class K8sEvaluationTask { private static final String K8S_NAMESPACE = "default"; private static final String K8S_API_URL = "https://kubernetes.default.svc"; private static final String MINIO_ENDPOINT = "http://minio-service.default.svc.cluster.local:9000"; private static final String MINIO_ACCESS_KEY = "accesskey"; private static final String MINIO_SECRET_KEY = "secretkey"; private static final String MINIO_BUCKET_NAME = "evaluation-results"; public static void main(String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidResponseException, ErrorResponseException, XmlParserException, InternalException { // 用户选择模型和数据集 String modelPath = "/models/my-model.pb"; String datasetPath = "/datasets/my-dataset.csv"; // 从MiniIO下载模型和验证集 MinioClient minioClient = new MinioClient(MINIO_ENDPOINT, MINIO_ACCESS_KEY, MINIO_SECRET_KEY); InputStream modelStream = minioClient.getObject("models", modelPath); InputStream datasetStream = minioClient.getObject("datasets", datasetPath); // 从Pachyderm下载数据集 Properties properties = new Properties(); InputStream inputStream = K8sEvaluationTask.class.getResourceAsStream("/pachyderm.properties"); properties.load(inputStream); String pachdHost = properties.getProperty("pachd.host"); String pachdPort = properties.getProperty("pachd.port"); String pachToken = properties.getProperty("pachd.token"); String pachRepo = "my-repo"; String pachBranch = "master"; String pachPath = "/my-dataset.csv"; ApiClient pachydermClient = Configuration.getDefaultApiClient(); pachydermClient.addDefaultHeader("Authorization", "Bearer " + pachToken); DefaultApi defaultApi = new DefaultApi(pachydermClient); InputStream pachydermStream = defaultApi.getFile(pachRepo, pachBranch, pachPath); // 启动Kubernetes Pod ApiClient apiClient = Configuration.getDefaultApiClient(); apiClient.setBasePath(K8S_API_URL); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient okHttpClient = apiClient.getHttpClient().newBuilder() .addInterceptor(loggingInterceptor) .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .build(); apiClient.setHttpClient(okHttpClient); V1Pod pod = new V1Pod(); V1ObjectMeta metadata = new V1ObjectMeta(); metadata.setName("evaluation-task"); pod.setMetadata(metadata); V1PodSpec spec = new V1PodSpec(); V1Container container = new V1Container(); container.setName("evaluation"); container.setImage("tensorflow/tensorflow:latest-gpu"); List<String> command = new ArrayList<>(); command.add("python"); command.add("/app/evaluation.py"); container.setCommand(command); List<String> argsList = new ArrayList<>(); argsList.add("--model_path=/mnt/models/my-model.pb"); argsList.add("--dataset_path=/mnt/datasets/my-dataset.csv"); container.setArgs(argsList); V1EnvVar minioEndpoint = new V1EnvVar(); minioEndpoint.setName("MINIO_ENDPOINT"); minioEndpoint.setValue(MINIO_ENDPOINT); container.addEnvItem(minioEndpoint); V1EnvVar minioAccessKey = new V1EnvVar(); minioAccessKey.setName("MINIO_ACCESS_KEY"); minioAccessKey.setValueFrom(new V1EnvVarSource().secretKeyRef(new V1SecretKeySelector().name("minio-credentials").key("accesskey"))); container.addEnvItem(minioAccessKey); V1EnvVar minioSecretKey = new V1EnvVar(); minioSecretKey.setName("MINIO_SECRET_KEY"); minioSecretKey.setValueFrom(new V1EnvVarSource().secretKeyRef(new V1SecretKeySelector().name("minio-credentials").key("secretkey"))); container.addEnvItem(minioSecretKey); V1VolumeMount modelVolumeMount = new V1VolumeMount(); modelVolumeMount.setName("models"); modelVolumeMount.setMountPath("/mnt/models"); container.addVolumeMountsItem(modelVolumeMount); V1VolumeMount datasetVolumeMount = new V1VolumeMount(); datasetVolumeMount.setName("datasets"); datasetVolumeMount.setMountPath("/mnt/datasets"); container.addVolumeMountsItem(datasetVolumeMount); V1ContainerPort containerPort = new V1ContainerPort(); containerPort.setContainerPort(8080); container.addPortsItem(containerPort); spec.addContainersItem(container); V1Volume modelVolume = new V1Volume(); modelVolume.setName("models"); V1SecretKeySelector modelSecretKeySelector = new V1SecretKeySelector(); modelSecretKeySelector.setName("minio-credentials"); modelSecretKeySelector.setKey("accesskey"); V1Volume.Secret modelSecret = new V1Volume.Secret(); modelSecret.setSecretName("minio-credentials"); modelVolume.setSecret(modelSecret); spec.addVolumesItem(modelVolume); V1Volume datasetVolume = new V1Volume(); datasetVolume.setName("datasets"); V1SecretKeySelector datasetSecretKeySelector = new V1SecretKeySelector(); datasetSecretKeySelector.setName("pachyderm-credentials"); datasetSecretKeySelector.setKey("token"); V1Volume.Secret datasetSecret = new V1Volume.Secret(); datasetSecret.setSecretName("pachyderm-credentials"); datasetVolume.setSecret(datasetSecret); spec.addVolumesItem(datasetVolume); V1PodTemplateSpec template = new V1PodTemplateSpec(); template.setSpec(spec); pod.setSpec(spec); // 发布Pod DefaultApi defaultApi = new DefaultApi(apiClient); defaultApi.createNamespacedPod(K8S_NAMESPACE, pod, null, null, null); // 等待Pod完成 boolean isRunning = true; while (isRunning) { try { TimeUnit.SECONDS.sleep(5); V1Pod statusPod = defaultApi.readNamespacedPodStatus("evaluation-task", K8S_NAMESPACE, null, null, null); if (statusPod.getStatus().getPhase().equals("Succeeded")) { isRunning = false; } else if (statusPod.getStatus().getPhase().equals("Failed")) { throw new RuntimeException("Pod failed to execute"); } } catch (InterruptedException | ApiException e) { e.printStackTrace(); } } // 上传评估结果到MiniIO Item item = new Item(); item.objectName(MINIO_BUCKET_NAME + "/evaluation-results.csv"); minioClient.putObject("evaluation-results", "evaluation-results.csv", pachydermStream, null, null, null, null); } } ``` 这个代码示例假设您已经有了一个MinIO服务器和一个Pachyderm集群。您需要将MinIO和Pachyderm的端点和凭据配置为环境变量或属性文件。此外,您需要根据您的模型和数据集的位置修改代码示例中的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值