繁体   English   中英

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

[英]How to deploy staging in Google Cloud Platform with Kubernetes and Gitlab CI/CD?

我最近与DockerKubernetesGoogle Cloud Platform(GCP)Gitlab一起玩,以实现从commitstaging CI/CD 到目前为止,我已成功testingbuilding并将映像pushing送到Container registry of Gitlab

我有一个小节点和docker应用程序,输出'Hello world' 另外,我已经在Container registry of Gitlab 目前,该过程为docker-in-docker。 我想将我的映像从Gitlab container registryKubernetes engine GCP中的Kubernetes engine 我已经安装了kubectlgcloud sdk Auto 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

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

一个简单的gitlab-ci.yml文件,可使用Google Container Registry(GCR)在GKE中构建和部署。 首先,我们构建图像并将其推送到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

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

最后, 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

Yu不需要实际将图像存储在GCR中就可以在您的GKE中使用它,尽管在附近有它很好。 您需要在gcloud上有一个服务帐户,这样您才能对GCR进行不过期的身份验证(或者您需要使用gcloud cli对GCR进行身份验证),然后只需标记图像并将其推送即可。

在kubernetes上运行它是另外一回事了,我强烈建议您也了解一下Helm,它为您的应用程序创建了可在多个环境中重复使用的安装图表。

如果遵循GitOps方法,则可以将配置与CI分离,以使其更可靠,更安全。

请看一下我对另一个非常相似的问题的回答

有关更高级的概述,请参见:


免责声明:我是Kubernetes贡献者和Weaveworks员工。 我们构建了开源和商业工具,可帮助人们更快地使用Kubernetes进行生产。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM