feat(bot): Update stuff to migrate to k8s.

This commit is contained in:
Nyeki.py
2024-10-31 04:57:58 -03:00
parent d20f808b88
commit c0616cbcfe
16 changed files with 367 additions and 330 deletions
+89 -21
View File
@@ -98,39 +98,107 @@ The following steps show how to pull and install Payphone on a running cluster o
### Building and Publishing the Payphone Image
To be able to deploy Payphone to your k8s cluster, you first need to upload the image to a private
registry. This documentation uses Docker Hub as a registry
Payphone's container images are hosted here at `git.nyeki.dev`. To login, run the following
command:
```sh
# Only run this step if you need to build the source code and it is in your machine. Build and
# publish to the private registry. Use your own image name, for example `samantha/payphone:latest`.
docker login
docker login git.nyeki.dev
```
docker build -t nekidev/payphone:latest .
docker publish nekidev/payphone:latest
A new browser tab will open and you'll be prompted for log in. Once you accept, you'll be logged in
to the container registry.
# In the following steps, if you're using microk8s in production, use `microk8s kubectl` instead of
# `kubectl`. You can add this to your ~/.bash_aliases to avoid having to prepend `microk8s` to all
# commands:
# alias kubectl='microk8s kubectl'
When you have made an update and want to update the production deployment's image, you need to
build and push the image to the container registry:
# This step requires you to create a personal access token for Docker Hub. To create it:
# 1. Go to https://app.docker.com/settings/personal-access-tokens/create
# 2. Set the access permission to "Read-only"
kubectl create secret docker-registry regcred --docker-server=https://index.docker.io/v1/ --docker-username=<your-username> --docker-password=<your-personal-access-token>
```sh
docker build -t git.nyeki.dev/payphone/payphone:latest .
docker push git.nyeki.dev/payphone/payphone:latest
```
### Starting Payphone in the Cluster
# This will pull the images for citus and redis and start their containers.
kubectl apply -f redis.yml
kubectl apply -f citus.yml
#### Setting Up Config Files
# After applying citus.yml, you'll need to connect the nodes. We'll always use citus-0 as the
# master instance.
First of all, you'll need to set up the environment variables that Payphone needs. For this,
you'll need:
1. A Discord bot token, base64 encoded. You can encode your token at
[www.base64encode.org](https://www.base64encode.org/).
2. A suggestions channel that the bot can write to (its ID).
3. A non-expiring invite to the official server.
First of all, open `k8s/config.yml`. At the bottom of the file, you'll find a `data` object with
some environment variables definitions. Set `SUGGESTIONS_CHANNEL_ID` and `SERVER_INVITE` and save
the file.
Then, after you've base64-encoded your token, open `k8s/secrets.yml` and in a similar `data` object
at the bottom of the file, set `TOKEN` to the base64-encoded Discord bot token and save the
changes.
After the files are updated with their new values, run the following commands to apply these
changes:
```sh
kubectl apply -f k8s/config.yml
kubectl apply -f k8s/secrets.yml
```
If you see a ConfigMap and a Secret being created, that means you've successfully applied the
changes.
#### Setting Up Redis
Now, you can initialize Redis. This step is pretty straightforward and you can forget about it
later.
```sh
kubectl apply -f k8s/redis.yml
```
#### Setting Up Citus Database
This step requires some updates in the `k8s/citus.yml` file. Open the file and make sure that the
default amount of replicas fits your needs. At least one is needed.
```sh
kubectl apply -f k8s/citus.yml
```
Once all the nodes are up and running, you have to connect them. The node `citus-0` is always used
as the coordinator node. Run the following command to set the coordinator host:
```sh
kubectl exec citus-0 -- psql -U payphone payphone -c "SELECT citus_set_coordinator_host('citus-0.citus-headless.default.svc.cluster.local', 5432);"
```
Once that command was run, you need to add the rest of the nodes to the cluster. To do that, run
the following command replacing `<node-index>` with the index of the node you're adding, once per
node.
```sh
kubectl exec citus-0 -- psql -U payphone payphone -c "SELECT * from citus_add_node('citus-<node-index>.citus-headless.default.svc.cluster.local', 5432);"
```
For example, if you had 3 replicas in total, you'd run the command 2 times, once for node 1 and
once for node 2 (node 0 is the coordinator, so you don't have to add it).
As an optional step, you can check that the nodes were successfully connected running the following
command, which will show all the nodes connected:
```sh
kubectl exec citus-0 -- psql -U payphone payphone -c "SELECT * FROM citus_get_active_worker_nodes();"
```
#### Setting Up Payphone
Finally, you need to start the bot's instance.
```sh
# Steps before running this command:
# 1. Update the image name in `bot.yml` to your own image name (the one that you have in your
# private docker hub registry)
# 2. Set the total number of shards in both `spec.replicas` and
# `spec.template.spec.containers.bot.env.TOTAL_SHARDS.value`.
kubectl apply -f bot.yml
kubectl apply -f k8s/bot.yml
```