kubectl apply -f 适用于 PC 但不适用于 Gitlab Runner

kubectl apply -f works on PC but not in Gitlab Runner

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

$ 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

但是,没有新的 pods。当我从我的电脑 运行 它时,我确实看到了新的 pods 创建。

更新:

运行 kubectl get pods 在 Gitlab 运行 中显示了两个与我在 PC 上看到的不同的 pods。

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

 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 添加了他们自己的 default for namespace:

<project_name>-<project_id>-<environment>

正因为如此,我把它放在了helloworld的元数据部分-deployment.yml:

namespace: helloworld-16393682-production

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

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

kubectl get pods -n helloworld-16393682-production

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

You can permanently save the namespace for all subsequent kubectl commands in that contex

你的情况可能是:

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

或者,如果您正在使用 multiples cluster,您可以使用以下命令在命名空间之间切换:

kubectl config use-context helloworld-16393682-production

在这个link你可以看到很多有用的命令和配置。

希望对您有所帮助! =)