鑷姩鍖栭儴缃叉祦绋

. 纭繚鎮ㄥ凡缁忔纭畨瑁呬簡 GitLab Runner锛屽苟鍦 GitLab 涓婂垱寤轰簡 CI/CD 閰嶇疆鏂囦欢銆傛偍鍙互鍒涘缓涓涓悕涓 .gitlab-ci.yml 鐨勬枃浠讹紝灏嗗叾鏀惧湪鎮ㄧ殑 GitLab 瀛樺偍搴撶殑鏍圭洰褰曚笅銆傚湪璇ユ枃浠朵腑锛屾偍闇瑕佸畾涔変竴涓垨澶氫釜闃舵锛屾瘡涓樁娈甸兘浼氭墽琛屼竴缁勪换鍔℃潵鏋勫缓銆佹祴璇曞拰閮ㄧ讲鎮ㄧ殑搴旂敤绋嬪簭銆備互涓嬫槸涓涓ず渚 .gitlab-ci.yml 閰嶇疆鏂囦欢锛

image: docker:19
services:
  - name: docker:dind
    command: ["--insecure-registry=registry.example.com:5000"]

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - docker build -t registry.example.com:5000/my-app:$CI_COMMIT_SHA .
    - docker push registry.example.com:5000/my-app:$CI_COMMIT_SHA
  artifacts:
    expire_in: 1 week
    paths:
      - Dockerfile
      - src/

test:
  stage: test
  script:
    - docker run --rm registry.example.com:5000/my-app:$CI_COMMIT_SHA ./vendor/bin/phpunit

deploy:
  stage: deploy
  script:
    - helm upgrade --install --namespace my-namespace my-release charts/my-app
  only:
    - master

鍦ㄤ笂闈㈢殑绀轰緥涓紝Docker 鏋勫缓浜嗗簲鐢ㄧ▼搴忛暅鍍忥紝骞舵帹閫佸埌鍚嶄负 registry.example.com:5000 鐨 Docker Registry銆傝鏂囦欢杩樺寘鎷湪 Kubernetes 闆嗙兢涓婁娇鐢 Helm 閮ㄧ讲搴旂敤绋嬪簭鐨勫懡浠ゃ

  1. 瀹夎 Helm銆傛偍鍙互浠 Helm 鐨勫畼鏂圭綉绔欎笂涓嬭浇鏈鏂扮増鏈殑 Helm CLI 骞跺畨瑁呫備竴鏃﹀畨瑁呭畬鎴愶紝鎮ㄥ氨鍙互鍒涘缓 Helm chart 鏂囦欢锛屽湪鍏朵腑瀹氫箟鎮ㄨ閮ㄧ讲鐨勫簲鐢ㄧ▼搴忕殑 Kubernetes 璧勬簮銆

  2. 浣跨敤 Helm 閮ㄧ讲搴旂敤绋嬪簭銆傛偍鍙互浣跨敤浠ヤ笅鍛戒护灏 Helm chart 閮ㄧ讲鍒 Kubernetes 闆嗙兢涓細

helm upgrade --install MY_RELEASE MY_CHART --namespace MY_NAMESPACE --set image.tag=MY_IMAGE_TAG

鍏朵腑锛孧Y_RELEASE 鏄偍瑕侀儴缃茬殑搴旂敤绋嬪簭鐨勫彂琛岀増鏈紝MY_CHART 鏄寘鍚簲鐢ㄧ▼搴 Kubernetes 璧勬簮鐨 Helm chart 鏂囦欢锛孧Y_NAMESPACE 鏄寚瀹氱殑 Kubernetes 鍛藉悕绌洪棿锛孧Y_IMAGE_TAG 鏄簲鐢ㄧ▼搴忕殑闀滃儚鏍囪銆

  1. 閰嶇疆 Ingress 璧勬簮銆傛偍鍙互浣跨敤 Ingress 璧勬簮灏嗘偍鐨勫簲鐢ㄧ▼搴忓叕寮鍒颁簰鑱旂綉锛屽苟涓哄簲鐢ㄧ▼搴忔坊鍔 SSL/TLS 鍔犲瘑绛夊姛鑳姐備互涓嬫槸涓涓ず渚 Ingress YAML 鏂囦欢锛
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - host: mydomain.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-app
            port:
              name: http

鍦ㄨ繖涓ず渚嬩腑锛孖ngress 璧勬簮灏嗗簲鐢ㄧ▼搴忓叕寮鍒 mydomain.com 鐨勬牴璺緞涓嬨傛偍鍙互鏍规嵁闇瑕佸湪 rules 閮ㄥ垎涓坊鍔犳洿澶氱殑涓绘満鍜岃矾寰勮鍒欙紝鎴栦娇鐢ㄥ叾浠 Ingress 鎺у埗鍣ㄧ殑娉ㄩ噴銆

  1. 閰嶇疆 Service 璧勬簮銆傛偍闇瑕佸垱寤轰竴涓 ClusterIP 绫诲瀷鐨 Service 璧勬簮锛屼互渚 Ingress 璧勬簮鑳藉灏嗘祦閲忚矾鐢卞埌璇ラ泦缇 IP銆備互涓嬫槸 Service YAML 鏂囦欢鐨勭ず渚嬶細
apiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    targetPort: 8080

鍦ㄨ繖涓ず渚嬩腑锛孲ervice 璧勬簮灏嗗簲鐢ㄧ▼搴忔毚闇插湪闆嗙兢鐨 IP 鍦板潃涓婄殑绔彛 80 涓娿 targetPort 瀛楁瀹氫箟浜嗗簲鐢ㄧ▼搴忓鍣ㄤ娇鐢ㄧ殑绔彛銆傚鏋滄偍鐨勫簲鐢ㄧ▼搴忓湪瀹瑰櫒涓娇鐢ㄧ殑绔彛涓嶅悓锛岃鏍规嵁闇瑕佷慨鏀规瀛楁銆

  1. 纭繚鎮ㄧ殑鏈嶅姟鍣ㄥ凡瀹夎 kubectl锛屼互渚挎偍鍙互浣跨敤瀹冩潵閮ㄧ讲鍜岀鐞 Kubernetes 璧勬簮銆傚彲浠ヤ粠 Kubernetes 鐨勫畼鏂圭綉绔欎笂涓嬭浇鏈鏂扮増鏈殑 kubectl 骞跺畨瑁呫傚畨瑁呭畬鎴愬悗锛屾偍闇瑕侀厤缃 kubectl 浠ヤ笌 Kubernetes 闆嗙兢杩涜閫氫俊銆

  2. 鏈鍚庯紝鎮ㄩ渶瑕佸垱寤轰竴涓厤鍚堟偍鐨 GitLab CI/CD 閰嶇疆鏂囦欢锛屽寘鍚儴缃 Helm chart 鍜 Kubernetes 璧勬簮鐨勫懡浠ゃ備互涓嬫槸涓涓ず渚嬶細

deploy:
  stage: deploy
  script:
    - kubectl config use-context my-cluster
    - kubectl create namespace my-namespace
    - helm upgrade --install --namespace my-namespace my-release charts/my-app
    - kubectl expose deployment my-app --namespace my-namespace --type=ClusterIP --port=80
    - kubectl apply -f ingress.yaml --namespace my-namespace

鍦ㄨ繖涓ず渚嬩腑锛孋I/CD 閰嶇疆鏂囦欢鍖呮嫭浣跨敤 kubectl 鍛戒护鍒涘缓 Kubernetes 鍛藉悕绌洪棿銆佷娇鐢 Helm 閮ㄧ讲搴旂敤绋嬪簭銆佷互鍙婁娇鐢 kubectl 鍛戒护鍒涘缓 ClusterIP 绫诲瀷鐨 Service 鍜 Ingress 璧勬簮銆

浠ヤ笂灏辨槸鍙兘娑夊強鍒扮殑娴佺▼鍜屾楠わ紝鎮ㄥ彲浠ユ牴鎹偍鐨勫疄闄呮儏鍐佃繘琛岃皟鏁淬傚笇鏈涘鎮ㄦ湁鎵甯姪锛

All posts

Other pages


Deprecated: 鏂囦欢 娌℃湁 comments.php 鐨勪富棰 鑷増鏈 3.0.0 璧峰凡寮冪敤锛屼笖娌℃湁鍙敤鐨勬浛浠c 璇峰湪鎮ㄧ殑涓婚涓寘鍚竴涓 comments.php 妯℃澘銆 in /www/wwwroot/liguoqi.site/wp-includes/functions.php on line 6078

鍙戣〃鍥炲

鎮ㄧ殑鐢靛瓙閭鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢 * 鏍囨敞