1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Go言語で学ぶWebアプリケーション開発6:[大型クラウド環境(AWS・GCP・Azure)へのデプロイについて & KubernetesでのCI/CDについて]

Posted at

はじめに

前回の記事では、Kubernetesを活用したデプロイ方法、負荷テストの具体的な実施方法について解説しました。
これ以上はかなり深掘りした内容になるのですが、さらなる発展系としてAWS・GCP・Azureなどの大型クラウド環境にデプロイする方法と、Kubernetesの CI/CDパイプライン構築(自動テストの導入)について解説します。
専門的な内容にはなりますが、知っておくと一段階レベルアップできると思うので、同じように学習に励んでいる方の知見になると嬉しいです!

対象読者

  • AWS/GCP/Azureなどの大型クラウド環境にデプロイしたい方
  • ECS、GKE、AKSなどのコンテナサービスを利用してみたい方
  • KubernetesのCI/CDパイプライン(自動テスト)を構築したい方

目次

  1. クラウド環境へのデプロイ(AWS・GCP・Azure)
    • AWS(ECS + Fargate)
    • GCP(GKE)
    • Azure(AKS)
  2. KubernetesのCI/CDパイプライン構築
    • GitHub Actionsを使った CI/CD
    • ArgoCDを使った継続的デプロイ
    • 自動テストの導入

1. クラウド環境へのデプロイ(AWS・GCP・Azure)

1.1 AWS(ECS + Fargate)でのデプロイ

AWS ECS (Elastic Container Service) + Fargate を利用すると、コンテナをサーバーレスで実行でき、スケール管理が容易になります。

ステップ 1: DockerイメージをAmazon Elastic Container Registry (ECR) にプッシュ

aws ecr create-repository --repository-name go-app
$(aws ecr get-login --no-include-email)

docker tag my-go-app:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/go-app:latest
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/go-app:latest

ステップ 2: ECS クラスタとタスク定義の作成

  • ECSコンソールでFargateを使用したクラスターを作成
  • タスク定義でECRのイメージを使用
  • ALB(Application Load Balancer)を設定して外部公開

ステップ 3: ECSサービスのデプロイ

aws ecs create-service --cluster go-app-cluster --service-name go-app-service --task-definition go-app-task

1.2 GCP(GKE: Google Kubernetes Engine)でのデプロイ

ステップ 1: GKEクラスタの作成

gcloud container clusters create go-app-cluster --num-nodes=3

ステップ 2: GCR(Google Container Registry)にDockerイメージをプッシュ

docker tag my-go-app gcr.io/my-project/go-app

docker push gcr.io/my-project/go-app

ステップ 3: Kubernetesへのデプロイ

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

1.3 Azure(AKS: Azure Kubernetes Service)でのデプロイ

ステップ 1: AKS クラスタの作成

az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 3 --enable-addons monitoring --generate-ssh-keys

ステップ 2: ACR(Azure Container Registry)にDockerイメージをプッシュ

az acr create --resource-group myResourceGroup --name myACR --sku Basic
az acr login --name myACR
docker tag my-go-app myacr.azurecr.io/go-app
docker push myacr.azurecr.io/go-app

ステップ 3: Kubernetesへのデプロイ

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

2. KubernetesのCI/CDパイプライン構築

2.1 GitHub Actions を使った CI/CD

GitHub Actionsのワークフローを作成する。

.github/workflows/deploy.yml

name: Deploy to Kubernetes
on:
  push:
    branches:
      - main
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: チェックアウト
        uses: actions/checkout@v2

      - name: Docker ビルドとプッシュ
        run: |
          docker build -t myacr.azurecr.io/go-app:${{ github.sha }} .
          docker push myacr.azurecr.io/go-app:${{ github.sha }}

      - name: Kubernetes にデプロイ
        run: |
          kubectl set image deployment/go-app go-app=myacr.azurecr.io/go-app:${{ github.sha }}

2.2 ArgoCDを使った継続的デプロイ

ArgoCDはKubernetesネイティブのCDツールです。

ArgoCD のインストール

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

ArgoCD でアプリケーションを管理

kubectl apply -f application.yaml

2.3 自動テストの導入

Kubernetesでのデプロイ前に自動テストを実行することで、品質を確保できる。

.github/workflows/test.yml

name: Run Tests
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: チェックアウト
        uses: actions/checkout@v2
      - name: Go テストの実行
        run: go test ./...

まとめ

項目 説明
AWS ECS + Fargate サーバーレスでコンテナを管理できるAWSのサービス
GCP GKE GoogleのフルマネージドKubernetesサービス
Azure AKS AzureのKubernetesサービス
GitHub Actions CI/CDの自動化に最適なツール
ArgoCD KubernetesネイティブのCDツール
自動テスト デプロイ前にGoのユニットテストを実施

少し専門的かつ、難しめの内容になりますが、これができると少しレベルアップしたような気持ちになれるので、ぜひ挑戦してみましょう!
次はモニタリング(Prometheus & Grafana)、クラウドネイティブなサービスメッシュ(Istio) についても触れていこうと思います!
自分もまだまだ曖昧な点が多いので、同じ勉強をしてる方の知見として活用していただけると嬉しいです!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?