首页 > 解决方案 > 使用 gitlab 在 pod 内的多个容器中安装 kubernetes 卷

问题描述

我第一次设置了一个由单节点 kubernetes (minikube) 组成的 CI/CD 环境。

在这个节点上我创建了一个 PV

NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                   STORAGECLASS   REASON   AGE
data-volume   1Gi        RWO            Retain           Bound    gitlab-managed-apps/data-volume-claim   manual                  20m

和聚氯乙烯

NAME                STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-volume-claim   Bound    data-volume   1Gi        RWO            manual         19m

现在我想创建一个具有多个容器访问此卷的 pod。

您建议在哪里以及如何使用 gitlab 管道 gitlab-ci 等进行设置?多个存储库可能最适合该项目。

标签: kubernetesgitlabgitlab-ciminikubepersistent-volumes

解决方案


这是部署清单文件的完整工作示例,在 Pod 的规范中使用相同的 PV 定义了两个容器(基于不同的 nginx docker 映像),它们相应地在端口 80/81 上提供自定义静态 html 内容:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: null
  generation: 1
  labels:
    run: nginx
  name: nginx
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
    spec:
      volumes:
      - name: my-pv-storage
        persistentVolumeClaim:
          claimName: my-pv-claim-nginx
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: my-pv-storage
          subPath: html_custom 
      - image: custom-nginx
        imagePullPolicy: IfNotPresent
        name: custom-nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: my-pv-storage
          subPath: html
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status: {}

推荐阅读