/bin/bash:第 117 行:kubectl:找不到命令 gitlab-ci [英] /bin/bash: line 117: kubectl: command not found gitlab-ci

查看:211
本文介绍了/bin/bash:第 117 行:kubectl:找不到命令 gitlab-ci的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 gitlab-ci.yml 文件中使用 kubectl 命令.

I am not able to use kubectl command inside gitlab-ci.yml file.

我已经完成了文档中提到的步骤,在 doc.

I have already gone through the steps mentioned in the doc to add an existing cluster in the doc.

他们没有提到,我该如何使用 kubectl.

Nowhere they have mentioned, How can I use kubectl.

我尝试了以下配置.

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

但我遇到了错误,

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

显然,它无法连接到集群,或者尝试连接到此 roffe/kubectl 镜像容器内的集群.

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.

请建议我如何部署到现有的 k8s 集群.

Please suggest how I can deploy to the existing k8s cluster.

更新

我浏览了这个 doc我在我的 gitlab-ci.yml 中使用定义的变量来更新 kubectl 的上下文.

I went through this doc and I am using defined variables in my gitlab-ci.yml to update the context of the kubectl.

但还是不行.

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

我得到的错误,

$ 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

推荐答案

要使用现有集群自动部署,您需要执行以下步骤:

To automate deployment with an existing cluster, You need to follow below steps:

1.将您的集群添加到 gitlab 项目.

关注 此文档,并添加您现有的集群

Follow this doc, and add your existing cluster

2.构建您的项目并推送到 docker 或任何注册表

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

3.将您的 deployment.yml 应用到集群

要在 gitlab-ci.yml 中使用 kubectl,您需要一个具有 kubectl 的图像.你用过的那个就可以了.

To use kubectl inside gitlab-ci.yml, you need an image that has kubectl. The one that you have used will work.

但是容器内的 kubectl 不知道你之前添加的集群的上下文.

But the kubectl inside the container has no idea about the context of cluster that you added earlier.

现在这就是 gitlab 的环境变量发挥作用的地方了.

Now this is where gitlab's envrionment variable play their role.

默认环境范围是 *,这意味着所有作业,无论其环境如何,都使用该集群.每个范围只能由项目中的单个集群使用,否则会发生验证错误.此外,没有设置环境关键字的作业无法访问任何集群.看这里

The default environment scope is *, which means all jobs, regardless of their environment, use that cluster. Each scope can be used only by a single cluster in a project, and a validation error occurs if otherwise. Also, jobs that don’t have an environment keyword set can’t access any cluster. see here

因此,当您添加集群时,默认情况下它会在 * 范围内,并且如果它使用某种环境,它将被传递给每个作业.

So, when you added your cluster, it goes by default in scope * and will be passed to every job provided it uses some environment.

另外,当您创建集群时,gitlab 默认为该集群创建环境变量,看这里.

Also, When you create a cluster, gitlab create environment variables for that cluster by default, see here.

需要注意的重要一点是它还添加了一个环境变量KUBECONFIG.

Important thing to notice is that it also adds an environment variable KUBECONFIG.

为了访问您的 Kubernetes 集群,kubectl 使用了一个配置文件.默认的 kubectl 配置文件位于 ~/.kube/config 并被称为 kubeconfig 文件.

In order to access your Kubernetes cluster, kubectl uses a configuration file. The default kubectl configuration file is located at ~/.kube/config and is referred to as the kubeconfig file.

kubeconfig 文件组织有关集群、用户、命名空间和身份验证机制的信息.kubectl 命令使用这些文件来查找选择集群并与之通信所需的信息.

kubeconfig files organize information about clusters, users, namespaces, and authentication mechanisms. The kubectl command uses these files to find the information it needs to choose a cluster and communicate with it.

加载顺序遵循以下规则:

The loading order follows these rules:

  • 如果设置了 --kubeconfig 标志,则只加载给定的文件.该标志只能设置一次,不会发生合并.

  • If the --kubeconfig flag is set, then only the given file is loaded. The flag may only be set once and no merging takes place.

如果设置了 $KUBECONFIG 环境变量,则根据系统的正常路径分隔规则将其解析为文件系统路径列表.

If the $KUBECONFIG environment variable is set, then it is parsed as a list of filesystem paths according to the normal path delimiting rules for your system.

否则使用${HOME}/.kube/config文件,不进行合并.

Otherwise, the ${HOME}/.kube/config file is used and no merging takes place.

所以,kubectl 命令可以使用变量KUBECONFIG 来设置上下文看这里.

So, kubectl command can use the variable KUBECONFIG to set the context see here.

因此,您的部署工作将如下所示,

So, your deployment job will be like below,

deploy-prod:
  stage: deploy
  image: roffe/kubectl
  script:
    - kubectl get pods
    - kubectl get all
    - kubectl get namespaces
    - kubectl apply -f scheduler-deployment.yaml
  environment:
    name: production
    kubernetes:
      namespace: default
  only:
    - master

您还可以使用 environment.kubernetes.namespace

这篇关于/bin/bash:第 117 行:kubectl:找不到命令 gitlab-ci的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆