1. 程式人生 > 其它 >kubernetes--csi-driver-nfs-在Task和Step上使用Volume

kubernetes--csi-driver-nfs-在Task和Step上使用Volume

在Task和Step上使用Volume Volume可顯式定義要在Task和Step上使用的儲存卷   ◼ 例如,為Maven指定Cache     ◆將下載的模組保存於指定的Volume上,即可由相關的Task和Step重複使用     ◆即便是不同的Pipeline,也能夠使用同一個基於PVC等支援多個Pod訪問的儲存卷的maven cache   ◼ Volume的定義和使用     ◆在Task的spec.volumes欄位中定義儲存卷列表     ◆在Step中使用volumeMounts進行引用   ◼ 其使用方式與在Pod和Container上的方式相同 右側示例即為使用了Volume的task/build-package的定義   ◼ 提示:相關的PVC資源需要事先定義   ◼ 另外,基於該Pipeline多次執行的PipelineRun便可通過該Volume使用maven cahe; 

1,部署csi-nfs

https://github.com/kubernetes-csi/csi-driver-nfs/blob/master/docs/install-csi-driver-v3.1.0.md

curl -skSL https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/v3.1.0/deploy/install-driver.sh | bash -s v3.1.0 --

NFS CSI driver installed successfully

git clone https://github.com/kubernetes-csi/csi-driver-nfs.git


cd csi-driver-nfs
./deploy/install-driver.sh v3.1.0 local

root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver/csi-driver-nfs/deploy# kubectl get deploy  csi-nfs-controller -nkube-system -oyaml|grep image |grep -v fNotPresen
        image: k8s.gcr.io/sig-storage/csi-provisioner:v2.2.2
        image: k8s.gcr.io
/sig-storage/livenessprobe:v2.5.0 image: mcr.microsoft.com/k8s/csi/nfs-csi:v3.1.0
root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver/csi-driver-nfs/deploy# kubectl get deploy  csi-nfs-controller -nkube-system -oyaml|grep image |grep -v fNotPresen
        image: k8s.gcr.io/sig-storage/csi-provisioner:v2.2.2
image: k8s.gcr.io/sig-storage/livenessprobe:v2.5.0 image: mcr.microsoft.com/k8s/csi/nfs-csi:v3.1.0 root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver/csi-driver-nfs/deploy# kubectl get deploy csi-nfs-controller -nkube-system -oyaml|grep image |grep -v fNotPresen image: gexuchuan123/sig-storage-csi-provisioner:v2.2.2 image: gexuchuan123/sig-storage-livenessprobe:v2.5.0 image: gexuchuan123/k8s-csi-nfs-csi:v3.1.0

 #更改映象

kubectl edit deploy  csi-nfs-controller -nkube-system

node

root@master01:~# cat   /root/tekton-and-argocd-in-practise/nfs-csi-driver/csi-driver-nfs/deploy/v3.1.0/csi-nfs-node.yaml|grep image
          image: gexuchuan123/sig-storage-livenessprobe:v2.5.0                #k8s.gcr.io/sig-storage/livenessprobe:v2.5.0
          image: gexuchuan123/sig-storage-csi-node-driver-registrar:v2.4.0  #k8s.gcr.io/sig-storage/csi-node-driver-registrar:v2.4.0   #拉去映象
          image: gexuchuan123/k8s-csi-nfs-csi:v3.1.0              #mcr.microsoft.com/k8s/csi/nfs-csi:v3.1.0

 

root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver/csi-driver-nfs/deploy# kubectl get po -nkube-system 
NAME                                       READY   STATUS    RESTARTS   AGE
csi-nfs-controller-789dd4c454-p6lbr        3/3     Running   0          21m
csi-nfs-controller-789dd4c454-tcqj4        3/3     Running   0          21m
csi-nfs-node-dblv9                         3/3     Running   0          8m34s
csi-nfs-node-lcdp8                         3/3     Running   0          8m33s
csi-nfs-node-qtb7m                         3/3     Running   0          8m3

 2,nfs名稱空間建立pvc

 https://github.com/kubernetes-csi/csi-driver-nfs/blob/master/deploy/example/nfs-provisioner/README.md

kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nfs-server.yaml

2.1 default 名稱空間下,參考

root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver# cat nfs-server.yaml 
---
kind: Service
apiVersion: v1
metadata:
  name: nfs-server
  labels:
    app: nfs-server
spec:
  type: ClusterIP  # use "LoadBalancer" to get a public ip
  selector:
    app: nfs-server
  ports:
    - name: tcp-2049
      port: 2049
      protocol: TCP
    - name: udp-111
      port: 111
      protocol: UDP
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-server
  template:
    metadata:
      name: nfs-server
      labels:
        app: nfs-server
    spec:
      nodeSelector:
        "kubernetes.io/os": linux
      containers:
        - name: nfs-server
          image: itsthenetwork/nfs-server-alpine:latest
          env:
            - name: SHARED_DIRECTORY
              value: "/exports"
          volumeMounts:
            - mountPath: /exports
              name: nfs-vol
          securityContext:
            privileged: true
          ports:
            - name: tcp-2049
              containerPort: 2049
              protocol: TCP
            - name: udp-111
              containerPort: 111
              protocol: UDP
      volumes:
        - name: nfs-vol
          hostPath:
            path: /nfs-vol  # modify this to specify another path to store nfs share data
            type: DirectoryOrCreate
root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver# cat 03-storageclass-nfs.yaml 
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-csi
provisioner: nfs.csi.k8s.io
parameters:
  server: nfs-server.default.svc.cluster.local     #nfs-server.nfs.svc.cluster.local
  share: /
  # csi.storage.k8s.io/provisioner-secret is only needed for providing mountOptions in DeleteVolume
  # csi.storage.k8s.io/provisioner-secret-name: "mount-options"
  # csi.storage.k8s.io/provisioner-secret-namespace: "default"
reclaimPolicy: Retain
volumeBindingMode: Immediate
mountOptions:
  - hard
  - nfsvers=4.1
root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver# cat 05-pvc-maven-cache.yaml 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: maven-cache
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-csi

kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nginx-pod.yaml

2.2 nfs名稱空間空間下,部署

root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver# cat 01-nfs-server.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: nfs
---
kind: Service
apiVersion: v1
metadata:
  name: nfs-server
  namespace: nfs
  labels:
    app: nfs-server
spec:
  type: ClusterIP  # use "LoadBalancer" to get a public ip
  selector:
    app: nfs-server
  ports:
    - name: tcp-2049
      port: 2049
      protocol: TCP
    - name: udp-111
      port: 111
      protocol: UDP
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-server
  namespace: nfs
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-server
  template:
    metadata:
      name: nfs-server
      labels:
        app: nfs-server
    spec:
      nodeSelector:
        "kubernetes.io/os": linux
      containers:
        - name: nfs-server
          image: itsthenetwork/nfs-server-alpine:latest
          env:
            - name: SHARED_DIRECTORY
              value: "/exports"
          volumeMounts:
            - mountPath: /exports
              name: nfs-vol
          securityContext:
            privileged: true
          ports:
            - name: tcp-2049
              containerPort: 2049
              protocol: TCP
            - name: udp-111
              containerPort: 111
              protocol: UDP
      volumes:
        - name: nfs-vol
          hostPath:
            path: /nfs-vol  # modify this to specify another path to store nfs share data
            type: DirectoryOrCreate
View Code
root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver# cat 03-storageclass-nfs.yaml 
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-csi
provisioner: nfs.csi.k8s.io
parameters:
  server: nfs-server.nfs.svc.cluster.local
  share: /
  # csi.storage.k8s.io/provisioner-secret is only needed for providing mountOptions in DeleteVolume
  # csi.storage.k8s.io/provisioner-secret-name: "mount-options"
  # csi.storage.k8s.io/provisioner-secret-namespace: "default"
reclaimPolicy: Retain
volumeBindingMode: Immediate
mountOptions:
  - hard
  - nfsvers=4.1
View Code
root@master01:~/tekton-and-argocd-in-practise/nfs-csi-driver# cat 05-pvc-maven-cache.yaml 
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: maven-cache
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-csi
View Code

root@master01:~/tekton-and-argocd-in-practise/03-tekton-advanced# kubectl get pv
NAME   CAPACITY                       ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-3c9f2e64-e459-4495-a962-4fd693eb1935 5Gi     RWX   Retain  Bound   default/maven-cache       nfs-csi 4h28

root@master01:~/tekton-and-argocd-in-practise/03-tekton-advanced# kubectl get pvc
NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
maven-cache   Bound    pvc-3c9f2e64-e459-4495-a962-4fd693eb1935   5Gi        RWX            nfs-csi        

2.3 在容器中指定掛載目錄,task引用maven-cache儲存卷

root@master01:~/tekton-and-argocd-in-practise/03-tekton-advanced# cat task-source-to-package-02.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: source-to-package
spec:
  params:
  - name: git-repo-url
    type: string
  workspaces:
    - name: source
      description: code storage
  steps:
    - name: fetch-from-source
      image: alpine/git:v2.32.0
      script: |
        git clone $(params.git-repo-url) $(workspaces.source.path)/source
    - name: build-to-package
      image: maven:3.8-openjdk-11-slim
      workingDir: $(workspaces.source.path)/source
      script: |
        mvn clean install
      volumeMounts:
        - name: maven-cache
          mountPath: /root/.m2
  volumes:
  - name: maven-cache
    persistentVolumeClaim:
      claimName: maven-cache

檢視

kubectl get task source-to-package -oyaml

2.4  pipline 引用task,共享儲存卷

root@master01:~/tekton-and-argocd-in-practise/03-tekton-advanced# cat pipeline-source-to-packagea
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: s2p-demo
spec:
  params:
    - name: git-repo-url
      type: string
  workspaces:
    - name: source
  tasks:
    - name: source-to-package
      params:
        - name: git-repo-url
          value: $(params.git-repo-url)
      workspaces:
        - name: source
          workspace: source
      taskRef:
        name: source-to-package

tkn  pipeline start s2p-demo --showlog -p git-repo-url='https://gitee.com/mageedu/spring-boot-helloWorld.git' -w name=source,emptyDir=""

root@master01:~/tekton-and-argocd-in-practise/03-tekton-advanced# tkn  pipeline list
NAME                   AGE              LAST RUN                         STARTED          DURATION     STATUS
s2p-demo               27 minutes ago   s2p-demo-run-8z5xg               27 minutes ago   13 minutes   Succeeded

 

 

root@master01:~/tekton-and-argocd-in-practise/03-tekton-advanced# cat pipelinerun-s2p-demo.yaml 
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: s2p-demo-run-00001
spec:
  pipelineRef:
    name: s2p-demo
  params:
    - name: git-repo-url
      value: https://gitee.com/mageedu/spring-boot-helloWorld.git
  workspaces:
    - name: source
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi

驗證快取生效

 tkn pipelinerun logs s2p-demo-run-00001