Kubectl: command not found while using bitnami/kubectl in .gitlab-ci.yml

Hello, I am very new to DevOps and I tried to create testing simple docker containerized project, create AWS EKS cluster and deploy while using gitlab agent and .gitlab-ci.yml with this content:

stages:
  - build
  - deploy

variables:
  CONTAINER_IMAGE: git2.abuco.cz:5050/jan.dunder/my-simple-web-app

build:
  stage: build
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker build -t $CONTAINER_IMAGE:latest .
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker push $CONTAINER_IMAGE:latest

deploy:
  stage: deploy
  image:
    name: bitnami/kubectl:latest
    entrypoint: ['']
  script:
    - kubectl config get-contexts
    - kubectl config use-context jan.dunder/my-simple-web-app:honzikuv-agent
    - kubectl set image deployment/my-web-app my-web-app=$CONTAINER_IMAGE:latest
  only:
    - master

The deploy stage is returning an error kubectl: command not found during the pipeline deploy stage as described here:

Running with gitlab-runner 17.2.1 (9882d9c7)
  on l1 LH4r8TXa, system ID: s_8ce298fce05c
Preparing the "shell" executor
00:00
Using Shell (bash) executor...
Preparing environment
00:00
Running on local1...
Getting source from Git repository
00:00
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/builds/LH4r8TXa/0/jan.dunder/my-simple-web-app/.git/
Checking out 6f8d3feb as detached HEAD (ref is master)...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
$ echo "Running deploy stage"
Running deploy stage
$ kubectl version --client
bash: line 157: kubectl: command not found
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit status 1

I was trying to deploy to my kubernetes cluster in AWS EKS and followed the gitlab documentation. The deploy stage for kubernetes is described here: Update your .gitlab-ci.yml file to run kubectl commands.

Hello,

you’re encountering an issue where the kubectl command is not found during the deploy stage of your GitLab CI/CD pipeline. This is happening because kubectl is not installed or not in the PATH within the Docker image you’re using for the deploy stage.

Here’s a detailed approach to resolve this issue:

Updated .gitlab-ci.yml Configuration
Ensure the Correct Docker Image: You are using bitnami/kubectl:latest, which is a good choice, but you might need to ensure the correct version and that it contains the kubectl binary.

Set the kubectl Binary Correctly: The error might be due to kubectl not being in the PATH or not being correctly installed. Make sure the Docker image you are using has kubectl in the PATH.

Update .gitlab-ci.yml: You should make sure that kubectl commands are executed properly in your CI/CD pipeline. Here’s an updated version of your .gitlab-ci.yml that includes installation of kubectl if it’s missing: stages:

  • build
  • deploy

variables:
CONTAINER_IMAGE: git2.abuco.cz:5050/jan.dunder/my-simple-web-app

build:
stage: build
image: docker:latest
services:
- docker:dind
script:
- docker build -t $CONTAINER_IMAGE:latest .
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker push $CONTAINER_IMAGE:latest

deploy:
stage: deploy
image:
name: bitnami/kubectl:latest
entrypoint: [‘’]
before_script:
- kubectl version --client
script:
- kubectl config get-contexts
- kubectl config use-context jan.dunder/my-simple-web-app:honzikuv-agent
- kubectl set image deployment/my-web-app my-web-app=$CONTAINER_IMAGE:latest
only:
- master
Additional Debugging Steps
Verify Docker Image: Check the Docker image bitnami/kubectl:latest to ensure it includes kubectl: docker run --rm bitnami/kubectl:latest kubectl version --client
Check Docker Entry Point: The entrypoint: [‘’] is used to override the default entrypoint of the Docker image. Ensure that this is necessary and correctly configured.

Custom Docker Image: If you continue facing issues, consider creating a custom Docker image that includes both kubectl and any additional tools you need. For example: FROM bitnami/kubectl:latest

Install additional tools or configurations if necessary

And use this custom image in your .gitlab-ci.yml.

Verify kubectl Context: Ensure that the context jan.dunder/my-simple-web-app:honzikuv-agent is correctly set up and available. You might need to set it up in a previous step or verify costcoess its correctness.

Check Permissions: Make sure that your GitLab Runner has access to the Kubernetes cluster and that any authentication or kubeconfig files are properly set up.

By following these steps, you should be able to resolve the kubectl: command not found issue and proceed with deploying your application to AWS EKS.

Hope that helps!