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

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

问题描述

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

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.

他们没有提到任何地方,如何使用 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.

但是仍然无法正常工作.

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

我得到的错误

$ 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的envrionment变量发挥作用的地方.

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

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

因此,当您添加集群时,默认情况下它将进入范围 * ,并且将在使用某些环境的情况下将其传递给每个作业.

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天全站免登陆