如何通过Groovy与Kubernetes插件在Jenkins Pipeline中运行Kafka?
Alright, let's walk through how to extend your Jenkins Pipeline now that you've got the Ubuntu trusty container set up with Git, Go 1.9, and your project code pulled in. Since you've included PostgreSQL 9.6 and Kafka in your pod template, I'll cover the most common next steps: interacting with those services, running tests, and building your Go application.
Since all containers in a Kubernetes Pod share the same network namespace, you can use the PostgreSQL container's name (postgres) directly as the hostname for connections. First, let's add PostgreSQL client tools to your Ubuntu container, then test the connection and run database-related tests.
Here's the updated Pipeline code snippet:
pipeline { agent { kubernetes { podTemplate(containers: [ containerTemplate(name: 'ubuntu', image: 'ubuntu:trusty', ttyEnabled: true, command: 'cat'), containerTemplate(name: 'postgres', image: 'postgres:9.6', envVars: [ envVar(key: 'POSTGRES_USER', value: 'app_user'), envVar(key: 'POSTGRES_PASSWORD', value: 'app_pass'), envVar(key: 'POSTGRES_DB', value: 'app_db') ]), // Don't forget Zookeeper—Kafka requires it! containerTemplate(name: 'zookeeper', image: 'wurstmeister/zookeeper:latest'), containerTemplate(name: 'kafka', image: 'wurstmeister/kafka:latest', envVars: [ envVar(key: 'KAFKA_ADVERTISED_LISTENERS', value: 'PLAINTEXT://kafka:9092'), envVar(key: 'KAFKA_ZOOKEEPER_CONNECT', value: 'zookeeper:2181') ]) ]) } } stages { stage('Finalize Dependencies & Pull Code') { steps { container('ubuntu') { sh ''' apt-get update && apt-get install -y git golang-1.9 postgresql-client // Configure Go environment consistently export GOPATH=/workspace/go export PATH=$PATH:/usr/lib/go-1.9/bin:$GOPATH/bin mkdir -p $GOPATH/src // Pull your project code git clone https://github.com/your-username/your-go-project.git $GOPATH/src/your-project ''' } } } stage('Test PostgreSQL Connection & Run DB Tests') { steps { container('ubuntu') { sh ''' export GOPATH=/workspace/go export PATH=$PATH:/usr/lib/go-1.9/bin:$GOPATH/bin // Verify connection to Postgres psql -h postgres -U app_user -d app_db -c "SELECT version();" // Run your Go project's database tests cd $GOPATH/src/your-project go test ./db -v ''' } } } } }
Key notes here:
- We added
postgresql-clientto the Ubuntu container to runpsqlcommands. - The Postgres container uses env vars to initialize a default user, password, and database—adjust these to match your project's config.
For Kafka interactions, use the kafka container name as the hostname (port 9092, as set in KAFKA_ADVERTISED_LISTENERS). If you need to debug or set up test topics, you can install Kafka's command-line tools temporarily:
stage('Set Up Kafka & Run Messaging Tests') { steps { container('ubuntu') { sh ''' export GOPATH=/workspace/go export PATH=$PATH:/usr/lib/go-1.9/bin:$GOPATH/bin // Install dependencies for Kafka tools apt-get install -y openjdk-7-jre wget // Grab Kafka binaries (match your Kafka version if needed) wget https://archive.apache.org/dist/kafka/2.1.0/kafka_2.11-2.1.0.tgz tar xzf kafka_2.11-2.1.0.tgz // Create a test topic for your app kafka_2.11-2.1.0/bin/kafka-topics.sh --create --bootstrap-server kafka:9092 --replication-factor 1 --partitions 1 --topic test-events // Run Kafka-related tests in your Go project cd $GOPATH/src/your-project go test ./messaging -v ''' } } }
Once tests pass, build your Go binary and archive it for deployment:
stage('Build Go Application') { steps { container('ubuntu') { sh ''' export GOPATH=/workspace/go export PATH=$PATH:/usr/lib/go-1.9/bin:$GOPATH/bin cd $GOPATH/src/your-project // Build a static Linux binary (works across most distros) CGO_ENABLED=0 go build -o my-app ./cmd/main.go // Package it for easy distribution tar czf my-app.tar.gz my-app ''' } } post { success { archiveArtifacts artifacts: 'my-app.tar.gz', fingerprint: true } } }
- Zookeeper is Mandatory: Wurstmeister's Kafka image won't start without a Zookeeper instance—always include it in your pod template.
- Consistent Go Env: Define
GOPATHandPATHin every stage that uses Go, or set them in a top-levelenvironmentblock to avoid repetition. - TTY for Ubuntu: The
ttyEnabled: trueandcommand: 'cat'keeps the Ubuntu container running so Jenkins can execute commands inside it.
内容的提问来源于stack exchange,提问作者Matheus Felipe

