> ## Documentation Index
> Fetch the complete documentation index at: https://docs.casebender.com/llms.txt
> Use this file to discover all available pages before exploring further.

# DigitalOceanへのデプロイ

> DigitalOceanにCaseBenderをデプロイする

## 概要

このガイドでは、Kubernetes（DOKS）とマネージドサービスを使用して、事前に構築されたDockerイメージでCaseBenderをDigitalOceanにデプロイする方法を説明します。

## 前提条件

1. [DigitalOceanアカウント](https://cloud.digitalocean.com/)
2. [doctl](https://docs.digitalocean.com/reference/doctl/how-to/install/) CLIがインストールされていること
3. [kubectl](https://kubernetes.io/docs/tasks/tools/)がインストールされていること
4. [Docker](https://docs.docker.com/get-docker/)がインストールされていること

## ステップ1: 初期設定

### doctlのインストールと設定

<CodeGroup>
  ```bash macOS theme={null}
  # Homebrewを使用
  brew install doctl

  # APIトークンで認証

  doctl auth init

  # コンテナレジストリ用のDockerの設定

  doctl registry login

  ```

  ```bash Linux theme={null}
  # 最新リリースのダウンロード
  cd ~/Downloads
  wget https://github.com/digitalocean/doctl/releases/download/v1.XX.X/doctl-1.XX.X-linux-amd64.tar.gz

  # 解凍してパスに移動
  tar xf ~/Downloads/doctl-1.XX.X-linux-amd64.tar.gz
  sudo mv ~/Downloads/doctl /usr/local/bin

  # APIトークンで認証
  doctl auth init

  # コンテナレジストリ用のDockerの設定
  doctl registry login
  ```

  ```powershell Windows theme={null}
  # Chocolateyを使用
  choco install doctl

  # APIトークンで認証
  doctl auth init

  # コンテナレジストリ用のDockerの設定
  doctl registry login
  ```
</CodeGroup>

## ステップ2: Kubernetesクラスターの作成

```bash theme={null}
# DOKSクラスターの作成
doctl kubernetes cluster create casebender \
  --region nyc1 \
  --size s-2vcpu-4gb \
  --count 3 \
  --version latest

# kubeconfigの取得
doctl kubernetes cluster kubeconfig save casebender
```

## ステップ3: マネージドサービスのセットアップ

### オブジェクトストレージ用のSpacesの作成

```bash theme={null}
# Spacesバケットの作成
doctl spaces create casebender-storage \
  --region nyc3

# Spacesアクセスキーの作成
doctl spaces access-key create

# 注意: アクセスキーとシークレットキーを安全に保存してください
# アプリケーション設定に必要になります
```

### マネージドPostgreSQLの作成

```bash theme={null}
# データベースクラスターの作成
doctl databases create \
  --engine pg \
  --name casebender-db \
  --region nyc1 \
  --size db-s-2vcpu-4gb \
  --version 14 \
  --num-nodes 1

# データベースの作成
doctl databases db create casebender-db casebender

# 接続詳細の取得
doctl databases connection casebender-db --format ConnectionString
```

### マネージドRedisの作成

```bash theme={null}
# Redisクラスターの作成
doctl databases create \
  --engine redis \
  --name casebender-redis \
  --region nyc1 \
  --size db-s-1vcpu-2gb \
  --version 7

# 接続詳細の取得
doctl databases connection casebender-redis --format ConnectionString
```

## ステップ4: コンテナレジストリの設定

```bash theme={null}
# コンテナレジストリの作成
doctl registry create casebender-registry

# レジストリエンドポイントの取得
REGISTRY_ENDPOINT=$(doctl registry get-endpoint)

# CaseBenderイメージのプル
docker pull casebender/casebender:latest
docker pull casebender/workflow-processor:latest
docker pull casebender/misp-processor:latest

# レジストリ用のイメージのタグ付け
docker tag casebender/casebender:latest registry.digitalocean.com/casebender-registry/app:latest
docker tag casebender/workflow-processor:latest registry.digitalocean.com/casebender-registry/workflow-processor:latest
docker tag casebender/misp-processor:latest registry.digitalocean.com/casebender-registry/misp-processor:latest

# イメージのプッシュ
docker push registry.digitalocean.com/casebender-registry/app:latest
docker push registry.digitalocean.com/casebender-registry/workflow-processor:latest
docker push registry.digitalocean.com/casebender-registry/misp-processor:latest

# レジストリをKubernetesクラスターに追加
doctl kubernetes cluster registry add casebender
```

## ステップ5: Kubernetesへのデプロイ

### 名前空間の作成

```bash theme={null}
kubectl create namespace casebender
```

### シークレットの作成

```bash theme={null}
# データベースとRedis用のシークレットを作成
kubectl create secret generic db-credentials \
  --namespace casebender \
  --from-literal=postgres-url="postgresql://doadmin:password@casebender-db-do-user-1234567-0.b.db.ondigitalocean.com:25060/casebender?sslmode=require" \
  --from-literal=redis-url="rediss://default:password@casebender-redis-do-user-1234567-0.b.db.ondigitalocean.com:25061"

# アプリケーション用のシークレットを作成
kubectl create secret generic app-secrets \
  --namespace casebender \
  --from-literal=auth-secret="your-auth-secret" \
  --from-literal=auth-salt="your-auth-salt"
```

### アプリケーションのデプロイ

`deployment.yaml`を作成:

```yaml theme={null}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: casebender-app
  namespace: casebender
spec:
  replicas: 2
  selector:
    matchLabels:
      app: casebender-app
  template:
    metadata:
      labels:
        app: casebender-app
    spec:
      containers:
        - name: app
          image: registry.digitalocean.com/casebender-registry/app:latest
          ports:
            - containerPort: 3000
          env:
            - name: AUTH_SECRET
              valueFrom:
                secretKeyRef:
                  name: app-secrets
                  key: auth-secret
            - name: AUTH_SALT
              valueFrom:
                secretKeyRef:
                  name: app-secrets
                  key: auth-salt
            - name: POSTGRES_PRISMA_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: postgres-url
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: redis-url
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: workflow-processor
  namespace: casebender
spec:
  replicas: 1
  selector:
    matchLabels:
      app: workflow-processor
  template:
    metadata:
      labels:
        app: workflow-processor
    spec:
      containers:
        - name: processor
          image: registry.digitalocean.com/casebender-registry/workflow-processor:latest
          env:
            - name: POSTGRES_PRISMA_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: postgres-url
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: redis-url
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: misp-processor
  namespace: casebender
spec:
  replicas: 1
  selector:
    matchLabels:
      app: misp-processor
  template:
    metadata:
      labels:
        app: misp-processor
    spec:
      containers:
        - name: processor
          image: registry.digitalocean.com/casebender-registry/misp-processor:latest
          env:
            - name: POSTGRES_PRISMA_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: postgres-url
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: redis-url
```

デプロイメントを適用:

```bash theme={null}
kubectl apply -f deployment.yaml
```

### サービスの作成

`service.yaml`を作成:

```yaml theme={null}
apiVersion: v1
kind: Service
metadata:
  name: casebender-app
  namespace: casebender
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 3000
  selector:
    app: casebender-app
```

サービスを適用:

```bash theme={null}
kubectl apply -f service.yaml
```

## ステップ7: イングレスのセットアップ

### NGINX Ingressコントローラーのインストール

```bash theme={null}
# Helmリポジトリの追加
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

# NGINX Ingressコントローラーのインストール
helm install nginx-ingress ingress-nginx/ingress-nginx \
  --namespace casebender \
  --set controller.publishService.enabled=true
```

### イングレスの設定

`ingress.yaml`を作成:

```yaml theme={null}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: casebender-ingress
  namespace: casebender
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - your-domain.com
      secretName: casebender-tls
  rules:
    - host: your-domain.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: casebender-app
                port:
                  number: 80
```

イングレスを適用:

```bash theme={null}
kubectl apply -f ingress.yaml
```

## ステップ8: cert-managerでSSLを設定

```bash theme={null}
# cert-managerのインストール
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.8.0/cert-manager.yaml

# ClusterIssuerの作成
cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: letsencrypt-prod
spec:
  acme:
    server: https://acme-v02.api.letsencrypt.org/directory
    email: your-email@domain.com
    privateKeySecretRef:
      name: letsencrypt-prod
    solvers:
    - http01:
        ingress:
          class: nginx
EOF
```

## モニタリングとメンテナンス

### モニタリングのセットアップ

```bash theme={null}
# メトリクスサーバーのインストール
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# PrometheusとGrafanaのデプロイ
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

helm install prometheus prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --create-namespace
```

### 水平ポッドオートスケーリングの設定

`hpa.yaml`を作成:

```yaml theme={null}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: casebender-app-hpa
  namespace: casebender
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: casebender-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 80
```

HPAを適用:

```bash theme={null}
kubectl apply -f hpa.yaml
```

## バックアップと災害復旧

### データベースバックアップ

DigitalOceanのマネージドデータベースは自動的にバックアップを処理します。また、以下も可能です:

```bash theme={null}
# 手動バックアップの作成
doctl databases backup casebender-db
```

### データベースフェイルオーバーの設定

```bash theme={null}
# 自動フェイルオーバーの有効化
doctl databases replica casebender-db create \
  --region sfo2
```

## セキュリティのベストプラクティス

1. DigitalOcean Cloud Firewallの有効化
2. プライベートネットワークの使用
3. ネットワークポリシーの実装
4. 定期的なセキュリティ更新
5. 監査ログの有効化

## コスト最適化

1. 適切なノードサイズの使用
2. 自動スケーリングの実装
3. ブロックストレージの賢い使用
4. リソース使用量のモニタリング
5. 予約ドロップレットの検討

## 次のステップ

* CI/CDパイプラインの設定
* モニタリングアラートの設定
* ロギングソリューションの実装
* セキュリティ対策の確認
