如何使用Kubernetes和Gitlab CI / CD在Google Cloud Platform中部署登台?

sur*_*w55 2 node.js docker google-cloud-platform gitlab-ci kubernetes

我摆弄DockerKubernetesGoogle Cloud Platform(GCP)Gitlab最近实现CI/CDcommitstaging。到目前为止,我已经成功testingbuilding并且pushing将图像Container registry of Gitlab

我有一个输出的小节点和docker应用程序'Hello world'。另外,我还在中构建了docker镜像Container registry of Gitlab。目前,该过程为docker-in-docker。我想在GCP中将图片从推Gitlab container registry送到Kubernetes engine。我已经安装了kubectlgcloud sdk。该汽车的DevOps似乎很有前途,但我想实现我自己的.gitlab-ci.yml文件。

这是我的.gitlab-ci.yml下面:

stages:
  - testing
  - build
  - staging

variables:
  CONTAINER_TEST_IMAGE: registry.gitlab.com/surajneupane55/node-app-
  testing
 CONTAINER_RELEASE_IMAGE: registry.gitlab.com/surajneupane55/node-
 app-testing:latest


test:
  stage: testing
  image: node:boron
  script:
  - npm install
  - npm test


build_image:
  stage: build
  only: [master]
  image: docker:git
  services:
    - docker:dind
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN 
      registry.gitlab.com/surajneupane55
    - docker build -t $CONTAINER_TEST_IMAGE .
    - docker push $CONTAINER_TEST_IMAGE


staging_site:

//I need help here!!!
//For staging my project in Kubernetes cluster in GCP
//Already created node-app Kubernetes cluster
Run Code Online (Sandbox Code Playgroud)

请让我知道我的方法是否错误,因为这是我在CI / CD的第一个学习项目。

sur*_*w55 5

使用gitlab-ci.yml.NET在GKE中构建和部署的简单文件Google Container Registry(GCR)。首先,我们构建图像并将其推送到GCR。使用有效的凭据,我们可以轻松地将GKE与GCR连接并进行部署。

stages:
  - build
  - deploy

variables:
  CONTAINER_TEST_IMAGE: gcr.io/node-testing-189614/node-testing:latest




build_image:
  stage: build
  only: [master]
  image: google/cloud-sdk
  services:
    - docker:dind

  script:
    - echo "$GOOGLE_KEY_SECRET" > key.json # Google Cloud service accounts
    - gcloud auth activate-service-account --key-file key.json
    - gcloud config set project node-testing-189614
    - gcloud container builds submit -t $CONTAINER_TEST_IMAGE .




deploy_staging:
  image: google/cloud-sdk
  stage: deploy
  script:
    - echo "$GOOGLE_KEY_SECRET" > key.json # Google Cloud service accounts
    - gcloud auth activate-service-account --key-file key.json
    - gcloud config set project node-testing-189614
    - gcloud config set compute/zone europe-west1-b
    - gcloud config set container/use_client_certificate True
    - gcloud container clusters get-credentials node-testing
    - kubectl delete pods --all
    - kubectl apply -f staging.yml

  environment:
    name: staging
    url: http://***.***.**.***:****/ //External IP from Kubernetes
  only:
  - master
Run Code Online (Sandbox Code Playgroud)

在上方,我们删除GKE中的Pod,因为我们一直想用最新的标签更新图像。到目前为止,最好的解决方案是删除Pod,并让staging.yml文件创建一个新的Pod(如果没有)。

最后staging.yml看起来像这样:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: node-testing
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: node-testing
    spec:
      containers:
      - name: node-testing
        image: gcr.io/node-testing-189614/node-testing:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
      imagePullSecrets:
        - name: gcr.io/node-testing-189614/node-testing
Run Code Online (Sandbox Code Playgroud)