首頁 > 後端開發 > Golang > 主體

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

WBOY
發布: 2024-09-06 06:39:02
原創
685 人瀏覽過

介紹

在這篇文章中,我將引導您完成基於 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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!