背景
为了捕获前端应用的用户信息以及前端应用异常,用于用户行为分析、前端异常分析。
主流的成熟实现方案及技术组件就是sentry,于是决定搭建一个sentry看一下。
在网上一顿搜索搭建方案主要分两类:
- 直接源码下载,编译,安装相关技术组件。(这个太麻烦了,就没仔细看。)
- 基于docker以及docker-compose搭建。
https://blog.csdn.net/Super_RD/article/details/94721819
https://blog.csdn.net/nimei31/article/details/80864370
但我们所使用的环境是docker+kubernetes。如果一些使用docker,一些使用kubernetes,不太便统一管理。所以就基于kubernetes搭建了一套sentry环境。一路上遇到一些问题,将相关的配置记录下来。以免不时所需。
备注:可以使用dompose组件,实现基于docker-compose.yml文件将容器部署在kubernetes上。但本人根据自己项目的实现情况,并没有选择此方案。
sentry部署方案
大致方案为:
- 单结点redis做数据缓存
- 单结点postgres做数据持久化
- 单结点sentry web作为Sentry消息上报的接口服务端
- 单结点sentry cron 服务探活
- 单结点sentry worker消息存储
sentry部署资源准备
下载镜像
docker pull redis:latest
docker pull postgres:latest
docker pull sentry:latest
通过docker inspect指令确认了一下对应组件的版本:
- redis -> 5.0.7
- postgres -> 12.1-1.pgdg100+1
- sentry -> 9.1.2
编写kubernetes相关部署文件
init.sh
创建一个新的命名空间
kubectl create namespace f-paas
sentry-redis-deployment.yaml
创建一个deployment,控制创建一个redis实例。并创建一个service服务,容器内的服务都可以使用service名称访问到它。
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sentry-redis-deployment
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry-redis
name: sentry-redis-server
spec:
replicas: 1
template:
metadata:
labels:
layer: f-paas
env: all
app: sentry-redis
name: sentry-redis-server
spec:
containers:
- name: sentry-redis
image: redis:latest
ports:
- containerPort: 6379
volumeMounts:
- mountPath: /redis-master-data
name: data
volumes:
- name: data
hostPath:
path: /mnt/vdc/containers/sentry-redis
---
kind: Service
apiVersion: v1
metadata:
name: redis
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry-redis
name: sentry-redis-server
spec:
ports:
- port: 6379
targetPort: 6379
selector:
layer: f-paas
env: all
app: sentry-redis
name: sentry-redis-server
sentry-postgres-deployment.yaml
创建一个deployment,控制创建一个postgres实例。并创建一个service服务,容器内的服务都可以使用service名称访问到它。
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sentry-postgres-deployment
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry-postgres
name: sentry-postgres-server
spec:
replicas: 1
template:
metadata:
labels:
layer: f-paas
env: all
app: sentry-postgres
name: sentry-postgres-server
spec:
containers:
- name: sentry-postgres
image: postgres:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5432
env:
- name: POSTGRES_PASSWORD
value: "secret"
- name: POSTGRES_USER
value: "sentry"
- name: POSTGRES_DBNAME
value: "sentry"
- name: POSTGRES_DBUSER
value: "sentry"
- name: POSTGRES_DBPASS
value: "secret"
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: data
volumes:
- name: data
hostPath:
path: /mnt/vdc/containers/sentry-postgres/data
---
kind: Service
apiVersion: v1
metadata:
name: postgres
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry-postgres
name: sentry-postgres-server
spec:
# type: NodePort
ports:
- port: 5432
targetPort: 5432
# nodePort: 6379
selector:
layer: f-paas
env: all
app: sentry-postgres
name: sentry-postgres-server
sentry-deployment.yaml
创建一个deployment,控制创建一个postgres实例。并创建一个node-type类型的service服务,可以通过宿主机的9000端口,对外提供访问服务。
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sentry-deployment
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry
name: sentry-server
spec:
replicas: 1
template:
metadata:
labels:
layer: f-paas
env: all
app: sentry
name: sentry-server
spec:
containers:
- name: sentry
image: sentry:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9000
env:
- name: SENTRY_SECRET_KEY
value: "${SENTRY_SECRET_KEY}"
- name: SENTRY_POSTGRES_HOST
value: "postgres"
- name: SENTRY_DB_PASSWORD
value: "secret"
- name: SENTRY_DB_USER
value: "sentry"
volumeMounts:
- mountPath: /var/lib/sentry/files
name: data
volumes:
- name: data
hostPath:
path: /mnt/vdc/containers/sentry/files
---
kind: Service
apiVersion: v1
metadata:
name: sentry-svc
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry
name: sentry-server
spec:
type: NodePort
ports:
- port: 9000
targetPort: 9000
nodePort: 9000
selector:
layer: f-paas
env: all
app: sentry
name: sentry-server
sentry-cron-deployment.yaml
创建一个deployment,控制创建一个postgres实例。
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sentry-cron-deployment
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry-cron
name: sentry-cron-server
spec:
replicas: 1
template:
metadata:
labels:
layer: f-paas
env: all
app: sentry-cron
name: sentry-cron-server
spec:
containers:
- name: sentry-cron
image: sentry:latest
imagePullPolicy: IfNotPresent
command: ["run","cron"]
env:
- name: SENTRY_SECRET_KEY
value: "${SENTRY_SECRET_KEY}"
- name: SENTRY_POSTGRES_HOST
value: "postgres"
- name: SENTRY_DB_PASSWORD
value: "secret"
- name: SENTRY_DB_USER
value: "sentry"
sentry-worker-deployment.yaml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sentry-worker-deployment
namespace: f-paas
labels:
layer: f-paas
env: all
app: sentry-worker
name: sentry-worker-server
spec:
replicas: 1
template:
metadata:
labels:
layer: f-paas
env: all
app: sentry-worker
name: sentry-worker-server
spec:
containers:
- name: sentry-worker
image: sentry:latest
imagePullPolicy: IfNotPresent
command: ["run","worker"]
env:
- name: SENTRY_SECRET_KEY
value: "${SENTRY_SECRET_KEY}"
- name: SENTRY_POSTGRES_HOST
value: "postgres"
- name: SENTRY_DB_PASSWORD
value: "secret"
- name: SENTRY_DB_USER
value: "sentry"
生成SENTRY密钥
在以上的kubernetes部署配置文件中会有一个变量${SENTRY_SECRET_KEY},该密钥可以用以下指令来生成,并替换该变量实际值。
docker run --rm sentry config generate-secret-key
部署sentry
下面通过执行相关的配置文件,完成部署。
- 执行init.sh创建kubernetes命名空间。
sh ./init.sh
- 创建redis容器
kubectl create -f ./sentry-redis-deployment.yaml
- 创建postgres容器
kubectl create -f ./sentry-postgres-deployment.yaml
- 创建sentry web容器
kubectl create -f ./sentry-postgres-deployment.yaml
注:由于sentry运行需要将redis中的缓存数据写入到postgres数据库中。
以及sentry web管理界面需要一些系统元数据的配置信息。
而这些信息都是存储在postgres数据库中的,所以在创建完成sentry web实例以后,我们需要进入到sentry web容器实例中,初始化数据库,具体操作如下:
- 获取sentry web容器podID:
kubectl get pod -n f-paas | grep sentry-deployment
NAME READY STATUS RESTARTS AGE
......
sentry-deployment-7c97947ff8-md664 1/1 Running 0 23m
......
- 进入到sentry web容器中:
kubectl exec -it -n f-paas sentry-deployment-7c97947ff8-md664 /bin/bash
- 在容器中执行upgrade命令:
root@sentry-deployment-7c97947ff8-md664:/# sentry upgrade
该命令会执行一系列的建表操作及相关日志的输出。
在些过程中会提示你输入sentry 系统超级管理员的用户名及密码。
可以在该提示下进行初始化,也可以不输,等后单独执行指令sentry createuser进行系统超级管理员的初始化。
- 创建 sentry cron容器
kubectl create -f ./sentry-cron-deployment.yaml
- 创建 sentry worker容器
kubectl create -f ./sentry-worker-deployment.yaml
到此sentry在kubernetes中的部署就完成了,执行指令查询一下:
[root@kubernetes ~]# kubectl get all -n f-paas
... ...
NAME READY STATUS RESTARTS AGE
po/sentry-cron-deployment-67bf4fb4dc-4xt6k 1/1 Running 0 17h
po/sentry-deployment-7c97947ff8-md664 1/1 Running 0 17h
po/sentry-postgres-deployment-cb549f846-svf88 1/1 Running 0 17h
po/sentry-redis-deployment-d65dc7f64-b75km 1/1 Running 0 17h
po/sentry-worker-deployment-d75986c78-lxh8n 1/1 Running 0 17h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/postgres ClusterIP 100.96.81.40 5432/TCP 17h
svc/redis ClusterIP 100.100.6.196 6379/TCP 17h
svc/sentry-svc NodePort 100.104.222.125 9000:9000/TCP 17h
访问sentry
通过http://IP:PORT/就可以访问sentry的web界面了。
enjoy it _