Gitlab CI - K8s - 部署

时间:2017-01-06 17:49:10

标签: kubernetes gitlab-ci gitlab-ci-runner

在gitlab和k8s gitlab-k8s-cd上阅读本指南,但我的构建在这部分上仍然失败:

- kubectl delete secret registry.gitlab.com
- kubectl create secret docker-registry registry.gitlab.com --docker-server=https://registry.gitlab.com --docker-username=<my_username> --docker-password=$REGISTRY_PASSWD --docker-email=<my_email>

虽然我不完全确定--docker-password需要什么密码,但我在gitlab中为我的用户创建了一个API令牌,我在安全变量中使用它。

这是错误:

$ gcloud container clusters get-credentials deployment
Fetching cluster endpoint and auth data.
kubeconfig entry generated for deployment.
$ kubectl delete secret registry.gitlab.com
Error from server: secrets "registry.gitlab.com" not found
ERROR: Build failed: exit code 1

非常感谢任何帮助。

修改

从初始帖子开始,通过删除初始kubectl delete secret并重新构建工作,所以当没有先前的秘密时,删除失败。

第二次修改

对于K8s的deployment.yml有问题,任何人都可以解释为什么我会收到此错误:

error validating "deployment.yml": error validating data: field spec.template.spec.containers[0].ports[0]: expected object of type map[string]interface{},

使用这个yml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: <app>
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: <app>
    spec:
      containers:
      - name: deployment
        image: registry.gitlab.com/<username>/<app>
        imagePullPolicy: Always
        ports:
        - "80:8080"
        env:
        - name: PORT
          value: "8080"
      imagePullSecrets:
        - name: registry.gitlab.com

这个错误:

error validating "deployment.yml": error validating data: found invalid field imagePullSecrets for v1.Container; if you choose to ignore these errors, turn validation off with --validate=false

使用这个yml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: <app>
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: <app>
    spec:
      containers:
      - name: <app>
        image: registry.gitlab.com/<project>/<app>
        imagePullPolicy: Always
        ports:
        - "80:8080"
        env:
        - name: PORT
          value: "8080"
      imagePullSecrets:
        - name: registry.gitlab.com

最新的YAML

apiVersion: v1
kind: Service
metadata:
  name: <app_name>
  labels:
    app: <app_name>
spec:
  ports:
    - port: 80
  selector:
    app: <app_name>
    tier: frontend
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: <app_name>
  labels:
    app: <app_name>
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: <app_name>
        tier: frontend
    spec:
      containers:
      - image: registry.gitlab.com/<project>/<app>
        imagePullPolicy: Always
        name: <app_name>
        env:
        - name: PORT
          value: "8080"
        imagePullSecrets:
        - name: registry.gitlab.com
        ports:
          - containerPort: 8080
            hostPort: 80

2 个答案:

答案 0 :(得分:1)

关于您的第一个错误:

在Kubernetes中,端口的定义与Docker或Docker Compose中的端口不同。这就是端口规范的样子:

ports:
  - containerPort: 8080
    hostPort: 80

有关详细信息,请参阅reference

关于您的第二个错误:

根据PodSpecs上的referenceimagePullSecrets属性已正确放置在您的示例中。但是,通过阅读错误消息,您似乎实际上将imagePullSecrets属性包含在ContainerSpec中,而不是PodSpec。

在这种情况下,你问题中的YAML似乎是正确的。确保您的实际清单与您问题中的示例相匹配,并且您没有意外地将imagePullSecrets属性缩进到超出必要的范围。

答案 1 :(得分:0)

这是K8s的工作YAML文件:

apiVersion: v1
kind: Service
metadata:
  name: <app_name>
  labels:
    app: <app_name>
spec:
  ports:
    - port: 80
  selector:
    app: <app_name>
    tier: frontend
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: <app_name>
  labels:
    app: <app_name>
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: <app_name>
        tier: frontend
    spec:
      containers:
      - image: registry.gitlab.com/<project>/<app>:latest
        imagePullPolicy: Always
        name: <app_name>
        env:
        - name: PORT
          value: "8080"
        ports:
          - containerPort: 8080
            hostPort: 80
      imagePullSecrets:
        - name: registry.gitlab.com

这也是正在运行的gitlab-ci文件:

image: docker:latest
services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay

stages:
  - package
  - deploy

docker-build:
  stage: package
  script:
  - docker build -t registry.gitlab.com/<project>/<app> .
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  - docker push registry.gitlab.com/<project>/<app>

k8s-deploy:
  image: google/cloud-sdk
  stage: deploy
  script:
  - echo "$GOOGLE_KEY" > key.json
  - gcloud auth activate-service-account --key-file key.json
  - gcloud config set compute/zone <zone>
  - gcloud config set project <project>
  - gcloud config set container/use_client_certificate True
  - gcloud container clusters get-credentials <container-name>
  - kubectl delete secret registry.gitlab.com
  - kubectl create secret docker-registry registry.gitlab.com --docker-server=https://registry.gitlab.com --docker-username=<username> --docker-password=$REGISTRY_PASSWD --docker-email=<user-email>
  - kubectl apply -f deployment.yml

只需要弄清楚如何更改脚本以允许回滚。