[英]How to deploy staging in Google Cloud Platform with Kubernetes and Gitlab CI/CD?
我最近與Docker
, Kubernetes
, Google Cloud Platform(GCP)
和Gitlab
一起玩,以實現從commit
到staging
CI/CD
。 到目前為止,我已成功testing
, building
並將映像pushing
送到Container registry of Gitlab
。
我有一個小節點和docker應用程序,輸出'Hello world'
。 另外,我已經在Container registry of Gitlab
。 目前,該過程為docker-in-docker。 我想將我的映像從Gitlab container registry
推Kubernetes engine
GCP中的Kubernetes engine
。 我已經安裝了kubectl
和gcloud 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,它為您的應用程序創建了可在多個環境中重復使用的安裝圖表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.