部署Sentry

个人建议: 不能全看博文,以及参考文章,,主要以官方文档为主..

安装Redis

redis如何安装这里不再赘述了..小伙伴,自行百度吧..

安装postgresql(python安装方式)

查询版本点击这里自行查找自己的版本

lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.4.1708 (Core)
Release:    7.4.1708
Codename:   Core

检查本机包情况

yum list | grep postgres
postgresql.i686                             9.2.23-3.el7_4             updates
postgresql.x86_64                           9.2.23-3.el7_4             updates
postgresql-contrib.x86_64                   9.2.23-3.el7_4             updates
postgresql-devel.i686                       9.2.23-3.el7_4             updates
postgresql-devel.x86_64                     9.2.23-3.el7_4             updates
postgresql-docs.x86_64                      9.2.23-3.el7_4             updates
postgresql-jdbc.noarch                      42.2.2-1.rhel7             pgdg10
postgresql-jdbc-javadoc.noarch              42.2.2-1.rhel7             pgdg10
postgresql-libs.i686                        9.2.23-3.el7_4             updates
postgresql-libs.x86_64                      9.2.23-3.el7_4             updates
postgresql-odbc.x86_64                      09.03.0100-2.el7           base
postgresql-plperl.x86_64                    9.2.23-3.el7_4             updates
postgresql-plpython.x86_64                  9.2.23-3.el7_4             updates

这里其实是有一部分包的,不过版本都显的略低了

安装9.6版本的postgres

其实现在已经有10.0版本了,但是以python安装的话,会出现以下错误

Error: could not determine PostgreSQL version from '10.3'

安装依赖

yum install gcc libffi-devel python-devel openssl-devel gcc-c++

安装postgresql 9.6 版本

wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-redhat96-9.6-3.noarch.rpm

rpm -ivh ./pgdg-redhat96-9.6-3.noarch.rpm

yum list | grep postgres

yum install postgresql96 postgresql96-devel postgresql96-libs postgresql96-server postgresql96-contrib

设置pg_config软连

ln -s /usr/pgsql-9.6/bin/pg_config /usr/bin/pg_config

检测是否安装成功

rpm -aq| grep postgres
postgresql96-9.6.8-1PGDG.rhel7.x86_64
postgresql96-contrib-9.6.8-1PGDG.rhel7.x86_64
postgresql96-devel-9.6.8-1PGDG.rhel7.x86_64
postgresql96-libs-9.6.8-1PGDG.rhel7.x86_64
postgresql96-server-9.6.8-1PGDG.rhel7.x86_64

初始化postgresql

/usr/pgsql-9.6/bin/postgresql96-setup initdb
Initializing database ... OK

加入开机自启

systemctl enable postgresql-9.6

修改配置

将相关连接的 Method 改为 trust

vim /var/lib/pgsql/9.6/data/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

启动服务

systemctl start postgresql-9.6

安装Redis

测试是否能进入数据库参考这篇文章

安装Sentry

安装pip

sudo yum -y install epel-release
yum install python-pip

安装virtualenv

pip install -U virtualenv
virtualenv /mnt/www/sentry/

安装sentry

source /mnt/www/sentry/bin/activate
pip install -U sentry

配置Sentry

初始化配置,如果不设置路径的话,默认会生成在 ~/.sentry 目录中,推荐使用默认路径。初始化配置包含两个文件,分别是 config.yml 和 sentry.conf.py

sentry init /etc/sentry

配置postgres数据库(sentry.conf.py)

DATABASES = {
    'default': {
        'ENGINE': 'sentry.db.postgres',
        'NAME': 'sentry',
        'USER': 'postgres',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
        'AUTOCOMMIT': True,
        'ATOMIC_REQUESTS': False,
    }
}

修改端口(sentry.conf.py)

SENTRY_WEB_PORT = 9100

配置邮件(config.yml)

邮件配置项保存在 config.yml 文件中,Sentry 收到异常时会给自己的邮箱发送邮件通知,所以需要配置下邮件,当然也可以不启用,但不建议这样做。

mail.backend: 'smtp'  # Use dummy if you want to disable email entirely
mail.host: 'smtp.qq.com'
mail.port: 587
mail.username: '3211028@qq.com'
mail.password: 'zzzz'
mail.use-tls: true
# The email address to send on behalf of
mail.from: '3211028@qq.com'

创建数据库

createdb -U postgres -E utf-8 sentry

执行数据更新

执行数据更新,数据填充完成后会提示是否创建用户,可以选择不创建后续再创建,另外,这一步比较 耗内存,我就是在执行这一步的时候出了很多意外,比如进程被 killed,报很多异常等等。

SENTRY_CONF=/etc/sentry sentry upgrade
Would you like to create a user account now? [Y/n]: y
Email: 3211028@qq.com
Password:
Repeat for confirmation:
Should this user be a superuser? [y/N]: y
User created: 3211028@qq.com

创建用户

创建用户,需要填写邮箱和密码,创建好后会提示是否作为超级用户,根据自己情况选择即可。

SENTRY_CONF=/etc/sentry sentry createuser

开启sentry服务

开启 web 服务

开启后可以访问 http://[IP或域名]:[前面配置的端口] 进行访问。

因为我们用的是````,所以每次启动都需要

SENTRY_CONF=/etc/sentry sentry run web

开启后台 worker 服务

SENTRY_CONF=/etc/sentry sentry run worker

开启 cron 进程

SENTRY_CONF=/etc/sentry sentry run cron

Laravel集成扩展包

Sentry官方针对各种语言的不同框架都提供了相应的扩展包,当然也包括 laravel。

supervisor挂起服务

[program:sentry-worker]
user=sentry
directory=/www/sentry/
environment=SENTRY_CONF="/etc/sentry"
command=/www/sentry/bin/sentry run worker -l WARNING
autostart=true
autorestart=true
redirect_stderr=true
killasgroup=true
stdout_logfile=/var/log/sentry/sentry-worker.log
stderr_logfile=/var/log/sentry/sentry-worker.log

注意:cron服务一定不能指定用户运行,否则会有以下错误

04:17:03 [ERROR] sentry.errors.serializer: DbfilenameShelf instance has no attribute 'dict'
Traceback (most recent call last):
  File "/www/sentry/lib/python2.7/site-packages/celery/apps/beat.py", line 112, in start_scheduler
    beat.start()
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 454, in start
    humanize_seconds(self.scheduler.max_interval))
  File "/www/sentry/lib/python2.7/site-packages/kombu/utils/__init__.py", line 325, in __get__
    value = obj.__dict__[self.__name__] = self.__get(obj)
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 494, in scheduler
    return self.get_scheduler()
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 489, in get_scheduler
    lazy=lazy)
  File "/www/sentry/lib/python2.7/site-packages/celery/utils/imports.py", line 53, in instantiate
    return symbol_by_name(name)(*args, **kwargs)
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 358, in __init__
    Scheduler.__init__(self, *args, **kwargs)
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 185, in __init__
    self.setup_schedule()
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 372, in setup_schedule
    self._remove_db()
  File "/www/sentry/lib/python2.7/site-packages/celery/beat.py", line 363, in _remove_db
    os.remove(self.schedule_filename + suffix)
OSError: [Errno 1] Operation not permitted: '/tmp/sentry-celerybeat'
04:17:03 [CRITICAL] celery.beat: beat raised exception <type 'exceptions.OSError'>: OSError(1, 'Operation not permitted')
celery beat v3.1.18 (Cipater) is starting.

问题

Error: pg_config executable not found.

ln -s /usr/pgsql-9.6/bin/pg_config /usr/bin/pg_config

command 'gcc' failed with exit status 1

gcc: error trying to exec 'cc1plus': execvp: 没有那个文件或目录
    error: command 'gcc' failed with exit status 1
yum install gcc-c++

worker accepts messages serialized with pickle is a very bad idea!....

(sentry) ➜  sentry sentry run worker
01:45:10 [INFO] sentry.bgtasks: bgtask.spawn (task_name=u'sentry.bgtasks.clean_dsymcache:clean_dsymcache')
Running a worker with superuser privileges when the
worker accepts messages serialized with pickle is a very bad idea!

If you really want to continue then you have to set the C_FORCE_ROOT
environment variable (but please think about this before you do).

User information: uid=0 euid=0 gid=0 egid=0

说不建议使用root用户启动,,,所以才有了下面supervisor挂起的时候新建了个sentry用户

基于docker

docker安装和python的优缺点

  • docker安装简单,粗暴,配置少
  • python安装灵活,内存占用少

docker安装占用内存


➜  onpremise git:(master) ✗ free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        1.1G        224M         25M        526M        539M
Swap:            0B          0B          0B

python安装占用内存

(sentry) ➜  sentry free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        557M        839M         29M        442M        1.0G
Swap:            0B          0B          0B

nginx反代

server{
    listen   80;
    server_name sentry.iluoy.com;

location / {
      proxy_pass         http://localhost:9100;
      proxy_redirect     off;

      proxy_set_header   Host              $host;
      proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

docker-compose部署

修改邮箱配置

修改.env

SENTRY_SERVER_EMAIL=xxxx
SENTRY_EMAIL_HOST=smtp.exmail.qq.com
SENTRY_EMAIL_USER=xxxx
SENTRY_EMAIL_PASSWORD=xxxx
SENTRY_EMAIL_PORT=465
SENTRY_EMAIL_USE_TLS=true

重启服务

sudo docker-compose stop
sudo docker-compose down
sudo docker-compose build
sudo docker-compose up -d

Connection unexpectedly closed: timed out

参考文章

参考文章