Azure K8S and Sentry (Part 1)

Gino Busok Jr
4 min readMar 24, 2019

Tracking errors, bugs, and uncaught exceptions can easily be done by using a service or application that does that. Sentry is one of them. One good thing about sentry is that it is open source so you can deploy it on your own server. But if you have money to burn, I suggest to just buy a subscription here https://sentry.io/pricing/.

If you really want to have your own sentry installation, then be warned that setting it up is not that simple. First, you need to have a running postgres, redis, and memcached. You also need an SMTP server where you can use your Google or MailGun account. For this guide, we will use SMTP as we can get one for free. Thanks Google!

Assuming you are using ubuntu, you can follow these links for instructions on how to install postgres, memcache and redis or you can use Docker. All of these dependencies as well as Sentry itself have Docker images, which make our lives a LOT easier. So docker it is!

If you decide to set-up Sentry by using Docker and docker-compose, just follow this Github link. It has everything that you will need to have your own Sentry instance. But if you googled “Azure K8S and Sentry <other keywords>” and you stumbled upon this medium page, then read on.

Assuming you:

  1. already know kubernetes
  2. already have a running cluster
  3. have a properly configured kubectl client
  4. are using AKS which is not really a requirement but there are config files, specifically for volumes, that are specifically for AKS

then read on…

Volumes

Let’s start! First, create your volumes where you will store your postgres data as well as your sentry files. For both yml files, do note the value for metadata.name as you will use that when creating the pods.

Postgres Storage

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sentry-pg-disk
spec:
accessModes:
- ReadWriteOnce
storageClassName: default
resources:
requests:
storage: 100Gi

Sentry Storage

Sentry needs 3 processes for it to work: its web app, worker, and its own cron app. Good thing these 3 are provided in their docker image. They all share the same volume and they would have the same environment configurations so all you need to do is run the same image 3 times with different commands. Because of the volume sharing characteristic of Sentry, you can’t use the Postgres storage configuration as it is not shareable. You need a new kind of storage and thank to MS Azure they have a very good documentation for this. Just follow this link https://docs.microsoft.com/en-us/azure/aks/azure-disks-dynamic-pv and do everything except the creation of volume. Once you have done that, you can now create this pod:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sentry-files-disk
spec:
accessModes:
- ReadWriteMany
storageClassName: azure-file
resources:
requests:
storage: 100Gi

These are the commands to create pod files:

kubectl apply -f your-postgres-storage.yml
kubectl apply -f your-sentry-files-storage.yml
# Run this to check if your volumes are properly created
kubectl get pvc

And you’re done with volumes! You can now install the dependencies.

Postgres, Redis and Memcached

You are now ready to install postgres, redis, and memcached. Their pod configs are almost the same so I’ll just share the postgres yml here:

apiVersion: apps/v1
kind: Deployment
metadata:
name: sentry-pg
spec:
selector:
matchLabels:
app: sentry-pg
replicas: 1
template:
metadata:
labels:
app: sentry-pg
spec:
containers:
- name: sentry-pg
image: postgres:9.5
ports:
- containerPort: 5432
resources:
limits:
memory: "500Mi"
cpu: "0.5"
requests:
memory: "400Mi"
#Only for postgres, no need for both redis and memcache
volumeMounts:
- mountPath: "/var/lib/postgresql/data"
name: sentry-pg
subPath: postgres
volumes:
- name: sentry-pg
persipersistentVolumeClaim:
claimName: sentry-pg-disk

You can increase the value for resources depending on your cluster. The value for spec.containers.volumeMounts.name should be the same as the value for spec.volumes.name. Default port for redis is 6379 while on memcached its 11211. All you need to do next is to create them.

kubectl apply -f your-pg-pod.yml
kubectl apply -f your-redis-pod.yml
kubectl apply -f your-memcached-pod.yml

Once they are up and running, you will need to create service so sentry can connect to them. Their service configurations are the same:

kind: Service
apiVersion: v1
metadata:
name: service-sentry-pg
spec:
type: NodePort
selector:
app: sentry-pg
ports:
- protocol: TCP
port: 5432
targetPort: 5432

You will use the value for metadata.name when you create the pods of sentry. Create these services (which you should already know by now) then you should be done… with the storage at least.

# verify by running this command
# which should list the pods running in your cluster
# like the screenshot below but ignore the name
kubectl get pods

Once you verified that the pods for memcached, postgres, and redis are running, then give yourself a pat on the back because you are DONE with installing dependencies. Sentry set-up will be up next on Part 2. Stay tuned!

--

--