Gitlab K8 Agent - kubectl commands in CI/CD result in The connection to the server localhost:8080 was refused - did you specify the right host or port

Describe your question in as much detail as possible:

Trying to use to use the gitlab agent (sass gitlab.com account, not self hosted gitlab) in my CI/CD workflow that used to be certificate base (new cluster, and freshly installed agent)

I have followed these instructions: Installing the agent for Kubernetes | GitLab

However, no matter what I do, I get the same error when I try to execute any kubectl within my deploy steps.

  • What are you seeing, and how does that differ from what you expect to see?

I am seeing the following error: " The connection to the server localhost:8080 was refused - did you specify the right host or port" on kubectl commands to the cluster.

What I expect to see, is the commands to be executed within the cluster.

  • Consider including screenshots, error messages, and/or other helpful visuals

I am able to see that KUBECONF is set, to what I believe would be right.
The cluster is in fact ‘connected’ (can see it in Infrastructure->Kubernetes)

  • What version are you on? Are you using self-managed or GitLab.com?

Gitlab.com.

  • Add the CI configuration from .gitlab-ci.yml and other configuration if relevant (e.g. docker-compose.yml)

deploy steps from .gitlab-ci.yml:

deploy:
  stage: deploy
  environment:
    name: production
  image:
    name: bitnami/kubectl:latest
    entrypoint: [""]
  when: manual
  only:
    - master
  script:
    - cat ${KUBECONFIG}
    - kubectl version
    - kubectl cluster-info

KUBECONFIG:

apiVersion: v1
kind: Config
clusters:
- name: gitlab
cluster:
server: https://kas.gitlab.com/k8s-proxy
users:
- name:xxxxxx
user:
token: xxxx
contexts:
- name: xxxxt
context:
cluster: xxxx
user: xxx
  • What troubleshooting steps have you already taken? Can you link to any docs or other resources so we know where you have been?

I’ve tried printing out the KUBECONFIG, i’ve tried passing the kubeconfig to kubectl.

Any hints/ideas?

Thanks.

Does the gitlab agent work properly with gitlab.com sass free edition? Is it restricted to paid tiers? Everything I have tried does not work for me and results in kubectl in my CI/CD pipeline not to work and give the above error.

I was finally able to resolve this issue.

I ended up having to set the KUBE_CONTEXT using an export:

export KUBE_CONTEXT="<user>/<repo>:<agent-name>"

As well, had to set the current context for kubectl:

kubectl config --kubeconfig=${KUBE_CONFIG} use-context <user>/<repo>:<agent-name>