Search code examples
kubernetesdeploymentsonarqubeinit

deployment throwing error for init container only when I add a second regular container to my deployment


Hi There I am currently trying to deploy sonarqube 7.8-community in GKE using a DB cloudsql instance.

This requires 2 containers ( one for sonarqube and the other for the cloudproxy in order to connect to the DB)

Sonarqube container, however, also requires an init container to give it some special memory requirments.

When I create the deployment with just the sonarqube image and the init container it works fine but this wont be of any use as I need the cloudsql proxy container to connect to my external db. When I add this container though the deployment suddenly errors with the below

deirdrerodgers@cloudshell:~ (meta-gear-306013)$ kubectl create -f initsonar.yaml
The Deployment "sonardeploy" is invalid:spec.template.spec.initContainers[0].volumeMounts[0].name: Not found: "init-sysctl"

This is my complete yaml file with the init container and the other two containers. I wonder is the issue because it doesnt know which container to apply the init container to?

 apiVersion: apps/v1
 kind: Deployment
 metadata:
   labels:
     app: sonardeploy
   name: sonardeploy
   namespace: sonar
 spec:
   replicas: 1
   selector:
     matchLabels:
       app: sonardeploy
   strategy: {}
   template:
     metadata:
       labels:
         app: sonardeploy
     spec:
       initContainers:
         - name: init-sysctl
           image: busybox:1.32
           imagePullPolicy: IfNotPresent
           securityContext:
             privileged: true
           resources:
            {}
           command: ["sh",
                     "-e",
                     "/tmp/scripts/init_sysctl.sh"]
           volumeMounts:
             - name: init-sysctl
               mountPath: /tmp/scripts/
       volumes:
       - name: init-sysctl
         configMap:
           name: sonarqube-sonarqube-init-sysctl
           items:
             - key: init_sysctl.sh
               path: init_sysctl.sh
     spec:
       containers:
       - image: sonarqube:7.8-community
         name: sonarqube
         env:
           - name: SONARQUBE_JDBC_USERNAME
             valueFrom:
             secretKeyRef:
               name: sonarsecret
               key: username
           - name: SONARQUBE_JDBC_PASSWORD
             valueFrom:
             secretKeyRef:
               name: sonarsecret
               key: password
           - name: SONARQUBE_JDBC_URL
             value: jdbc:postgresql://localhost:5432/sonar
         ports:
           - containerPort: 9000
             name: sonarqube
       - name: cloudsql-proxy
         image: gcr.io/cloudsql-docker/gce-proxy:1.17
         command: ["/cloud_sql_proxy",
                   "-instances=meta-gear-306013:us-central1:sonardb=tcp:5432",
                   "-credential_file=/secrets/service_account.json"]
         securityContext:
           runAsNonRoot: true
         volumeMounts:
         - name: cloudsql-instance-credentials-volume
           mountPath: /secrets/
           readOnly: true
       volumes:
       - name: cloudsql-instance-credentials-volume
         secret:
           secretName: cloudsql-instance-credentials

Solution

  • Your yaml file is incorrect. You have two spec: blocks. It should be only one. You need to combine it together. Under spec block should be initContainers block, then containers and finally volumes block. Look at the correct yaml file below:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: sonardeploy
      name: sonardeploy
      namespace: sonar
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: sonardeploy
      strategy: {}
      template:
        metadata:
          labels:
            app: sonardeploy
        spec:
          initContainers:
            - name: init-sysctl
              image: busybox:1.32
              imagePullPolicy: IfNotPresent
              securityContext:
                privileged: true
              resources:
               {}
              command: ["sh",
                        "-e",
                        "/tmp/scripts/init_sysctl.sh"]
              volumeMounts:
                - name: init-sysctl
                  mountPath: /tmp/scripts/
          containers:
          - image: sonarqube:7.8-community
            name: sonarqube
            env:
              - name: SONARQUBE_JDBC_USERNAME
                valueFrom:
                secretKeyRef:
                  name: sonarsecret
                  key: username
              - name: SONARQUBE_JDBC_PASSWORD
                valueFrom:
                secretKeyRef:
                  name: sonarsecret
                  key: password
              - name: SONARQUBE_JDBC_URL
                value: jdbc:postgresql://localhost:5432/sonar
            ports:
              - containerPort: 9000
                name: sonarqube
          - name: cloudsql-proxy
            image: gcr.io/cloudsql-docker/gce-proxy:1.17
            command: ["/cloud_sql_proxy",
                      "-instances=meta-gear-306013:us-central1:sonardb=tcp:5432",
                      "-credential_file=/secrets/service_account.json"]
            securityContext:
              runAsNonRoot: true
            volumeMounts:
            - name: cloudsql-instance-credentials-volume
              mountPath: /secrets/
              readOnly: true
          volumes:
          - name: cloudsql-instance-credentials-volume
            secret:
              secretName: cloudsql-instance-credentials
          - name: init-sysctl
            configMap:
              name: sonarqube-sonarqube-init-sysctl
              items:
                - key: init_sysctl.sh
                  path: init_sysctl.sh