K8s create POD and mount PVC

PVC

A Persistent Volume Claim (PVC) in Kubernetes is a request for a specific amount of storage resources. PVCs are used to dynamically provision persistent storage volumes in a Kubernetes cluster.

Here is a brief explanation of the components and concepts related to PVCs:

  • Persistent Volume (PV): A PV is a piece of networked storage in the cluster, such as a physical disk or a network share. It is a resource that is provisioned by the cluster administrator and made available to be claimed by PVCs.
  • Storage Class: A Storage Class defines the properties and parameters for dynamically provisioning a PV. It allows you to define the type, size, and access mode of the storage.
  • Persistent Volume Claim (PVC): A PVC is a request for a specific amount of storage resources defined in terms of size and access mode (e.g., ReadWriteOnce, ReadOnlyMany, ReadWriteMany). When a pod wants to use persistent storage, it creates a PVC, and the cluster’s storage layer dynamically provisions and binds it to an available PV.
  • Binding: When a PVC is created, Kubernetes attempts to find an available PV that satisfies the requirements specified in the PVC. If a suitable PV is found, the PVC gets bound to the PV.
  • Mounting: Once a PVC is bound to a PV, the pod’s container can mount the PVC as a volume to access persistent storage. This allows the data to survive pod restarts and be shared across multiple pods if the PVC is created with an appropriate access mode.

To use a PVC:

  1. Define a Storage Class with the desired properties and parameters (optional if your cluster has a default Storage Class).
  2. Create a PVC that requests a specific amount of storage resources using the desired Storage Class.
  3. Create a pod and configure it to mount the PVC as a volume.
  4. When the pod is created, Kubernetes will automatically provision and bind a suitable PV to the PVC, and the pod’s container can use the PVC as a persistent storage volume.

PVCs help to decouple the applications from the underlying storage infrastructure, allowing for more flexibility and portable deployments.

maven pom依赖

		<dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>7.0.0</version>
        </dependency>

创建POD && 挂载PVC && 拉去镜像策略等配置

apiVersion: v1
kind: Pod
metadata:
 name: li55
 namespace: default
spec:
 containers:
 - name: li55
   image: your-harbor-image-name
   imagePullPolicy: Always
   command:
   - java
   - -jar
   - /workspace/microDataTask.jar
   resources:
     limits:
       cpu: 2
       memory: 2Gi
     requests:
       cpu: 2
       memory: 2Gi
 volumes:
 - name: li55
   persistentVolumeClaim:
     claimName: li55
apiVersion: v1
kind: PersistentVolume
metadata:
 name: li55
spec:
 accessModes:
   - ReadWriteOnce
 resources:
   requests:
     storage: 10Gi
apiVersion: v1
kind: VolumeMount
metadata:
 name: li55
 namespace: default
spec:
 mountPath: /home/li55
 readOnly: false

V1ResourceRequirements resourceRequirements = new V1ResourceRequirementsBuilder()
                .addToLimits("cpu", new Quantity(String.valueOf(2)))
                .addToLimits("memory", new Quantity( 2 + "Gi"))
                .addToRequests("cpu", new Quantity(String.valueOf(2)))
                .addToRequests("memory", new Quantity(2 + "Gi"))
                .build();
List<V1VolumeMount> volumeMountList = new LinkedList<>();
        // 增加jar包ceph路径
        V1VolumeMount workPath = new V1VolumeMountBuilder()
                .withName("li55")
                .withMountPath("/home/li55")
                .withReadOnly(false)
                .build();
        volumeMountList.add(workPath);

List<V1Volume> volumeList = new LinkedList<>();
V1Volume dataVolume = new V1VolumeBuilder()
                .withName("li55")
                .withNewPersistentVolumeClaim()
                .withClaimName("li55")
                .endPersistentVolumeClaim()
                .build();
        volumeList.add(dataVolume);
V1Pod pod = new V1PodBuilder()
                .withNewMetadata()
                .withName(podName)
                .withNamespace(namespace)
                .endMetadata()
                .withKind("Pod")
                .withApiVersion("v1")
                .withNewSpec()
                .addNewContainer()
                .withName(podName)
                .withImage("your-harbor-image-name")
                .withImagePullPolicy("Always")//Always,IfNotPresent
                .withCommand("java", "-jar", "/workspace/microDataTask.jar", json.toJSONString())
                .withResources(resourceRequirements)
                .withVolumeMounts(volumeMountList)
                .endContainer()
                .withVolumes(volumeList)
//                .withEnv(envVarList)
                .withRestartPolicy("Never")
                .withTerminationGracePeriodSeconds(2L)
                .endSpec()
                .build();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值