|
| 1 | +# Deploy AstrBot with Kubernetes |
| 2 | + |
| 3 | +> [!WARNING] |
| 4 | +> You can deploy AstrBot in a high-availability setup using Kubernetes (K8s), allowing it to automatically recover from failures. |
| 5 | +> |
| 6 | +> Due to the current use of an SQLite database, this deployment does not support horizontal scaling with multiple replicas. Additionally, if using the Sidecar mode, pay special attention to the persistence of NapCat's login state. |
| 7 | +> |
| 8 | +> The following tutorial assumes that you have `kubectl` installed and configured, and that you can connect to your K8s cluster. |
| 9 | +
|
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Before you begin, make sure your Kubernetes cluster meets the following conditions: |
| 13 | + |
| 14 | +1. **Default StorageClass**: Used to dynamically create `PersistentVolumeClaim` (PVC). You can check this with `kubectl get sc`. If you don't have one, you need to manually create a `PersistentVolume` (PV) or install a corresponding storage plugin (e.g., `nfs-client-provisioner`). |
| 15 | +2. **Network Access**: Ensure that your cluster nodes can pull images from `docker.io` or your specified image repository. |
| 16 | + |
| 17 | +## Deployment Methods |
| 18 | + |
| 19 | +We offer two deployment options: |
| 20 | + |
| 21 | +* **Integrated Deployment (Sidecar Mode)**: Deploy AstrBot and NapCat in the same Pod. Recommended for personal QQ accounts. |
| 22 | +* **Standalone Deployment**: Deploy only AstrBot. Suitable for other platforms or if you want to manage NapCat independently. |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +### Method 1: Deploy with NapCatQQ (Sidecar) |
| 27 | + |
| 28 | +This method is located in the `k8s/astrbot_with_napcat` directory. |
| 29 | + |
| 30 | +#### 1. Deploy |
| 31 | + |
| 32 | +```bash |
| 33 | +# 1. Create namespace |
| 34 | +kubectl apply -f k8s/astrbot_with_napcat/00-namespace.yaml |
| 35 | + |
| 36 | +# 2. Create Persistent Volume Claim |
| 37 | +# Note: astrbot-data-shared-pvc requires ReadWriteMany (RWX) access mode. |
| 38 | +# If your cluster does not support RWX, you need to configure shared storage such as NFS and modify the storageClassName in 01-pvc.yaml. |
| 39 | +kubectl apply -f k8s/astrbot_with_napcat/01-pvc.yaml |
| 40 | + |
| 41 | +# 3. Deploy the application |
| 42 | +kubectl apply -f k8s/astrbot_with_napcat/02-deployment.yaml |
| 43 | +``` |
| 44 | + |
| 45 | +#### 2. Expose Service (Choose one) |
| 46 | + |
| 47 | +* **Option A: NodePort** |
| 48 | + |
| 49 | + ```bash |
| 50 | + kubectl apply -f k8s/astrbot_with_napcat/03-service-nodeport.yaml |
| 51 | + ``` |
| 52 | + |
| 53 | + The service will be exposed via the node IP and a port automatically assigned by Kubernetes. You can find the port with the following command: |
| 54 | + |
| 55 | + ```bash |
| 56 | + kubectl get svc -n astrbot-ns |
| 57 | + ``` |
| 58 | + |
| 59 | + In the output, find the `PORT(S)` column for `astrbot-webui-svc` and `napcat-web-svc`. The format is `<internal-port>:<NodePort>/TCP`. For example, if you see `8080:30185/TCP`, the access address is `http://<NodeIP>:30185`. |
| 60 | + |
| 61 | +* **Option B: LoadBalancer** |
| 62 | + |
| 63 | + If your cluster supports `LoadBalancer` type services (usually provided in K8s services from cloud providers), you can use this method. |
| 64 | + |
| 65 | + ```bash |
| 66 | + kubectl apply -f k8s/astrbot_with_napcat/04-service-loadbalancer.yaml |
| 67 | + ``` |
| 68 | + |
| 69 | + After execution, check the assigned external IP (EXTERNAL-IP) with `kubectl get svc -n astrbot-ns`. |
| 70 | + |
| 71 | +#### 3. Configure Connection |
| 72 | + |
| 73 | +Since AstrBot and NapCat are in the same Pod, they can communicate directly via `localhost`. |
| 74 | + |
| 75 | +1. **Add a message platform in AstrBot:** |
| 76 | + * Go to the AstrBot WebUI, select `Settings` -> `Message Platform` -> `Add`. |
| 77 | + * **Select Message Platform Category**: `aiocqhttp` |
| 78 | + * **Bot Name**: `napcat` (or custom) |
| 79 | + * **Reverse Websocket Host**: `0.0.0.0` |
| 80 | + * **Reverse Websocket Port**: `6199` |
| 81 | + * Save the configuration. |
| 82 | + |
| 83 | + |
| 84 | +2. **Configure Websocket Client in NapCat:** |
| 85 | + * Go to the NapCat WebUI, select `Settings` -> `Reverse WS` -> `Add`. |
| 86 | + * **Enable**: On |
| 87 | + * **URL**: `ws://localhost:6199/ws` |
| 88 | + * **Message Format**: `Array` |
| 89 | + * Save the configuration. |
| 90 | + |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +### Method 2: Deploy AstrBot Only (General Purpose) |
| 95 | + |
| 96 | +This method is located in the `k8s/astrbot` directory. |
| 97 | + |
| 98 | +#### 1. Deploy |
| 99 | + |
| 100 | +```bash |
| 101 | +# 1. Create namespace |
| 102 | +kubectl apply -f k8s/astrbot/00-namespace.yaml |
| 103 | +
|
| 104 | +# 2. Create Persistent Volume Claim |
| 105 | +kubectl apply -f k8s/astrbot/01-pvc.yaml |
| 106 | +
|
| 107 | +# 3. Deploy the application |
| 108 | +kubectl apply -f k8s/astrbot/02-deployment.yaml |
| 109 | +``` |
| 110 | + |
| 111 | +#### 2. Expose Service (Choose one) |
| 112 | + |
| 113 | +* **Option A: NodePort** |
| 114 | + |
| 115 | + ```bash |
| 116 | + kubectl apply -f k8s/astrbot/03-service-nodeport.yaml |
| 117 | + ``` |
| 118 | + |
| 119 | + The service will be exposed via the node IP and a port automatically assigned by Kubernetes. You can find the port with the following command: |
| 120 | + |
| 121 | + ```bash |
| 122 | + kubectl get svc -n astrbot-standalone-ns |
| 123 | + ``` |
| 124 | + |
| 125 | + In the output, find the `PORT(S)` column for `astrbot-webui-svc`. The format is `<internal-port>:<NodePort>/TCP`. For example, if you see `8080:30185/TCP`, the access address is `http://<NodeIP>:30185`. |
| 126 | + |
| 127 | +* **Option B: LoadBalancer** |
| 128 | + |
| 129 | + ```bash |
| 130 | + kubectl apply -f k8s/astrbot/04-service-loadbalancer.yaml |
| 131 | + ``` |
| 132 | + |
| 133 | + After execution, check the assigned external IP (EXTERNAL-IP) with `kubectl get svc -n astrbot-standalone-ns`. |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## Advanced Configuration |
| 138 | + |
| 139 | +### Image Mirror (for users in mainland China) |
| 140 | + |
| 141 | +If you have difficulty pulling the `soulter/astrbot:latest` or `mlikiowa/napcat-docker:latest` images, you can manually edit the corresponding `02-deployment.yaml` file and replace the `image` field with a domestic mirror address, for example: |
| 142 | + |
| 143 | +```yaml |
| 144 | +# Example: |
| 145 | +# image: soulter/astrbot:latest |
| 146 | +# Replace with: |
| 147 | +image: m.daocloud.io/docker.io/soulter/astrbot:latest |
| 148 | +``` |
| 149 | + |
| 150 | +### Enable Docker Sandbox Code Executor |
| 151 | + |
| 152 | +If you need to use the sandbox code executor, you need to mount the Docker socket file into the Pod. |
| 153 | + |
| 154 | +Edit the `02-deployment.yaml` file and add `volumes` and `volumeMounts` under `spec.template.spec`: |
| 155 | + |
| 156 | +1. **Add the following to the `volumeMounts` list of the `astrbot` container:** |
| 157 | + |
| 158 | + ```yaml |
| 159 | + - name: docker-sock |
| 160 | + mountPath: /var/run/docker.sock |
| 161 | + ``` |
| 162 | + |
| 163 | +2. **Add the following to the `spec.template.spec.volumes` list:** |
| 164 | + |
| 165 | + ```yaml |
| 166 | + - name: docker-sock |
| 167 | + hostPath: |
| 168 | + path: /var/run/docker.sock |
| 169 | + type: Socket |
| 170 | + ``` |
| 171 | + |
| 172 | +> [!WARNING] |
| 173 | +> Mounting the Docker socket into a Pod poses a security risk. Please ensure you understand the implications. |
| 174 | + |
| 175 | +## View Logs |
| 176 | + |
| 177 | +* **Sidecar Deployment Mode:** |
| 178 | + |
| 179 | + ```bash |
| 180 | + # View AstrBot logs |
| 181 | + kubectl logs -f -n astrbot-ns deployment/astrbot-stack -c astrbot |
| 182 | +
|
| 183 | + # View NapCat logs |
| 184 | + kubectl logs -f -n astrbot-ns deployment/astrbot-stack -c napcat |
| 185 | + ``` |
| 186 | + |
| 187 | +* **Standalone Deployment Mode:** |
| 188 | + |
| 189 | + ```bash |
| 190 | + kubectl logs -f -n astrbot-standalone-ns deployment/astrbot-standalone |
| 191 | + ``` |
| 192 | + |
| 193 | +## 🎉 All Done! |
| 194 | + |
| 195 | +After deploying and exposing the service, you can access the AstrBot admin panel through the corresponding IP and port. |
| 196 | + |
| 197 | +> The default username and password are `astrbot` and `astrbot`. |
0 commit comments