Gitlab+豬齒魚 實現自動化部署

Choerodon豬齒魚V0.23版本中的部署 > 應用部署 > 流水線功能在豬齒魚中停用,需要切換爲開發 > 應用流水線功能,相比於老版的流水線,新版本的應用流水線增強了豬齒魚的管理功能,提供了更多的擴展。通過 Gitlab 和 豬齒魚的 DevOps 實現提交代碼後自動更新服務的流程。

前置條件

開發規範

開發人員在特性分支(feature-*)進行功能開發,完成功能開發之後將特性分支合併到環境分支develop進行部署。

image

自動化部署配置

在豬齒魚中配置 K8s Config Map

功能路徑:應用部署 > 資源 > 資源視圖 > 選擇環境 > 配置映射

在配置映射中需要定義CM的名稱,這裏以hzero-dev爲例,在配置映射中維護公共的環境變量,例如註冊中心地址等.

image

調整 Chart 中的配置文件

創建完成配置映射之後,需要在k8s部署時讀取cm配置,這一步需要調整項目下的helm配置。

注意配置的縮進,直接拷貝(Ctrl + V)IDEA會自動調整縮進,請使用Paste as Plain Text保證縮進不會被自動調整。

image

  • charts/hzero-platform/templates/_helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{- /*
service.labels.standard prints the standard service Helm labels.
The standard labels are frequently used in metadata.
*/ -}}

{{- define "service.image" -}}
{{- printf "%s:%s" .Values.image.repository (default (.Chart.Version) .Values.image.tag) -}}
{{- end -}}

{{- define "service.microservice.labels" -}}
choerodon.io/version: {{ default (.Chart.Version) .Values.image.tag }}
choerodon.io/service: {{ .Chart.Name | quote }}
choerodon.io/metrics-port: {{ .Values.deployment.managementPort | quote }}
{{- end -}}

{{- define "service.labels.standard" -}}
choerodon.io/release: {{ .Release.Name | quote }}
{{- end -}}

{{- define "service.match.labels" -}}
choerodon.io/release: {{ .Release.Name | quote }}
{{- end -}}

{{- define "service.logging.deployment.label" -}}
choerodon.io/logs-parser: {{ .Values.logs.parser | quote }}
{{- end -}}

{{- define "service.monitoring.pod.annotations" -}}
choerodon.io/metrics-group: {{ .Values.metrics.group | quote }}
choerodon.io/metrics-path: {{ .Values.metrics.path | quote }}
{{- end -}}

{{/*
Return the appropriate apiVersion for deployment.
*/}}
{{- define "app.deployment.apiVersion" -}}
{{- if semverCompare "<1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "apps/v1beta2" -}}
{{- else -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for statefulset.
*/}}
{{- define "app.statefulset.apiVersion" -}}
{{- if semverCompare "<1.9-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "apps/v1beta2" -}}
{{- else -}}
{{- print "apps/v1" -}}
{{- end -}}
{{- end -}}

{{/*
Return the appropriate apiVersion for ingress.
*/}}
{{- define "app.ingress.apiVersion" -}}
{{- if semverCompare "<1.14-0" .Capabilities.KubeVersion.GitVersion -}}
{{- print "extensions/v1beta1" -}}
{{- else -}}
{{- print "networking.k8s.io/v1beta1" -}}
{{- end -}}
{{- end -}}
  • charts/hzero-platform/templates/deployment.yaml

26~28行定義讀取CMCM的名稱來自於values.yaml

apiVersion: {{ include "app.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  labels:
{{ include "service.labels.standard" . | indent 4 }}
{{ include "service.logging.deployment.label" . | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
{{ include "service.labels.standard" . | indent 6 }}
  template:
    metadata:
      labels:
{{ include "service.labels.standard" . | indent 8 }}
{{ include "service.microservice.labels" . | indent 8 }}
        SERVICE_CODE: {{ .Chart.Name | quote }}
      annotations:
{{ include "service.monitoring.pod.annotations" . | indent 8 }}
    spec:
      containers:
        - name: {{ .Release.Name }}
          image: "{{ .Values.image.repository }}:{{ .Chart.Version }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
          - configMapRef:
              name: {{ .Values.configMap.name }}
          env:
{{- range $name, $value := .Values.env.open }}
{{- if not (empty $value) }}
          - name: {{ $name | quote }}
            value: {{ $value | quote }}
{{- end }}
{{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          # readinessProbe:
          #   httpGet:
          #     path: /health
          #     port: {{ .Values.deployment.managementPort }}
          #     scheme: HTTP
          readinessProbe:
            exec:
              command:
              - curl
              - localhost:{{ .Values.deployment.managementPort }}/actuator/health
            failureThreshold: 3
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 10
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
          - mountPath: /Charts
            name: data
{{- if not (empty .Values.persistence.subPath) }}
            subPath: {{ .Values.persistence.subPath }}
{{- end }}
      volumes:
      - name: data
        {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim | default ( .Release.Name ) }}
        {{- else }}
        emptyDir: {}
        {{- end }}
  • charts/hzero-platform/values.yaml

6~7行定義了CM的名稱,這裏需要和豬齒魚配置映射的名稱匹配。

# Default values for hzero-platform.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
configMap:
  name: hzero-dev

image:
  repository: registry.choerodon.com.cn/hzero-hzero/hzero-platform
  pullPolicy: Always

env:
  open:
    # 數據庫地址
    SPRING_DATASOURCE_URL: jdbc:mysql://db.hzero.org:3306/hzero_platform?useUnicode=true&characterEncoding=utf-8&useSSL=false
    # Redis DB
    SPRING_REDIS_DATABASE: 1
metrics:
  path: /actuator/prometheus
  group: spring-boot

logs:
  parser: spring-boot

persistence:
  enabled: false

service:
  enabled: false
  type: ClusterIP
  port: 8100
  name: hzero-platform
deployment:
  managementPort: 8101

resources:
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources,such as Minikube. If you do want to specify resources,uncomment the following
  # lines,adjust them as necessary,and remove the curly braces after 'resources:'.
  limits:
    # cpu: 100m
    memory: 1.7Gi
  requests:
    # cpu: 100m
    memory: 1.2Gi

創建部署配置

功能路徑:部署 > 應用部署 > 資源 > 創建部署配置

在部署配置選擇對應服務,如果項目中有values.yaml文件,這裏會自動帶出。將環境相關的信息替換完成後保存。

image

創建應用流水線

功能路徑:開發 > 應用流水線

在應用流水線中創建流水線,基本的流程至少需要包含構建、發佈和部署三個流程,代碼檢查是可選的部分。

image

代碼檢查配置

選擇需要檢查的代碼分支,檢查類型分爲兩類,SonarMaven用來檢查Maven項目,SonarScanner用來檢查通用項目,自定義配置用來配置私有的Sonar服務信息。

image

構建配置

構建這一步主要執行Maven編譯打包和Docker鏡像構建,共享目錄設置定義是否使用緩存,比如歷史拉取的Maven資源等等。

image

Maven構建這一步,沒有特殊需要,一般只需要執行

mvn package -Dmaven.test.skip=true -U -B

Docker構建這一步需要配置一些文件路徑,Dockerfile文件路徑需要填寫文件的相對路徑,鏡像構建上下文是執行鏡像打包命令的目錄,一般是構建產物所在的目錄,例如Maven默認構建的包所在的目錄爲target目錄,這裏填寫的是構建產出物的相對路徑。

image

生效“應用流水線”

在上一步創建完應用流水線之後,如下所示:

image

**注意:**如果你的目標分支不是master還需要做一些手工處理,需要手工將master分支的.gitlab-ci.yml文件複製或者合併到目標分支

測試自動化部署

完成所有配置之後,在項目中提交代碼或者直接在應用流水線中選擇全新執行,然後在豬齒魚的應用流水線中和 Gitlab 的CI/CD > Pipelines中可以看到編譯和鏡像打包步驟的執行。

image

在部署節點執行完成之後,在部署 > 應用部署 > 資源中查看相關的服務可以看到實例的替換。

觸發器部署

在實際項目開發過程中,可能存在部分基礎組件項目,這些項目不會直接部署到服務器,而是需要打包成可以來的JarMaven倉庫中,然後再部署依賴了此模塊的項目,爲了解決這類的需求,需要藉助Gitlab的觸發器來實現。

image

創建觸發器

如果項目依賴了其他的模塊,需要在模塊代碼更新之後自動部署,可以在Gitlab中創建觸發器。

image

觸發觸發器

新建好觸發器之後,複製下方提示中的腳本到被依賴模塊項目的應用流水線的自定義階段。

image

自定義CI配置:

image

如果需要將當前工程部署到Maven倉庫供其他項目拉取,在CIMaven構建步驟中選擇自定義倉庫配置

image

然後執行打包命令

mvn clean deploy -Dmaven.test.skip=true -Dmaven.springboot.skip=true -Dmaven.javadoc.skip=true -U -B -s settings.xml

完整的應用流水線配置如下所示:

image

關於豬齒魚

Choerodon 豬齒魚作爲開源多雲應用敏捷全鏈路技術平臺,是基於開源技術Kubernetes,Istio,knative,Gitlab,Spring Cloud來實現本地和雲端環境的集成,實現企業多雲/混合雲應用環境的一致性。平臺通過提供精益敏捷、持續交付、容器環境、微服務、DevOps等能力來幫助組織團隊來完成軟件的生命週期管理,從而更快、更頻繁地交付更穩定的軟件。

更多內容

大家可以通過以下社區途徑瞭解Choerodon豬齒魚文檔、最新動態、產品特性:

【Choerodon官網】

https://choerodon.io/zh/

【漢得開放平臺】

https://open.hand-china.com/

【漢得開放論壇】

https://openforum.hand-china.com/

也可加入Choerodon豬齒魚官方社區用戶交流羣,交流豬齒魚使用心得、Docker、微服務、K8S、敏捷管理等相關理論實踐心得,羣同步更新版本更新等信息,大家可以加羣討論交流。

①-Choerodon豬齒魚官方交流(已滿);

②-Choerodon豬齒魚官方交流(可加);【微信號發至客服郵箱[email protected],運營小夥伴拉您入官方交流羣】

歡迎加入Choerodon豬齒魚社區,共同爲企業數字化服務打造一個開放的生態平臺。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章