How to access a container port of another container using kubernetes-plugin in pipeline

3/26/2019

i'm trying to access a exposed port from a container to another container inside the same node, but i can't figure out how to do this using kubernetes plugin in pipeline.

In my script I have created both containers and exposed the port of a database container, in the other container i'm trying to access the port 1521 of a host.

def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
    containerTemplate(name: 'oracle', image: 'repo:5000/ng-oracle:latest',privileged: true, ttyEnabled: true, command: 'cat', ports:[
        portMapping(name: 'oracle1', containerPort: 1521, hostPort: 1521),
        portMapping(name: 'oracle2', containerPort: 22, hostPort: 2222),
    ]),
    containerTemplate(name: 'maven', image: 'repo:5000/ng-satelites:4', ttyEnabled: true, command: 'cat')
  ]) {
        node(label) {
            stage('all') {
                container('maven') {
                    stage('test-db') {
                        sh 'curl $(/sbin/ip route|awk \'/default/ { print $3 }\'):1521'
                    }
                }
            }
        }
}
-- Eric Freitas
jenkins
jenkins-kubernetes
jenkins-pipeline
jenkins-plugins

1 Answer

3/27/2019

Inside a Kubernetes POD, all containers 'see each other' through localhost. Therefore, you should be able to connect to your oracle container from your maven container on localhost:1521.

-- gmc
Source: StackOverflow