Deploy Evobot On Kubernetes

Evobot is a Discord Music Bot written in NodeJS. From the github page, it’s describe as “a Discord Music Bot built with discord.js & uses Command Handler from discordjs.guide”

This is pretty simple to deploy as we don’t need to create any persistent storage or any services. We only need to create the deployment file, a secret, configmap, and namespace. Let’s start off by making the evobot folder in the ~/k3s/ directory with $ mkdir ~/k3s/evobot/

Namespace

We’ll always start off by creating the namespace first. Let’s go ahead and create a namespace.evobot.yaml file and input the following:

apiVersion: v1
kind: Namespace
metadata:
  name: evobot
  labels:
    name: evobot

Once the yaml file has been created, let’s apply it with $ kubectl apply -f namespace.evobot.yaml.

Next let’s confirm the namespace was created:

$ kubectl get ns

NAME                   STATUS   AGE
default                Active   42h
kube-system            Active   42h
kube-public            Active   42h
kube-node-lease        Active   42h
metallb-system         Active   39h
cert-manager           Active   13h
ingress-nginx          Active   12h
kubernetes-dashboard   Active   11h
pihole                 Active   3h26m
evobot                 Active   4s

We should now see the evobot namespace created in the output of the previous command.

ConfigMap

Next, we’ll want to configure the ConfigMap. Let’s create the configmap.evobot.yaml file and input the following block of code:

apiVersion: v1
kind: ConfigMap
metadata:
  name: evobot-config
  namespace: evobot
  labels:
    app: evobot
data:
    MAX_PLAYLIST_SIZE: "10"     #Maximum size of playlist
    PREFIX: "/"                 #What to put before our bot commands in discord. 
    PRUNING: "false"            #Whether the bot should delete user's commands in discord once read
    LOCALE: "en"                #What language the bot should be in
    DEFAULT_VOLUME: "100"       #Default volume of the bot's sound output in Discord
    STAY_TIME: "30"             #How long the bot should stay in the discord voice channel after last played song. This is in seconds

As before, once the yaml file has been created, we can apply it with $ kubectl apply -f configmap.evobot.yaml

We’ll also want to confirm the configmap has been created successfully:

$ kubectl get configmap -n evobot

NAME               DATA   AGE
kube-root-ca.crt   1      97s
evobot-config      6      32s

With the get kubectl get configmap -n evobot command, we should now see the newly created evobot-config configmap

Secret

Now, we’ll want to input our Bot’s Discord Token, along with the Youtube API Key. I’m not going to go into details on how to get the token or the API key, but Erit has linked some useful guides in the github repo, which I’ll link below:

With all of that out of the way, let’s go ahead and create our secret.evobot.yaml file and input the following code block:

apiVersion: v1
kind: Secret
metadata:
  name: discord-token 
  namespace: evobot
type: Opaque
stringData:
  TOKEN: ReplaceThisWithDiscordToken                 #Replace this with Your Discord token
---
apiVersion: v1
kind: Secret
metadata:
  name: youtube-key 
  namespace: evobot
type: Opaque
stringData:
  YOUTUBE_API_KEY: ReplaceThisWithYoutubeAPIKey       #Replace this with your youtube API key 
---
apiVersion: v1
kind: Secret
metadata:
  name: soundcloud-id 
  namespace: evobot
type: Opaque
stringData:
  SOUNDCLOUD_CLIENT_ID: ""      #If you have a soundcloud Client ID, replace the quotes with your soundcloud client id

Again, as before, let’s apply this file with $ kubectl apply -f secret.evobot.yaml

We can confirm the secrets have been created successfully by running the following command:

$ kubectl get secrets -n evobot

NAME                  TYPE                                  DATA   AGE
default-token-24j2w   kubernetes.io/service-account-token   3      6m48s
discord-token         Opaque                                1      2m18s
youtube-key           Opaque                                1      2m18s
soundcloud-id         Opaque                                1      71s

Deployment

Now we’re ready to deploy our evobot application. Let’s go ahead and create the deploy.evobot.yaml file and input the following code:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels: 
    app: evobot
  name: evobot
  namespace: evobot
spec:
  replicas: 1
  selector:
    matchLabels:
      app: evobot
  template:
    metadata:
      labels:
        app: evobot
    spec:
      containers:
        - image: eritislami/evobot:master
          name: evobot
          envFrom:
            - configMapRef:
                name: evobot-config
            - secretRef:
                name: discord-token
            - secretRef:
                name: youtube-key
            - secretRef:
                name: soundcloud-id

Now we’re ready to deploy our application. let’s go ahead and apply the deploy file with $ kubectl apply -f deploy.evobot.yaml

Let’s confirm the bot is up and running:

$ kubectl get all -n evobot

NAME                          READY   STATUS    RESTARTS   AGE
pod/evobot-6f4bfc5599-sgnxt   1/1     Running   0          30s

NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/evobot   1/1     1            1           30s

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.apps/evobot-6f4bfc5599   1         1         1       30s

$ kubectl logs evobot-6f4bfc5599-sgnxt -n evobot --follow
BasedClusterBot ready!                  #This is the name of my Discord bot, your's will be a bit different