Jenkins容器在Kubernetes集群上的持久性-PersistentVolumeClaim(VMware / Vsphere)

时间:2019-07-04 11:36:21

标签: jenkins kubernetes vmware vsphere volumes

在删除部署/服务时尝试将我的jenkins作业持久保存到vSphere存储中。

我尝试使用标准方法:使用StorageClass,然后制作一个PersistentVolumeClaim,该文件在将创建部署的.ayml文件中引用。

storage-class.yml:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: mystorage
provisioner: kubernetes.io/vsphere-volume
parameters:
  diskformat: zeroedthick

persistent-volume-claim.yml:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc0003
spec:
  storageClassName: mystorage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 15Gi

jenkins.yml:

---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-auto-ci
  labels:
    app: jenkins-auto-ci
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: jenkins-auto-ci
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins-auto-ci
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: jenkins-auto-ci
    spec:
      containers:
      - name: jenkins-auto-ci
        image: jenkins
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - name: http-port
          containerPort: 80
        - name: jnlp-port
          containerPort: 50000
        volumeMounts:
        - name: jenkins-home
          mountPath: "/var"
      volumes:
        - name: jenkins-home
          persistentVolumeClaim:
            claimName: pvc0003

我希望在删除并重新创建部署后,詹金斯工作能够继续存在。

1 个答案:

答案 0 :(得分:0)

您应该创建VMDK,即虚拟机磁盘。

您可以使用govc(即vSphere CLI)来完成此操作。

govc datastore.disk.create -ds datastore1 -size 2G volumes/myDisk.vmdk

或通过SSH以root的身份使用ESXi CLI进入主机并执行:

vmkfstools -c 2G /vmfs/volumes/datastore1/volumes/myDisk.vmdk

完成此操作后,您应该创建PV,将其命名为vsphere_pv.yaml,如下所示:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  vsphereVolume:
    volumePath: "[datastore1] volumes/myDisk"
    fsType: ext4

本示例中的datastore1是在vCenter的根文件夹中创建的,如果您将其放置在其他位置,则需要更改volumePath。如果它位于DatastoreCluster中,则将volumePath设置为"[DatastoreCluster/datastore1] volumes/myDisk"

通过kubectl apply -f vsphere_pv.yaml

将Yaml应用于Kubernetes

您可以通过描述kubectl describe pv pv0001

来检查它是否已创建

现在您需要PVC,我们称之为vsphere_pvc.yaml来消耗PV。

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc0001
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

通过kubectl apply -f vsphere_pvc.yaml

将Yaml应用于Kubernetes

您可以通过描述kubectl describe pvc pv0001

来检查它是否已创建

完成此操作后,您的yaml可能如下所示:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins-auto-ci
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: jenkins-auto-ci
    spec:
      containers:
      - name: jenkins-auto-ci
        image: jenkins
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
        ports:
        - name: http-port
          containerPort: 80
        - name: jnlp-port
          containerPort: 50000
        volumeMounts:
        - name: jenkins-home
          mountPath: "/var"
      volumes:
        - name: jenkins-home
          persistentVolumeClaim:
            claimName: pvc0001

在Vmware GitHub vsphere-storage-for-kubernetes上对此进行了很好的解释。