Octopus Deploy K8S 專案 [1] - 地端 K8S

前言

本篇建立 Octopus K8S 專案、設定 Ingress

部署到 develop 地端 K8S 機器
qc 預計部署到 GKE 參考下一篇 w4560000 - Octopus Deploy K8S 專案 [2] - GKE

建立 Octopus 專案


建立 Project

新增 develop Channel

新增 Octopus Variables

K8s.Web.Core.HealthCheckPath = /-/healthz
K8s.Web.Host[OctopusProject_Core_Sample] = octopuscoresampledevelop.leozheng0512.com (develop環境)
K8s.Web.ImageRegistry = [GCP ProjectID]/web/#{Octopus.Project.Name | ToLower}
K8s.Web.Namespace = web
K8s.Web.ProjectSimpleName = #{Octopus.Project.Name | ToLower | Replace “.“ “-“ }
K8s.Web.WebPort = 80

設定 Runbook - Create K8S Namespace Web

Namespace 不需每次上版都要部署(屬於初始設定),設定在 Runbooks 要使用再部署即可


選擇 Run a kubectl CLI Script

建立 namespace web

kubectl create ns web

設定好後 部署至 develop


設定 Runbook - Setup Kubernetes Ingress - Deploy Bare Metal Kubernetes Ingress

Ingress 不需每次上版都要部署(屬於初始設定),設定在 Runbooks 要使用再部署即可

新增 Step

選擇 Deploy raw Kubernetes YAML

也可以選擇 Deploy Kubernetes containers 內的 Ingress 流程
但有一點要注意當你用 Octopus 介面 設定好 Ingress 時
Octopus 會自行生成 YAML 檔,會自行帶入固定值,比如 K8S YAML 檔的 apiVersion
而若 K8S 持續更新,但 Octopus 沒跟上版本的話,apiVersion 由 Octopus 帶入固定值,則部署時會造成異常

而選擇 Deploy raw Kubernetes YAML 則可由我們自己掌控所有 YAML 檔內容,避免 Octopus 因版本問題 帶入錯誤資訊

設定 Deploy Bare Metal Kubernetes Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
  namespace: '#{K8s.Web.Namespace}'
spec:
  ingressClassName: nginx
  rules:
    - host: '#{K8s.Web.Host[OctopusProject_Core_Sample]}'
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service: 
                name: 'octopusproject-core-sample'
                port:
                  number: 80

設定只部署到 develop (目前 qc 還沒建立 GKE)

測試部署 Ingress 至 develop

部署成功

設定 Project Process - 新增 Deploy To Bare Metal Kubernetes Cluster

設定 Project Process 點擊新增 Step

選擇 Deploy Kubernetes containers

設定 StepName、Target roles

設定 CONFIGURE FEATURES (先只勾選 Service 即可)

設定 Deployment

透過設定 ASPNETCORE_ENVIRONMENT 來切分環境

# This YAML exposes the fields defined in the UI. It can be edited directly or have new YAML pasted in.
# Not all available Kubernetes properties are recognized by the form exposed in the UI, and unrecognized properties are ignored during import.
# If the required properties are not supported by this step, the 'Deploy raw Kubernetes YAML' step can be used to deploy YAML directly to Kubernetes, and supports all properties.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: '#{K8s.Web.ProjectSimpleName}'
  labels:
    app: '#{K8s.Web.ProjectSimpleName}'
  namespace: '#{K8s.Web.Namespace}'
spec:
  selector:
    matchLabels:
      octopusexport: OctopusExport
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: '#{K8s.Web.ProjectSimpleName}'
        octopusexport: OctopusExport
    spec:
      containers:
        - name: web-container
          image: '#{K8s.Web.ImageRegistry}'
          ports:
            - name: http
              containerPort: '#{K8s.Web.WebPort}'
              protocol: TCP
          env:
            - name: ASPNETCORE_ENVIRONMENT
              value: '#{Octopus.Environment.Name}'
          livenessProbe:
            httpGet:
              host: ''
              path: '#{K8s.Web.Core.HealthCheckPath}'
              port: '#{K8s.Web.WebPort}'
              scheme: HTTP
          readinessProbe:
            periodSeconds: 15
            httpGet:
              host: ''
              path: '#{K8s.Web.Core.HealthCheckPath}'
              port: '#{K8s.Web.WebPort}'
              scheme: HTTP

設定 Service

Type = ClusterIP,透過 Ingress 轉導

# This YAML exposes the fields defined in the UI. It can be edited directly or have new YAML pasted in.
# Not all available Kubernetes properties are recognized by the form exposed in the UI, and unrecognized properties are ignored during import.
# If the required properties are not supported by this step, the 'Deploy raw Kubernetes YAML' step can be used to deploy YAML directly to Kubernetes, and supports all properties.
apiVersion: v1
kind: Service
metadata:
  name: '#{K8s.Web.ProjectSimpleName}'
  namespace: '#{K8s.Web.Namespace}'
spec:
  type: ClusterIP
  ports:
    - name: http-port
      port: 80
      protocol: TCP
  selector:
    octopusexport: OctopusExport

設定只部署到 develop (目前 qc 還沒建立 GKE)

設定好後 待 Releases 建立版本後 再測試即可


轉載請註明來源,若有任何錯誤或表達不清楚的地方,歡迎在下方評論區留言,也可以來信至 leozheng0621@gmail.com
如果文章對您有幫助,歡迎斗內(donate),請我喝杯咖啡

斗內💰

×

歡迎斗內

github