Go Web アプリケーションの DevOpsifying: エンドツーエンド ガイド
導入
この投稿では、Go ベースの Web アプリケーションの DevOpsifying プロセスについて説明します。 Docker を使用したアプリケーションのコンテナ化から、Helm を使用した Kubernetes クラスター (AWS EKS) へのデプロイ、GitHub Actions との継続的インテグレーションのセットアップ、ArgoCD を使用したデプロイの自動化まで、すべてをカバーします。このチュートリアルを終了するまでに、完全に動作する CI/CD 対応の Go Web アプリケーションが完成します。
前提条件
このプロジェクトを開始する前に、次の前提条件を満たしていることを確認してください:
AWS アカウント: Go ベースのアプリケーションをデプロイするための EKS クラスターを作成および管理するには、アクティブな AWS アカウントが必要です。
DockerHub アカウント: Docker イメージをプッシュするには、DockerHub アカウントが必要です。
DevOps の基本知識: CI/CD パイプライン、コンテナ化、オーケストレーション、クラウド展開の理解など、DevOps の概念と実践に精通していることが不可欠です。
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 イメージを使用してアプリケーションを構築します。第 2 段階では、ディストリビューションのないベース イメージを使用します。これは、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 構成 (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 の Ingress コントローラーは、クラスター内のサービスへの外部アクセスを管理し、通常は 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
ステップ 4: Helm を使用したパッケージ化
Kubernetes リソースをより効果的に管理するために、Kubernetes のパッケージ マネージャーである Helm を使用してアプリケーションをパッケージ化します。
Helm チャートを作成します:
helm create go-web-app-chart
チャートを作成した後、テンプレート ディレクトリ内のすべてを、deployment.yaml、service.yaml、および ingress.yaml ファイルに置き換えます。
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
Helm デプロイメント:
kubectl delete -f k8s/. helm install go-web-app helm/go-web-app-chart kubectl get all
ステップ 5: GitHub アクションとの継続的統合
アプリケーションのビルドとデプロイを自動化するために、GitHub Actions を使用して CI/CD パイプラインをセットアップしました。
GitHub アクション ワークフロー (.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://
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.
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.
以上がGo Web アプリケーションの DevOpsifying: エンドツーエンド ガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Golangは、パフォーマンスとスケーラビリティの点でPythonよりも優れています。 1)Golangのコンピレーションタイプの特性と効率的な並行性モデルにより、高い並行性シナリオでうまく機能します。 2)Pythonは解釈された言語として、ゆっくりと実行されますが、Cythonなどのツールを介してパフォーマンスを最適化できます。

Golangは並行性がCよりも優れていますが、Cは生の速度ではGolangよりも優れています。 1)Golangは、GoroutineとChannelを通じて効率的な並行性を達成します。これは、多数の同時タスクの処理に適しています。 2)Cコンパイラの最適化と標準ライブラリを介して、極端な最適化を必要とするアプリケーションに適したハードウェアに近い高性能を提供します。

goisidealforforbeginnersandsutable forcloudnetworkservicesduetoitssimplicity、andconcurrencyfeatures.1)installgofromtheofficialwebsiteandverify with'goversion'.2)

Golangは迅速な発展と同時シナリオに適しており、Cは極端なパフォーマンスと低レベルの制御が必要なシナリオに適しています。 1)Golangは、ごみ収集と並行機関のメカニズムを通じてパフォーマンスを向上させ、高配列Webサービス開発に適しています。 2)Cは、手動のメモリ管理とコンパイラの最適化を通じて究極のパフォーマンスを実現し、埋め込みシステム開発に適しています。

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

GolangとCにはそれぞれパフォーマンス競争において独自の利点があります。1)Golangは、高い並行性と迅速な発展に適しており、2)Cはより高いパフォーマンスと微細な制御を提供します。選択は、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。

GolangisidealforBuildingsCalables Systemsduetoitsefficiency andConcurrency、Whilepythonexcelsinquickscriptinganddataanalysisduetoitssimplicityand vastecosystem.golang'ssignencouragesclean、readisinediteNeditinesinedinediseNabletinedinedinedisedisedioncourase
