Localhost:8080 was refused via Jenkins but works via manual execution in server

Dear All,

I am very new to Kubernetes and in the beginning days of learning it .So advance apology If mistaking with some technical terms here. I have a strange issue where i am getting error message as port 8080 is refused while I am running the command via jenkins but it works fine when I run manually.

Command ran:
kubectl apply -f <kubernetes_deployment.yml>

Error in jenkins:
“delta”: “0:00:00.049013”, “end”: “2022-04-26 14:47:53.248479”, “msg”: “non-zero return code”, “rc”: 1, “start”: “2022-04-26 14:47:53.199466”, “stderr”: “The connection to the server localhost:8080 was refused - did you specify the right host or port?”, “stderr_lines”: [“The connection to the server localhost:8080 was refused - did you specify the right host or port?”], “stdout”: “”, “stdout_lines”: }
[WARNING]: Consider using ‘become’, ‘become_method’, and ‘become_user’ rather

Jenkins Running on Ubuntu 18.04.6 LTS on Server 1

Jenkins Version:2.332.2
Jenkins Server: Ubuntu 18.04.6 LTS

Cluster information:

Kubernets running on server 2

Kubernetes version:
Client Version: version.Info{Major:“1”, Minor:“23”, GitVersion:“v1.23.6”, GitCommit:“ad3338546da947756e8a88aa6822e9c11e7eac22”, GitTreeState:“clean”, BuildDate:“2022-04-14T08:49:13Z”, GoVersion:“go1.17.9”, Compiler:“gc”, Platform:“linux/amd64”}
Server Version: version.Info{Major:“1”, Minor:“22”, GitVersion:“v1.22.6”, GitCommit:“07959215dd83b4ae6317b33c824f845abd578642”, GitTreeState:“clean”, BuildDate:“2022-03-30T18:28:25Z”, GoVersion:“go1.16.12”, Compiler:“gc”, Platform:“linux/amd64”}

Cloud being used: (put bare-metal if not on a public cloud) : Microsoft Azure

Installation method: Minikube

Host OS: Ubuntu 20.04.4 LTS

This feels like a template or something, did you want to report a bug?

This feels like ansible

I’m really not sure what you are doing, let alone what the problem is, so no idea how to help.

Hi there,

“it doesn’t work” is always hard to remote debug. Try to always include these 3 things.

  1. what did you try? (Code samples, command lines, screenshots, videos, etc)
  2. what did happen? (Error messages, description, outputs, stuff)
  3. what did you expect to happen?

Otherwise, it’s hard for us to get into your context and you’ll have to have someone who is exactly knowledgeable come along.

Hi Gavin,

Thanks for commenting.
Below is my YML content. As part of this I am deploying .war file to the clusters.


  • hosts: localhost
    become: true
    become_user: root

    tasks:

    • name: Start docker
      service: name=docker state=started

    • name: create docker image using war file
      command: docker build -t souravmyproject1:1.0 .
      args:
      chdir: /opt/apr09cicd

    • name: create tag to image
      command: docker tag souravmyproject1:1.0 edu123/souravmyproject1:1.0

    • name: loginto docker
      command: docker login --username edu123 --password XXXXXXXX

    • name: push image on to dockerhub
      command: docker push edu123/souravmyproject1:1.0

    • name: Do deployment in Docker Swarm cluster
      command: docker service create --name mysvc-ansible --replicas 1 -p 8282:8080 edu123/souravmyproject1:1.0

    • name: remove docker images form ansible server
      command: docker rmi souravmyproject1:1.0 edu123/souravmyproject1:1.0
      ignore_errors: yes

    • name: Do deployment to kuberetes Prod
      command: kubectl apply -f https://raw.githubusercontent.com/majumder-sourav/DevOps_ClassNotes_Edureka/bb3acc4a8efcc5e23b448f5104e1b8b7ab57197c/CICD%20pipeline/kubedeployment.yml

When I am commenting Kubernetes deployment and running the same command manually on my worker/slave node its working fine.
Where as when I am running deployment to Kubernetes through Jenkins its not working .PFB the screenshot

Okay, then my guess as to the problem is that kubectl is trying to read $HOME/.kube/config, and that config says kubernetes is running on localhost:8080, but on the agent your sshing to, localhost:8080 isn’t kubernetes?

This isn’t really a jenkins issue, this is ansible/kubectl failing.

FYI, Proper term is agent

Generally when something works one place, and not the other place, its env variables related. I would recommend dumping your env where it works and where it doesn’t.

In this specific case, on a machine with no ~/.kube/config i get the same error

kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?