You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Get a Slack token and invite the bot to a Slack channel](#get-a-slack-token-and-invite-the-bot-to-a-slack-channel)
10
+
-[Running the slackbot on Kubernetes](#running-the-slackbot-on-kubernetes)
11
+
-[Upload the slackbot token to Kubernetes](#upload-the-slackbot-token-to-kubernetes)
12
+
-[Build the bot's container](#build-the-bots-container)
13
+
-[Running the container](#running-the-container)
14
+
-[Running the bot locally](#running-the-bot-locally)
15
+
-[Using the Bot](#using-the-bot)
16
+
-[Sentiment Analysis](#sentiment-analysis)
17
+
-[Entity Analysis](#entity-analysis)
18
+
-[Optional: Create a slackbot app that uses persistent storage](#optional-create-a-slackbot-app-that-uses-persistent-storage)
19
+
-[Shutting down](#shutting-down)
20
+
-[Cleanup](#cleanup)
21
+
4
22
5
23
This example shows a Slack bot built using the [Botkit](https://github.com/howdyai/botkit) library.
6
24
It runs on a Google Container Engine (Kubernetes) cluster, and uses one of the Google Cloud Platform's ML
@@ -131,8 +149,8 @@ Then, set GCLOUD_PROJECT to your project id:
131
149
export GCLOUD_PROJECT=my-cloud-project-id
132
150
```
133
151
134
-
Then, create a file containing your Slack token, and point 'SLACK_TOKEN_PATH' to that file when you run the script
135
-
(substitute 'my-slack-token with your actual token):
152
+
Then, create a file containing your Slack token, and point `SLACK_TOKEN_PATH` to that file when you run the script
153
+
(substitute `my-slack-token` with your actual token):
136
154
137
155
echo my-slack-token > slack-token
138
156
SLACK_TOKEN_PATH=./slack-token node demo_bot.js
@@ -180,14 +198,72 @@ To see the top entities, send it this message:
180
198
```
181
199
182
200
201
+
## Optional: Create a slackbot app that uses persistent storage
202
+
203
+
Kubernetes will keep your slackbot running — we have specified that we want one pod replica, and so if this pod goes down for some reason, Kubernetes will restart it.
204
+
You might be pondering what happens to your sqlite3 database if this happens.
205
+
With the configuration above, you will lose your data if the pod needs to be restarted.
206
+
207
+
One way to address that would be to use a more persistent database service instead of sqlite3, and configure your bot to connect to that instead. ([Cloud SQL](https://cloud.google.com/sql/) would be an option for such a service.)
208
+
209
+
Alternatively (for this simple scenario), we can just create a persistent disk on which to store our sqlite3 database, and configure our pod replica to access it. That way, if the slackbot pod needs to be restarted, the database file won't be lost. We'll do that for this example.
210
+
211
+
We'll accomplish this by defining a [Persistent Volume](http://kubernetes.io/docs/user-guide/persistent-volumes/) resource, and then creating a [Persistent Volume Claim](http://kubernetes.io/docs/user-guide/persistent-volumes/#persistentvolumeclaims) on that resource which will be used by the slackbot app.
212
+
213
+
First, create a persistent disk to use with this app, as follows. Name it `slackbotstore`. You can adjust its size as you like.
Once you've done that, rebuild your docker image to capture that code change:
228
+
229
+
```bash
230
+
export PROJECT_ID=my-cloud-project-id
231
+
docker build -t gcr.io/${PROJECT_ID}/slack-bot .
232
+
```
233
+
234
+
Generate a different .yaml file to use for this configuration:
235
+
236
+
```bash
237
+
./generate-dep.sh $PROJECT_ID
238
+
```
239
+
240
+
If you take a look at the result, in `slack-bot-dep.yaml`, you will see that it contains the specification of the persistent volume as well as a persistent volume claim on that volume. Then, you'll see that the slackbot mounts a volume matching that claim at `/var/sqlite3`.
241
+
(In this config, the slackbot is also specified as a [Deployment](http://kubernetes.io/docs/user-guide/deployments/) rather than a Replication Controller. For the purposes of this example, the difference is not important.)
242
+
243
+
Note: when you created your disk, if you named it something other than `slackbotstore`, you will need to edit this configuration to specify your disk name instead.
244
+
245
+
If you already have a slackbot running, you will probably want to shut it down before you start up this new version, so that there are not two separate bots monitoring your channel. See the "Shutting down" section below. Then, start up the persistent-storage version of the bot like this:
246
+
247
+
```bash
248
+
kubectl create -f slack-bot-dep.yaml
249
+
```
250
+
251
+
183
252
## Shutting down
184
253
185
-
To shutdown your bot, we tell Kubernetes to delete the replication controller.
254
+
To shut down your bot, we tell Kubernetes to delete the Replication Controller:
186
255
187
256
```bash
188
257
kubectl delete -f slack-bot-rc.yaml
189
258
```
190
259
260
+
Or, if you are running the variant that uses a persistent disk, shut it down with:
261
+
262
+
```bash
263
+
kubectl delete -f slack-bot-dep.yaml
264
+
```
265
+
266
+
This will delete the persistent disk resource and claim as well as the Deployment, but does *not* delete the disk itself, which remains part of your GCP project. If you restart later using the same config file, your existing sqlite3 db will be preserved.
(If you used a different name for your cluster, substitute that name for `slackbot-cluster`.)
203
279
This deletes the Google Compute Engine instances that are running the cluster.
204
280
281
+
If you created a persistent disk for your db, you may want to [delete that as well](https://cloud.google.com/sdk/gcloud/reference/compute/disks/delete).
0 commit comments