centos 7 에 pyspider 를 분산 배치 합 니 다.
14633 단어 pyspider
시스템 버 전:Linux centos-linux.shared 3.10.0-123.el7.x8664 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
python 버 전:Python 3.5.1
1.1.python 3 환경 구축:
본인 은 시도 후 통합 환경 Anaconda 를 선택 하 겠 습 니 다.
1.1.1.컴 파일
#
yum install -y ncurses-devel openssl openssl-devel zlib-devel gcc make glibc-devel libffi-devel glibc-static glibc-utils sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-deve
# python
wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
#
wget http://mirrors.sohu.com/python/3.5.1/Python-3.5.1.tgz
mv Python-3.5.1.tgz /usr/local/src;cd /usr/local/src
#
tar -zxf Python-3.5.1.tgz;cd Python-3.5.1
#
./configure --prefix=/usr/local/python3.5 --enable-shared
make && make install
#
ln -s /usr/local/python3.5/bin/python3 /usr/bin/python3
echo "/usr/local/python3.5/lib" > /etc/ld.so.conf.d/python3.5.conf
ldconfig
# python3
python3
# Python 3.5.1 (default, Oct 9 2016, 11:44:24)
# [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
# Type "help", "copyright", "credits" or "license" for more information.
# >>>
# pip
/usr/local/python3.5/bin/pip3 install --upgrade pip
ln -s /usr/local/python3.5/bin/pip /usr/bin/pip
# pip
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python get-pip.py
1.1.2.집적 환경 anaconda
# anaconda( )
wget https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh
#
./Anaconda3-4.2.0-Linux-x86_64.sh
# ,
yum install bzip2
1.2.mariaDB 설치
#
yum -y install mariadb mariadb-server
#
systemctl start mariadb
#
systemctl enable mariadb
#
mysql_secure_installation
#
mysql -u root -p
#
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'user_pass';
GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'localhost' WITH GRANT OPTION;
CREATE USER 'user_name'@'%' IDENTIFIED BY 'user_pass';
GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'%' WITH GRANT OPTION;
1.3.pyspider 설치본인 은 Anaconda 를 사용 합 니 다.
# sbird python 3.*
conda create -n sbird python=3*
#
source activate sbird
# pyspider
pip install pyspider
#
# it does not exist. The exported locale is "en_US.UTF-8" but it is not supported
# .bashrc
export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8
#ImportError: pycurl: libcurl link-time version (7.29.0) is older than compile-time version (7.49.0)
conda install pycurl
#
source deactivate sbird
# localhost:5000
systemctl stop firewalld.service
######### ==============
mkdir git;cd git
#
git clone https://github.com/binux/pyspider.git
#
/root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py
기타 방법
#
pip install virtualenv
mkdir python;cd python
# pyenv3
virtualenv -p /usr/bin/python3 pyenv3
#
cd pyenv3/
source ./bin/activate
pip install pyspider
# pycurl
yum install libcurl-devel
#
pip install pyspider
#
deactivate
본인 은 anaconda 방식 으로 설치 하 는 것 을 추천 합 니 다.pyspider 실행 중 오류 가 발생 하면 anaconda 설치 부분 을 참고 하여 localhost:5000 을 방문 하면 페이지 를 볼 수 있 습 니 다.
1.4.Supervisor 설치
#
yum install supervisor -y
# epel
vim /etc/yum.repos.d/epel.repo
#
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
http://mirrors.aliyuncs.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
http://mirrors.aliyuncs.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
http://mirrors.aliyuncs.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
#
yum install supervisor -y
#
echo_supervisord_conf
1.4.1.Supervisor 용법
supervisord #supervisor
supervisorctl # supervisor
# pyspider01
vim /etc/supervisord.d/pyspider01.ini
#
[program:pyspider01]
command = /root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/pyspider01.log
#
supervisorctl reload
#
supervisorctl start pyspider01
#
supervisord -c /etc/supervisord.conf
#
supervisorctl status
# output
pyspider01 RUNNING pid 4026, uptime 0:02:40
#
supervisorctl shutdown
1.5.redis 설치
# redis
mkdir download;cd download
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar xzf redis-3.2.4.tar.gz
cd redis-3.2.4
make
# yum
yum -y install redis
#
systemctl start redis.service
#
systemctl restart redis.service
#
systemctl stop redis.service
#
systemctl status redis.service
# /etc/redis.conf
vim /etc/redis.conf
#
daemonize no daemonize yes
bind 127.0.0.1 bind 10.211.55.22( ip)
# redis
systemctl restart redis.service
1.6.자체 시작 에 대하 여
# Supervisor
systemctl enable supervisord.service
# redis
systemctl enable redis.service
#
systemctl disable firewalld.service
이로써 pyspider 단일 서버 운행 환경 구축 및 배치 가 완료 되 었 고 localhost:5000 을 시작 하여 웹 인터페이스 에 들 어 갑 니 다.스 크 립 트 를 작성 하여 실행 할 수도 있 습 니 다./pyspider/supervisor/pyspider 01.log 에서 실행 상 태 를 볼 수 있 습 니 다.
2.분산 배치
방금 설 정 된 서버 는 centos 01 이 라 고 명명 하고 이러한 설정 에 따라 각각 두 대의 centos 02,centos 03 을 배치 합 니 다.
다음 과 같다.
서버 이름 ip 설명 하 다.
centos01 10.211.55.22 redis,mariaDB, scheduler
centos02 10.211.55.23 fetcher, processor, result_worker,phantomjs
centos03 10.211.55.24 fetcher, processor,,result_worker,webui
2.1.centos01서버 centos 01 에 들 어가 서 첫 번 째 단 계 를 거 쳐 기본 환경 이 설정 되 었 습 니 다.먼저 설정 파일/pyspider/config.json 을 편집 합 니 다.
{
"taskdb": "mysql+taskdb://user_name:[email protected]:3306/taskdb",
"projectdb": "mysql+projectdb://user_name:[email protected]:3306/projectdb",
"resultdb": "mysql+resultdb://user_name:[email protected]:3306/resultdb",
"message_queue": "redis://10.211.55.22:6379/db",
"logging-config": "/pyspider/logging.conf",
"phantomjs-proxy":"10.211.55.23:25555",
"webui": {
"username": "",
"password": "",
"need-auth": false,
"host":"10.211.55.24",
"port":"5000",
"scheduler-rpc":"http:// 10.211.55.22:5002",
"fetcher-rpc":"http://10.211.55.23:5001"
},
"fetcher": {
"xmlrpc":true,
"xmlrpc-host": "0.0.0.0",
"xmlrpc-port": "5001"
},
"scheduler": {
"xmlrpc":true,
"xmlrpc-host": "0.0.0.0",
"xmlrpc-port": "5002"
}
}
실행 시도:
/root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json scheduler
#
ImportError: No module named 'mysql'
# mysql-connector-python
cd ~/git/
git clone https://github.com/mysql/mysql-connector-python.git
#
source activate sbird
cd mysql-connector-python
python setup.py install
# redis
pip install redis
source deactivate
#
/root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json scheduler
# ok
[I 161010 15:57:25 scheduler:644] scheduler starting...
[I 161010 15:57:25 scheduler:779] scheduler.xmlrpc listening on 0.0.0.0:5002
[I 161010 15:57:25 scheduler:583] in 5m: new:0,success:0,retry:0,failed:0
실행 에 성공 하면 다음 과 같이/etc/supervisord.d/pyspider 01.ini 를 직접 변경 할 수 있 습 니 다.
[program:pyspider01]
command = /root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json scheduler
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/pyspider01.log
#
supervisorctl reload
#
supervisorctl status
centos 01 배치 완료.2.2.centos02
centos 02 에서 result 를 실행 해 야 합 니 다.worker、processor、phantomjs、fetcher
각각 파일 만 들 기:
/etc/supervisord.d/result_worker.ini
[program:result_worker]
command = /root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json result_worker
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/result_worker.log
/etc/supervisord.d/processor.ini
[program:processor]
command = /root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json processor
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/processor.log
/etc/supervisord.d/phantomjs.ini
[program:phantomjs]
command = /pyspider/phantomjs --config=/pyspider/pjsconfig.json /pyspider/phantomjs_fetcher.js 25555
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/phantomjs.log
/etc/supervisord.d/fetcher.ini
[program:fetcher]
command = /root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json fetcher
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/fetcher.log
pyspider 디 렉 터 리 에 pjsconfig.json 만 들 기
{
/*--ignore-ssl-errors=true */
"ignoreSslErrors": true,
/*--ssl-protocol=true */
"sslprotocol": "any",
/* Same as: --output-encoding=utf8 */
"outputEncoding": "utf8",
/* persistent Cookies. */
/*cookiesfile="e:/phontjscookies.txt",*/
cookiesfile="pyspider/phontjscookies.txt",
/* load image */
autoLoadImages = false
}
phantomjs 를/pyspider/폴 더 에 다운로드 하고 git/pyspider/pyspider/fetcher/phantomjsfetcher.js 를 phantomjs 로 복사fetcher.js
#
supervisorctl reload
#
supervisorctl status
# output
fetcher RUNNING pid 3446, uptime 0:00:07
phantomjs RUNNING pid 3448, uptime 0:00:07
processor RUNNING pid 3447, uptime 0:00:07
result_worker RUNNING pid 3445, uptime 0:00:07
centos 02 배치 완료.2.3.centos03
이 세 프로 세 스 fetcher,processor,result 배치worker 는 centos 02 와 마찬가지 로 본 서버 는 주로 앞 에 webui 를 추가 합 니 다.
파일 만 들 기:
/etc/supervisord.d/webui.ini
[program:webui]
command = /root/anaconda3/envs/sbird/bin/python /root/git/pyspider/run.py -c /pyspider/config.json webui
directory = /root/git/pyspider
user = root
process_name = %(program_name)s
autostart = true
autorestart = true
startsecs = 3
redirect_stderr = true
stdout_logfile_maxbytes = 500MB
stdout_logfile_backups = 10
stdout_logfile = /pyspider/supervisor/webui.log
#
supervisorctl reload
#
supervisorctl status
# output
fetcher RUNNING pid 2724, uptime 0:00:07
processor RUNNING pid 2725, uptime 0:00:07
result_worker RUNNING pid 2723, uptime 0:00:07
webui RUNNING pid 2726, uptime 0:00:07
3.총화방문http://10.211.55.24:5000하 시 면 됩 니 다.마음껏 오 르 세 요.