从另一个 pod 更改 kubernetes stroge class 挂载值

Change kubernetes stroge class mounted value from another pod

我想用 kubernetes 发布 sonarqube。我成功地使用了官方软件包。但我想使用一些旧版本的插件和一些自定义插件。在本地使用 docker-compose 文件,我创建了一个飞行容器,用插件填充插件目录 (/opt/sonarqube/extensions/plugins)。并将该卷与 sonarqube 容器一起使用。作为结论:Sonarqube 扩展卷目录是从不同的容器创建(或填充)的(完成工作并死亡)。

我想使用与 kubernetes 相同的路径,但不能这样做。我的飞行容器没有填满路径。

我的 kubernetes 部署文件:

1-) sonar-pvc-extensions.yml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: claim-sonar-extensions
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi

2-) sonarqube-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      name: sonarqube
  template:
    metadata:
      name: sonarqube
      labels:
        name: sonarqube
    spec:
      containers:
        - image: sonarqube:7.9.1-community
          name: sonarqube
          env:
            - name: SONARQUBE_JDBC_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-pwd
                  key: password
            - name: SONARQUBE_JDBC_URL
              value: jdbc:postgresql://sonar-postgres:5432/sonar
          ports:
            - containerPort: 9000
              name: sonarqube
          volumeMounts:               
            - name: data-sonar-extensions
              mountPath: /opt/sonarqube/extensions/plugins
          resources:
            requests:
              memory: 2000Mi
            limits:
              memory: 2000Mi
      volumes:
        - name: data-sonar-extensions
          persistentVolumeClaim:
            claimName: claim-sonar-extensions
      initContainers:
        - name: sysctl
          image: busybox
          imagePullPolicy: IfNotPresent
          command: ['sysctl', '-w', 'vm.max_map_count=262144']
          securityContext:
            privileged: true

3-)示例插件 Dockerfile

FROM alpine:3.4

RUN apk --no-cache add --repository http://dl-cdn.alpinelinux.org/alpine/edge/community wget ca-certificates

ENV PLUGINS_DIR /opt/sonarqube/extensions/plugins

WORKDIR $PLUGINS_DIR
RUN wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.1.0-SNAPSHOT/sonar-gitlab-plugin-4.1.0-SNAPSHOT.jar
RUN wget https://binaries.sonarsource.com/Distribution/sonar-java-plugin/sonar-java-plugin-6.3.0.21585.jar
RUN wget https://github.com/SonarSource/sonar-php/releases/download/3.4.0.5461/sonar-php-plugin-3.4.0.5461.jar
ENV JAVASCRIPT_VERSION 2.20.0.4207

VOLUME $PLUGINS_DIR

CMD ls -asl $PLUGINS_DIR

我用声纳插件尝试了这种方法-deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonarqube-plugin
spec:
  replicas: 1
  selector:
    matchLabels:
      name: sonarqube-plugin
  template:
    metadata:
      name: sonarqube-plugin
      labels:
        name: sonarqube-plugin
    spec:
      containers:
        - image: my-kubernetes-registry/plugins
          name: sonarqube-plugins
          volumeMounts:
            # This name must match the volumes.name below.
            - name: data-sonar-extensions
              mountPath: /opt/sonarqube/extensions/plugins
      volumes:
        - name: data-sonar-extensions
          persistentVolumeClaim:
            claimName: claim-sonar-extensions

但是没有成功。并打破一些东西。这次我的插件目录变空了:

sonarqube@sonarqube-85b98d9845-l2sql:/opt/sonarqube/extensions/plugins$ ls -al
total 24
drwxr-xr-x 3 root      root       4096 May 30 16:19 .
drwxr-xr-x 1 sonarqube sonarqube  4096 May 30 16:39 ..
drwx------ 2 root      root      16384 May 30 16:19 lost+found

我没有使用持久卷。 PVC 正在寻找 stroge class。所以我不能将 accessModes 用作 ReadWriteMany。

结果:我想用飞走容器更改 stroge 路径并在应用程序中使用该路径。

我是 kubernetes 的新手,如果您提出不同的方法,我将不胜感激。

您确定要使用相同的目录来安装容器卷和存储插件吗?我相信这会引起冲突。您可以尝试更改卷安装目录(在部署容器规范中)吗?

勾选 Init Containers 应该符合您的需要。

您可以使用 init 容器用数据填充卷,完成后您可以 运行 您的应用程序基于此数据。

当我们在 Kubernetes 中挂载 Docker 的 WORKDIR 时,Kubernetes 会清理或覆盖目录。在这种情况下,我们必须在 Docker 中设置与挂载路径不同的目录,如 WORKDIR,稍后在 Kubernetes 中,我们必须将我们的内容移动到挂载目录。

Docker文件,这里我配置WORKDIR/opt/sonarqube/plugins

FROM alpine:3.11

RUN apk --no-cache add --repository http://dl-cdn.alpinelinux.org/alpine/edge/community wget ca-certificates

WORKDIR /opt/sonarqube/plugins

RUN wget https://github.com/gabrie-allaigre/sonar-gitlab-plugin/releases/download/4.1.0-SNAPSHOT/sonar-gitlab-plugin-4.1.0-SNAPSHOT.jar &&\
    wget https://binaries.sonarsource.com/Distribution/sonar-java-plugin/sonar-java-plugin-6.3.0.21585.jar &&\
    wget https://github.com/SonarSource/sonar-php/releases/download/3.4.0.5461/sonar-php-plugin-3.4.0.5461.jar

sonar.yml,这里我使用了postStart生命周期钩子将插件复制到挂载路径。它会在 pod 启动之前将外部插件复制到挂载路径。

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: sonarqube
  name: sonarqube
spec:
  containers:
  - image: harik8/demo:latest
    name: sonarqube
    resources: {}
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c", "mv /opt/sonarqube/plugins/*.jar /opt/sonarqube/extensions/plugins"]
    command: ["cat"]
    tty: true
    volumeMounts:
    - name: data-sonar-extensions
      mountPath: /opt/sonarqube/extensions/plugins
  dnsPolicy: ClusterFirst
  restartPolicy: Never
  volumes:
  - name: data-sonar-extensions
    emptyDir: {}
status: {}

pod 启动后,执行并列出 /opt/sonarqube/extensions/plugins 目录,

$ kubectl exec -it sonarqube sh
/opt/sonarqube/extensions/plugins # pwd
/opt/sonarqube/extensions/plugins
/opt/sonarqube/extensions/plugins # ls -la
total 33320
drwxrwxrwx    2 root     root          4096 May 31 20:59 .
drwxr-xr-x    3 root     root          4096 May 31 20:59 ..
-rw-r--r--    1 root     root      10280677 Mar 28  2019 sonar-gitlab-plugin-4.1.0-SNAPSHOT.jar
-rw-r--r--    1 root     root      18712457 Apr  8 13:26 sonar-java-plugin-6.3.0.21585.jar
-rw-r--r--    1 root     root       5114341 May 11 15:24 sonar-php-plugin-3.4.0.5461.jar
/opt/sonarqube/extensions/plugins