Home > database >  /bin/bash: line 123: kubectl: command not found
/bin/bash: line 123: kubectl: command not found

Time:06-02

This is my first time using GitLab for EKS and I feel so lost. I've been following the docs and so far I

  • Created a project on GitLab that contains my kubernetes manifest files
  • Created a config.yaml in that project in the directory .gitlab/agents/stockagent

Here's the config.yaml, my project name is "Stock-Market-API-K8s" and my k8s manifests are in the root directory of that project

ci_access:
  projects:
    - id: "root/Stock-Market-API-K8s"

In my root directory of my project, I also have a .gitlab-ci.yml file and here's the contents of that

deploy:
  image:
    name: mpriv32/stock-api:latest
    entrypoint: ['']
  script:
    - kubectl config get-contexts
    - kubectl config use-context .gitlab/agents/stockagent
    - kubectl get pods

Using the default example from the docs, it seems that the get-contexts script is the one that failed. Here's the full error from my logs

Executing "step_script" stage of the job script
00:01
Using docker image sha256:58ddf823e9d7ee4c0e75779b7e01dab9b11ac0d985d1b2d2fe6c6b95a849573d for mpriv32/stock-api:latest with digest mpriv32/stock-api@sha256:a2e79a2c3a57327f93e36ec55297a606626e4dc8d72e469dd4dc2f3c1f589bac ...
$ kubectl config get-contexts
/bin/bash: line 123: kubectl: command not found
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

Here's my job.yaml file for my kubernetes pod, just in case it plays a factor at all

apiVersion: v1
kind: Pod
metadata:
  name: stock-api
  labels:
    app: stock-api

spec:
  containers:
  - name: stock-api
    image: mpriv32/stock-api:latest
    envFrom:
    - secretRef:
        name: api-credentials
  restartPolicy: Never

CodePudding user response:

In your case, I guess the image(mpriv32/stock-api:latest) that you are using doesn't have a dependency kubectl as a global executable, please use an image as an example - bitnami/kubectl which "contains" kubectl

deploy:
  image:
    name: bitnami/kubectl

the image keyword is the name of the Docker image the Docker executor uses to run CI/CD jobs. For more information https://docs.gitlab.com/ee/ci/docker/using_docker_images.html

Or you can build your docker image on top of bitnami/kubectl

FROM bitnami/kubectl:1.20.9 as kubectl

FROM ubuntu-or-whatever-image:tag

# Do whatever you need to with the
# ubuntu-or-whatever-image:tag image, then:

COPY --from=kubectl /opt/bitnami/kubectl/bin/kubectl /usr/local/bin/

Or you can go with the approach of building an image from the scratch by installing there the dependencies that you are using

smth like

FROM ubuntu:18.10

WORKDIR /root

COPY bootstrap.sh ./

RUN apt-get update && apt-get -y install --no-install-recommends \
    gnupg \
    curl \
    wget \
    git \
    apt-transport-https \
    ca-certificates \
    zsh \
    && rm -rf /var/lib/apt/lists/*

ENV SHELL /usr/bin/zsh

# Install kubectl
RUN curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \
    echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list && \
    apt-get update && apt-get -y install --no-install-recommends kubectl
  • Related