简体   繁体   English

从另一个吊舱更改 kubernetes stroge class 安装值

[英]Change kubernetes stroge class mounted value from another pod

I want to publish sonarqube with kubernetes.我想用 kubernetes 发布 sonarqube。 I did successfully with official packages.我用官方包成功了。 But i want to use some plugins old version and some custom plugins.但我想使用一些插件旧版本和一些自定义插件。 In local with docker-compose files, i created a fly-away container that fills the plugins directory(/opt/sonarqube/extensions/plugins) with plugins.在本地使用 docker-compose 文件,我创建了一个飞行容器,用插件填充插件目录(/opt/sonarqube/extensions/plugins)。 And use that volume with sonarqube container.并将该卷与 sonarqube 容器一起使用。 As a conclusion: Sonarqube extensions volume directory is created (or filled) from different container(do the job and die).作为结论:Sonarqube 扩展卷目录是从不同的容器创建(或填充)的(完成工作并死掉)。

I want to use the same path with kubernetes but couldn't do that.我想使用与 kubernetes 相同的路径,但不能这样做。 My flyaway container didn't fill the path.我的飞行容器没有填满路径。

My kubernetes deployments files:我的 kubernetes 部署文件:

1-) sonar-pvc-extensions.yml 1-) 声纳-PVC-extensions.yml

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

2-) sonarqube-deployment.yml 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-)Sample plugins Dockerfile 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

I tried that approach with sonar-plugin-deployment.yml:我用 sonar-plugin-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

But didn't successed.但没有成功。 And break something.并打破一些东西。 This time my plugins directory became empty:这次我的插件目录变空了:

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

I am not using persistent volume.我没有使用持久音量。 PVC is looking to stroge class. PVC 正在寻求存储 class。 So i cant use accessModes as ReadWriteMany.所以我不能将 accessModes 用作 ReadWriteMany。

As a results: I want to change a stroge path with a fly-away container and use that path inside an app.结果:我想用一个飞行容器改变一个 stroge 路径,并在应用程序中使用该路径。

I am noob to kubernetes if you suggest a different approach i will be apreciated.如果您提出不同的方法,我将不胜感激 kubernetes。

Are you sure, you want to use the same directory for container volume mounts and to store the plugins.您确定要使用相同的目录来安装容器卷并存储插件。 I believe this is causing a conflict.我相信这会引起冲突。 Can you try changing the volume mount directory (in deployment container spec)?您可以尝试更改卷挂载目录(在部署容器规范中)吗?

Check Init Containers should suit your need.检查Init Containers应该适合您的需要。

You can populate volume with data using init container and when it's done you can run your app on this data.您可以使用 init 容器使用数据填充卷,完成后您可以在此数据上运行您的应用程序。

When we mount Docker's WORKDIR in Kubernetes, Kubernetes cleans up or override the directory.当我们在 Kubernetes 中挂载 Docker 的WORKDIR时,Kubernetes 会清理或覆盖该目录。 In such cases, we have to set a different directory from the mount path as WORKDIR in Docker and later in Kubernetes we have to move our contents to mounted directory.在这种情况下,我们必须在 Docker 和后来的 Kubernetes 中设置与挂载路径不同的目录作为WORKDIR ,我们必须将内容移动到挂载目录。

Dockerfile, here I configured WORKDIR as /opt/sonarqube/plugins Dockerfile,这里我将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, Here I have used postStart lifecycle hook to copy the plugins to mount path. sonar.yml,这里我使用postStart生命周期钩子将插件复制到挂载路径。 It copies the external plugins to the mount path before pod starts.它在 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: {}

Once pod starts, exec and list /opt/sonarqube/extensions/plugins directory,一旦 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM