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

# クラスターとエールセットアップ

> AWS EC2にKubernetes(k3s)を構成し、エールをインストールするガイドです。

<Info>
  本ガイドではk3sを使用してKubernetesクラスターを構成していますが、kubeadm、kubesprayなど他の方法で構成されたクラスター環境でもエールを使用することができます。
</Info>

***

## K3sとエージェント設定

<Steps>
  <Step title="EC2にSSH接続後、以下のコマンドを実行してk3sをインストールします。">
    ```bash theme={null}
    curl -sfL https://get.k3s.io | sh -s - server \
      --bind-address=0.0.0.0 \
      --advertise-address=[EC2のパブリックIPアドレス] \
      --tls-san=[EC2のパブリックIPアドレス] \
      --node-external-ip=[EC2のパブリックIPアドレス] \
      --disable=traefik \
      --docker
    ```
  </Step>

  <Step title="kubectlをroot以外のユーザーで使用する場合は、以下のコマンドを実行します。">
    ```bash theme={null}
    mkdir -p ~/.kube && \
    sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config && \
    sudo chown $(whoami):$(whoami) ~/.kube/config && \
    chmod 600 ~/.kube/config && \
    echo "export KUBECONFIG=~/.kube/config" >> ~/.bashrc && \
    exec -l $SHELL
    ```
  </Step>

  <Step title="以下のコマンドを入力して、クラスターが正常に動作していることを確認します。">
    ```bash theme={null}
    kubectl get nodes
    ```

    ```bash theme={null}
    # STATUSがReadyであることを確認
    NAME              STATUS   ROLES                  AGE     VERSION
    ip-172-31-14-74   Ready    control-plane,master   6d21h   v1.31.3+k3s1
    ```
  </Step>

  <Step title="以下のコマンドを実行してale agentをインストールしてください。">
    ```
    kubectl apply -f https://raw.githubusercontent.com/ale-run/ale/refs/heads/main/k8s/dist/latest/agent.yaml
    ```
  </Step>
</Steps>

## Kubernetes Metrics Server

> ガイドに従ってインストールしたK3sにはMetrics Serverが組み込まれているため、インストールする必要はありません。未インストールのクラスターの場合は、以下のコマンドを確認してMetrics Serverをインストールしてください。

<AccordionGroup>
  <Accordion title="Kubernetes Metrics Serverインストールコマンド">
    ```bash theme={null}
    kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
    ```
  </Accordion>
</AccordionGroup>

<Info>
  以下のコマンドでMetrics Server経由でリソース使用状況が正常に表示されることを確認できます。

  ```bash theme={null}
  kubectl top nodes
  ```
</Info>

## Nginx Ingress Controller

<Steps>
  <Step title="以下のコマンドを実行してNginx Ingress Controllerをインストールしてください。">
    ```bash theme={null}
    helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
    helm repo update
    ```

    ```bash theme={null}
    helm install ingress-nginx ingress-nginx/ingress-nginx \
      --version 4.11.2 \
      --namespace ingress-nginx --create-namespace \
      --set controller.service.type=LoadBalancer \
      --set controller.allowSnippetAnnotations=true \
      --set controller.admissionWebhooks.enabled=false
    ```
  </Step>

  <Step title="以下のコマンドを実行して、KubernetesサービスのExternal IPが実際のEC2に紐付けられたElastic IPと一致することを確認します。">
    ```bash theme={null}
    kubectl get svc -n ingress-nginx
    ```

    ```
    NAME                       TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)                      AGE
    ingress-nginx-controller   LoadBalancer   10.43.158.249   [外部IP]        80:32119/TCP,443:31694/TCP   6d21h
    ```
  </Step>
</Steps>

## エールのインストールと実行

<Steps>
  <Step title="以下のコマンドを入力して`ale-run`パッケージをインストールしてください。">
    ```bash theme={null}
    mkdir ale-run
    cd ale-run
    ```

    ```bash theme={null}
    npm i @ale-run/runtime@latest
    ```
  </Step>

  <Step title="以下のコマンドを入力してエールを運用モードで実行します。">
    ```bash theme={null}
    pm2 start "npx aled run -c [Kubernetesクラスター名]" --name "ale-run" --watch
    ```

    <Tip>
      Kubernetesクラスター名を別途指定していない場合、一般的に`default`という名前で作成され、`kubectl config current-context`コマンドで確認できます。
    </Tip>
  </Step>

  <Step title="pm2で確認したログが以下のように表示されれば、実行が完了しています。">
    ```bash theme={null}
    pm2 log ale-run
    ```

    ```bash theme={null}
    ...
    [INFO] [plugin:loader] searching plugins at /home/ubuntu/ale-run/node_modules/@ale-run/runtime/dist
    [INFO] [plugin:loader] 2 plugin detected in /home/ubuntu/ale-run/node_modules/@ale-run/runtime/dist
    [INFO] [plugin:loader] plugin @ale-run/plugin-essentials has been added
    [INFO] [plugin:loader] plugin @ale-run/plugin-file-store has been added
    [INFO] [plugin:loader] searching plugins at /home/ubuntu/ale-run/node_modules
    [INFO] [plugin:loader] 1 plugin detected in /home/ubuntu/ale-run/node_modules
    [INFO] [plugin:loader] plugin @ale-run/plugin-ai-assistant has been added
    [INFO] [plugin:essentials] plugin @ale-run/plugin-essentials is installed {}
    [INFO] [plugin:essentials] plugin @ale-run/plugin-essentials is activate {}
    [INFO] [plugin:file-store] plugin @ale-run/plugin-file-store is activate {}
    [INFO] [plugin:file-store] working dir is /home/ubuntu/.ale
    [INFO] [plugin:file-store] storage type is direct
    [INFO] [plugin:file-store] upload dir is /home/ubuntu/.ale/files
    [INFO] [plugin:file-store] temp dir is /home/ubuntu/.ale/temp
    [INFO] [plugin:file-store] endpoint is (none)
    [INFO] [plugin:file-store] max file size is 10MB
    [INFO] [plugin:ai-assistant] plugin @ale-run/plugin-ai-assistant is activate {}
    [WARN] [plugin] [@ale-run/plugin-ai-assistant] activate failed: The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).
    [INFO] [runtime:init] api server listening on 0.0.0.0:9009
    ```

    <Warning>
      実行中にデータベースの競合が発生したり、設定を初期化したい場合は、以下のコマンドを入力して既存のエールのデータを削除してから再実行してください。

      ```bash theme={null}
      rm -r ~/.ale 
      ```
    </Warning>
  </Step>
</Steps>
