首页 > 解决方案 > kubectl apply -f 适用于 PC,但不适用于 Gitlab Runner

问题描述

我正在尝试使用 Gitlab CICD 部署到 kubernetes。无论我做什么,kubectl apply -f helloworld-deployment.yml --record在我.gitlab-ci.yml总是返回部署不变:

$ kubectl apply -f helloworld-deployment.yml --record
 deployment.apps/helloworld-deployment unchanged

即使我更改了图像上的标签,或者部署根本不存在。但是,如果我kubectl apply -f helloworld-deployment.yml --record从自己的计算机上运行,​​它可以正常工作并在标签更改时更新,并在不存在部署时创建部署。以下是我.gitlab-ci.yml正在测试的:

image: docker:dind
services:
    - docker:dind

stages:
    - deploy

deploy-prod:
    stage: deploy
    image: google/cloud-sdk
    environment: production
    script:
        - kubectl apply -f helloworld-deployment.yml --record

下面是helloworld-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
    name: helloworld-deployment
spec:
    replicas: 2
    selector:
        matchLabels:
            app: helloworld
    template:
        metadata:
            labels:
                app: helloworld
        spec:
            containers:
                - name: helloworld
                  image: registry.gitlab.com/repo/helloworld:test
                  imagePullPolicy: Always
                  ports:
                  - containerPort: 3000
            imagePullSecrets:
                - name: regcred

更新:

kubectl rollout history deployments/helloworld-deployment如果我运行并且没有现有部署,这就是我看到的:

Error from server (NotFound): deployments.apps "helloworld-deployment" not found

如果部署已经存在,我会看到:

REVISION  CHANGE-CAUSE
1         kubectl apply --filename=helloworld-deployment.yml --record=true

只有一次修订。

这次我确实注意到,当我更改标签时,我的 Gitlab Runner 的输出是:

deployment.apps/helloworld-deployment configured

但是,没有新的豆荚。当我从我的 PC 上运行它时,我确实看到创建了新的 pod。

更新:

运行kubectl get pods在 Gitlab 运行器中显示两个不同的 pod,而不是我在我的 PC 上看到的。

我肯定只有一个 Kubernetes 集群,但kubectl config view显示了一些差异(服务器 url 相同)。的输出contexts显示不同的命名空间。这是否意味着我需要在我的yml文件中设置命名空间或在命令中传递它?这是 Gitlab 运行器的输出:

 apiVersion: v1
 clusters:
 - cluster:
     certificate-authority-data: DATA+OMITTED
     server: URL
   name: gitlab-deploy
 contexts:
 - context:
     cluster: gitlab-deploy
     namespace: helloworld-16393682-production
     user: gitlab-deploy
   name: gitlab-deploy
 current-context: gitlab-deploy
 kind: Config
 preferences: {}
 users:
 - name: gitlab-deploy
   user:
     token: [MASKED]

这是我电脑的输出:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: URL
contexts:
- context:
    cluster: do-nyc3-helloworld
    user: do-nyc3-helloworld-admin
  name: do-nyc3-helloworld
current-context: do-nyc3-helloworld
kind: Config
preferences: {}
users:
- name: do-nyc3-helloworld-admin
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1beta1
      args:
      - kubernetes
      - cluster
      - kubeconfig
      - exec-credential
      - --version=v1beta1
      - --context=default
      - VALUE
      command: doctl
      env: null

看起来 Gitlab为命名空间添加了自己的默认值

<project_name>-<project_id>-<environment>

因此,我将其放在 helloworld-deployment.yml 的元数据部分:

namespace: helloworld-16393682-production

然后它按预期工作。它之前正在部署,但kubectl get pods由于该命令正在使用命名空间,因此没有显示它default

标签: kubernetesgitlabgitlab-cigitlab-ci-runnerkubernetes-deployment

解决方案


由于 Gitlab 使用自定义命名空间,因此您需要在命令中添加命名空间标志以显示您的 pod:

kubectl get pods -n helloworld-16393682-production

您可以为 kubectl 命令设置默认命名空间。见这里

您可以为该上下文中的所有后续 kubectl 命令永久保存命名空间

在您的情况下,它可能是:

kubectl config set-context --current --namespace=helloworld-16393682-production

或者,如果您使用多个集群,您可以使用以下命令在命名空间之间切换:

kubectl config use-context helloworld-16393682-production

在此链接中,您可以看到许多有用的命令和配置。

我希望它有帮助!=)


推荐阅读