I am not able to use kubectl command inside gitlab-ci.yml file.
I have already gone through the steps mentioned in the doc to add an existing cluster in the doc.
Nowhere they have mentioned, How can I use kubectl.
I tried the below configuration.
stages:
- docker-build
- deploy
docker-build-master:
image: docker:latest
stage: docker-build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE:prod" .
- docker push "$CI_REGISTRY_IMAGE:prod"
only:
- master
deploy-prod:
stage: deploy
image: roffe/kubectl
script:
- kubectl apply -f scheduler-deployment.yaml
only:
- master
But I am getting below error,
Executing "step_script" stage of the job script
00:01
Using docker image sha256:c8d24d490701efec4c8d544978b3e4ecc4855475a221b002a8f9e5e473398805 for roffe/kubectl with digest roffe/kubectl@sha256:ba13f8ffc55c83a7ca98a6e1337689fad8a5df418cb160fa1a741c80f42979bf ...
$ kubectl apply -f scheduler-deployment.yaml
error: unable to recognize "scheduler-deployment.yaml": Get http://localhost:8080/api?timeout=32s: dial tcp [::1]:8080: connect: connection refused
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1
Clearly, it is not able to connect to the cluster, or maybe trying to connect to the cluster inside this roffe/kubectl image container.
When I remove the image, I get this error.
/bin/bash: line 117: kubectl: command not found
I have gone through the whole doc I couldn't find a single example or reference that explains this part.
Please suggest how I can deploy to the existing k8s cluster.
Update
I went through this doc and I am using defined variables in my gitlab-ci.yml to update the context of the kubectl.
But still it doesn't work.
deploy-prod:
stage: deploy
image: roffe/kubectl
script:
- echo $HOME
- echo $KUBECONFIG
- echo $KUBE_URL
- mkdir -p $HOME/.kube
- echo -n $KUBECONFIG | base64 -d > $HOME/.kube/config
- kubectl get pods
only:
- master
Error that I get,
$ echo $HOME
/root
$ echo $KUBECONFIG
$ echo $KUBE_URL
$ mkdir -p $HOME/.kube
$ echo -n $KUBECONFIG | base64 -d > $HOME/.kube/config
$ kubectl get pods
The connection to the server localhost:8080 was refused - did you specify the right host or port?
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1