在Jenkins中运行KafkaContainer(testcontainers)的问题

3pvhb19x  于 2023-03-22  发布在  Jenkins
关注(0)|答案(1)|浏览(175)

大家好,谁正在阅读这篇文章!我有一个测试java应用程序,包含:

kafka = new KafkaContainer(
                DockerImageName.parse("harbor-main.test.com/devbird/confluentinc/cp-kafka:7.3.2")
                        .asCompatibleSubstituteFor("confluentinc/cp-kafka")
        kafka.withEmbeddedZookeeper().start();
...

我在Jenkins中使用Maven容器构建的。

mvn clean install

我正在使用Jenkins,在Kubernetes中运行。
代理文件,用于Jenkinsfile

apiVersion: v1
kind: Pod
metadata:
  namespace: project-jenkins
spec:
  containers:
    - name: maven
      image: harbor-main.test.com/maven-jdk17-git:latest
      command:
        - cat
      tty: true
      volumeMounts:
        - name: maven-cache
          mountPath: /root/.m2/repository
    - name: dind-daemon
      image: harbor-main.test.com/docker:dind-rootless
      securityContext:
        privileged: true
        readOnlyRootFilesystem: false
      command: 
        - cat
      tty: true
      volumeMounts:
        - name: docker-socket
          mountPath: /run/user/1001/docker.sock
  volumes:
    - name: docker-socket
      emptyDir: {}
    - name: maven-cache
      persistentVolumeClaim:
        claimName: maven-cache

我在Jenkins中得到错误:

08:12:51  23/03/16 08:12:51 WARN TestcontainersConfiguration: Attempted to read Testcontainers configuration file at file:/root/.testcontainers.properties but the file was not found. Exception message: FileNotFoundException: /root/.testcontainers.properties (No such file or directory)
08:12:52  23/03/16 08:12:51 ERROR DockerClientProviderStrategy: Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
08:12:52    UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)As no valid configuration was found, execution cannot continue.
08:12:52  See https://www.testcontainers.org/on_failure.html for more details.
08:12:52  Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.228 sec <<< FAILURE!

我是否遗漏了一些额外的测试容器配置?
我的代理文件是否正确?

deikduxw

deikduxw1#

我就能找出问题所在。

所以工作方案是:

在测试中:

kafka = new KafkaContainer(
                DockerImageName.parse("confluentinc/cp-kafka:7.3.2"))
                .waitingFor(Wait.forLogMessage(".*started.*\\n", 1));
                kafka.withEmbeddedZookeeper()
                .start();

在Jenkins管道中(在maven容器中):

sh('cp src/main/resources/.testcontainers.properties ~/.testcontainers.properties')

src/main/resources/. www.example.com文件的内容testcontainers.properties:

hub.image.name.prefix=harbor-main.test.com/
checks.disable=false

Jenkins代理yaml文件:

apiVersion: v1
kind: Pod
metadata:
  namespace: project-jenkins
spec:
  containers:
    - name: maven
      image: harbor-main.test.com/maven-jdk17-git:latest
      env:
        - name: DOCKER_HOST
          value: unix:///run/user/1000/docker.sock
        - name: DOCKER_TLS_CERTDIR
          value: ""
      command:
        - cat
      tty: true
      volumeMounts:
        - name: maven-cache
          mountPath: /root/.m2/repository
        - name: docker-socket
          mountPath: /var/run
    - name: dind-daemon
      image: harbor-main.test.com/docker:dind-rootless
      env:
        - name: DOCKER_HOST
          value: unix:///run/user/1000/docker.sock
        - name: DOCKER_TLS_CERTDIR
          value: ""
      securityContext:
        privileged: true
        readOnlyRootFilesystem: false
      command:
        - sh
        - -c
        - |
          dockerd-entrypoint.sh &
          sleep 5
          chmod 666 /run/user/1000/docker.sock
          tail -f /dev/null
      tty: true
      volumeMounts:
    - name: docker-socket
      emptyDir: {}
      volumeMounts:
        - name: docker-socket
          mountPath: /run/user/1000
  volumes:
    - name: docker-socket
      emptyDir: {}
    - name: maven-cache
      persistentVolumeClaim:
        claimName: maven-cache

在我的例子中,userID是1000。你可以使用命令获取它:

kubectl -n <namespace> exec -it <podname> -c <containername> -- id -u

我花了几天时间来解决它,希望有人会发现它有用。

相关问题