首頁 後端開發 Golang DevOpsifying Go Web 應用程式:端到端指南

DevOpsifying Go Web 應用程式:端到端指南

Sep 06, 2024 am 06:39 AM

介紹

在這篇文章中,我將引導您完成基於 Go 的 Web 應用程式的 DevOpsifying 流程。我們將涵蓋從使用 Docker 將應用程式容器化到使用 Helm 將其部署在 Kubernetes 叢集 (AWS EKS) 上、設定與 GitHub Actions 的持續整合以及使用 ArgoCD 自動化部署的所有內容。在本教程結束時,您將擁有一個完全可操作、支援 CI/CD 的 Go Web 應用程式。

先決條件

在開始此專案之前,請確保您符合以下先決條件:

AWS 帳戶:您需要一個有效的 AWS 帳戶來建立和管理 EKS 集群,以部署基於 Go 的應用程式。

DockerHub 帳戶: 您應該有一個 DockerHub 帳戶來推送您的 Docker 映像。

基本 DevOps 知識:熟悉 DevOps 概念和實踐至關重要,包括了解 CI/CD 管道、容器化、編排和雲端部署。

Helm:打包和部署應用程式需要具備 Kubernetes 套件管理器 Helm 的基本知識。

透過滿足這些先決條件,您將做好充分準備按照本指南中的步驟進行操作,並成功對基於 Go 的應用程式進行 DevOpsify!

第 1 步:取得原始碼

要開始使用該項目,您需要從 GitHub 儲存庫克隆原始程式碼。使用以下命令克隆項目:

git clone https://github.com/iam-veeramalla/go-web-app-devops.git
登入後複製

此儲存庫包含使用本指南中所述的 DevOps 實務設定和部署基於 Go 的應用程式所需的所有檔案和配置。複製後,您可以瀏覽以下步驟並依照這些步驟容器化、部署和管理應用程式。

第 2 步:容器化 Go Web 應用程式

第一步是容器化我們的 Go 應用程式。我們將使用多階段 Dockerfile 來建立 Go 應用程式並建立一個輕量級的生產就緒映像。

FROM golang:1.22.5 as build

WORKDIR /app

COPY go.mod .

RUN go mod download

COPY . .

RUN go build -o main .

FROM gcr.io/distroless/base

WORKDIR /app

COPY --from=build /app/main .

COPY --from=build /app/static ./static

EXPOSE 8080

CMD ["./main"]
登入後複製

建置與推送 Docker 映像的指令:

docker login
docker build . -t go-web-app
docker push go-web-app:latest
登入後複製

在此 Dockerfile 中,第一階段使用 Golang 映像來建置應用程式。第二階段使用 distroless 基礎鏡像,它更小、更安全,僅包含執行 Go 應用程式所需的檔案。

步驟 3:使用 AWS EKS 在 Kubernetes 上部署

接下來,我們將把容器化應用程式部署到 Kubernetes 叢集。以下是如何設定叢集和部署應用程式。

建立 EKS 叢集:

eksctl create cluster --name demo-cluster --region us-east-1
登入後複製

部署配置(deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-web-app
  labels:
    app: go-web-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-web-app
  template:
    metadata:
      labels:
        app: go-web-app
    spec:
      containers:
      - name: go-web-app
        image: iamamash/go-web-app:latest
        ports:
        - containerPort: 8080
登入後複製

服務配置(service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: go-web-app
  labels:
    app: go-web-app
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  selector:
    app: go-web-app
  type: ClusterIP
登入後複製

入口配置(ingress.yaml):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: go-web-app
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: go-web-app.local
    http:
      paths: 
      - path: /
        pathType: Prefix
        backend:
          service:
            name: go-web-app
            port:
              number: 80
登入後複製

使用 kubectl 套用設定:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml

登入後複製

設定 Nginx Ingress 控制器:

Kubernetes 中的入口控制器管理對叢集內服務的外部訪問,通常處理 HTTP 和 HTTPS 流量。它提供集中式路由,讓您可以定義流量如何到達您的服務的規則。在這個專案中,我們使用 Nginx Ingress 控制器來有效管理流量並將流量路由到部署在 Kubernetes 叢集中的基於 Go 的應用程式。

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.1/deploy/static/provider/aws/deploy.yaml
登入後複製

第四步:用Helm打包

為了更有效地管理我們的 Kubernetes 資源,我們使用 Helm(Kubernetes 的套件管理器)來打包我們的應用程式。

建立 Helm 圖表:

helm create go-web-app-chart
登入後複製

建立圖表後,用您的deployment.yaml、service.yaml 和 ingress.yaml 檔案取代 templates 目錄中的所有內容。

更新values.yaml:values.yaml 檔案將包含動態值,例如 Docker 映像標籤。標籤將根據 GitHub Actions 執行 ID 自動更新,確保每個部署都是唯一的。

# Default values for go-web-app-chart.
replicaCount: 1

image:
  repository: iamamash/Go-Web-App
  pullPolicy: IfNotPresent
  tag: "10620920515" # Will be updated by CI/CD pipeline

ingress:
  enabled: false
  className: ""
  annotations: {}
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
登入後複製

頭盔部署:

kubectl delete -f k8s/.
helm install go-web-app helm/go-web-app-chart
kubectl get all
登入後複製

第 5 步:與 GitHub Actions 持續集成

為了自動建置和部署我們的應用程序,我們使用 GitHub Actions 設定了 CI/CD 管道。

GitHub Actions 工作流程 (.github/workflows/cicd.yaml):

name: CI/CD

on:
  push:
    branches:
      - main
    paths-ignore:
      - 'helm/**'
      - 'README.md'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Set up Go 1.22
      uses: actions/setup-go@v2
      with:
        go-version: 1.22

    - name: Build
      run: go build -o go-web-app

    - name: Test
      run: go test ./...

  push:
    runs-on: ubuntu-latest
    needs: build
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to DockerHub
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}

    - name: Build and Push action
      uses: docker/build-push-action@v6
      with:
        context: .
        file: ./Dockerfile
        push: true
        tags: ${{ secrets.DOCKERHUB_USERNAME }}/go-web-app:${{github.run_id}}

  update-newtag-in-helm-chart:
    runs-on: ubuntu-latest
    needs: push
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.TOKEN }}

    - name: Update tag in Helm chart
      run: |
        sed -i 's/tag: .*/tag: "${{github.run_id}}"/' helm/go-web-app-chart/values.yaml

    - name: Commit and push changes
      run: |
        git config --global user.email "ansari2002ksp@gmail.com"
        git config --global user.name "Amash Ansari"
        git add helm/go-web-app-chart/values.yaml
        git commit -m "Updated tag in Helm chart"
        git push
登入後複製

To securely store sensitive information like DockerHub credentials and Personal Access Tokens (PAT) in GitHub, you can use GitHub Secrets. To create a secret, navigate to your repository on GitHub, go to Settings > Secrets and variables > Actions > New repository secret. Here, you can add secrets like DOCKERHUB_USERNAME, DOCKERHUB_TOKEN, and TOKEN. Once added, these secrets can be accessed in your GitHub Actions workflows using ${{ secrets.SECRET_NAME }} syntax, ensuring that your sensitive data is securely managed during the CI/CD process.

Step 6: Continuous Deployment with ArgoCD

Finally, we implement continuous deployment using ArgoCD to automatically deploy the application whenever changes are pushed.

Install ArgoCD:

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
kubectl get svc argocd-server -n argocd
登入後複製

Setup ArgoCD Project: To access the ArgoCD UI after setting it up, you first need to determine the external IP of the node where ArgoCD is running. You can obtain this by running the command:

kubectl get nodes -o wide
登入後複製

Next, get the port number at which the ArgoCD server is running using:

kubectl get svc argocd-server -n argocd
登入後複製

Once you have the external IP and port number, you can access the ArgoCD UI by navigating to http://:. For example, if the external IP is 54.161.25.151 and the port number is 30498, the URL to access ArgoCD UI would be http://54.161.25.151:30498.

To log in to the ArgoCD UI for the first time, use the default username admin. The password can be retrieved from the ArgoCD secrets using:

kubectl edit secret argocd-initial-admin-secret -n argocd
登入後複製

Copy the encoded password from the data.password field and decode it using base64:

echo <encoded-password> | base64 --decode
登入後複製

For example, if the encoded password is kjasdfbSNLnlkaW==, decoding it with:

echo kjasdfbSNLnlkaW== | base64 --decode
登入後複製

will provide the actual password. Be sure to exclude any trailing % symbol from the decoded output when using the password to log in.

Now, after accessing the ArgoCD UI, since both ArgoCD and the application are in the same cluster, you can create a project. To do this, click on the "New App" button and fill in the required fields, such as:

  • App Name: Provide a name for your application.
  • Sync Policy: Choose between manual or automatic synchronization.
  • Self-Heal: Enable this option if you want ArgoCD to automatically fix any drift.
  • Source Path: Enter the GitHub repository URL where your application code resides.
  • Helm Chart Path: Specify the path to the Helm chart within your repository.
  • Destination: Set the Cluster URL and namespace where you want the application deployed.
  • Helm Values: Select the appropriate values.yaml file for your Helm chart.

After filling in these details, click on "Create" and wait for ArgoCD to create the project. ArgoCD will pick up the Helm chart and deploy the application to the Kubernetes cluster for you. You can verify the deployment using:

kubectl get all
登入後複製

That's all you need to do!

Conclusion

Congratulations! You have successfully DevOpsified your Go web application. This end-to-end guide covered containerizing your application with Docker, deploying it with Kubernetes and Helm, automating builds with GitHub Actions, and setting up continuous deployments with ArgoCD. You are now ready to manage your Go application with full CI/CD capabilities.

DevOpsifying a Go Web Application: An End-to-End Guide

Feel free to leave your comments and feedback below! Happy DevOpsifying!

Reference

For a detailed video guide on deploying Go applications on AWS EKS, check out this video.

以上是DevOpsifying Go Web 應用程式:端到端指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1673
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
Golang vs. Python:性能和可伸縮性 Golang vs. Python:性能和可伸縮性 Apr 19, 2025 am 12:18 AM

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

Golang和C:並發與原始速度 Golang和C:並發與原始速度 Apr 21, 2025 am 12:16 AM

Golang在並發性上優於C ,而C 在原始速度上優於Golang。 1)Golang通過goroutine和channel實現高效並發,適合處理大量並發任務。 2)C 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

開始GO:初學者指南 開始GO:初學者指南 Apr 26, 2025 am 12:21 AM

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

Golang vs.C:性能和速度比較 Golang vs.C:性能和速度比較 Apr 21, 2025 am 12:13 AM

Golang適合快速開發和並發場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發機制提升性能,適合高並發Web服務開發。 2)C 通過手動內存管理和編譯器優化達到極致性能,適用於嵌入式系統開發。

Golang vs. Python:主要差異和相似之處 Golang vs. Python:主要差異和相似之處 Apr 17, 2025 am 12:15 AM

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。

Golang和C:性能的權衡 Golang和C:性能的權衡 Apr 17, 2025 am 12:18 AM

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

表演競賽:Golang vs.C 表演競賽:Golang vs.C Apr 16, 2025 am 12:07 AM

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

Golang vs. Python:利弊 Golang vs. Python:利弊 Apr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

See all articles