pyspider是Python中強大Web爬蟲框架,而且支持分佈式架構。python
在安裝pyspider時爬過一些坑,好比使用pip install pyspider時,python的版本要求在3.6及如下,由於async等已是python3.7的關鍵字;
使用git clone代碼安裝pyspider,python3 setup.py intall
,使用過程會遇到ssl證書的問題,總而言之,可能會遇到版本兼容問題。mysql
docker network create --driver bridge pyspider mkdir -p /volume1/docker/Pyspider/mysql/{conf,logs,data}/ /volume1/docker/Pyspider/redis/ docker run --network=pyspider --name redis -d -v /volume1/docker/Pyspider/redis:/data -p 6379:6379 redis docker run --network pyspider -p 33060:3306 --name pymysql -v /volume1/docker/Pyspider/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /volume1/docker/Pyspider/mysql/logs:/logs -v /volume1/docker/Pyspider/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root123 -d mysql docker run --network=pyspider --name scheduler -d -p 23333:23333 --restart=always binux/pyspider --taskdb "mysql+taskdb://pyspider:py1234@192.168.2.4:33060:33060/taskdb" --resultdb "mysql+projectdb://pyspider:py1234@192.168.2.4:33060:33060/resultdb" --projectdb "mysql+projectdb://pyspider:py1234@192.168.2.4:33060:33060/projectdb" --message-queue "redis://redis:6379/0" scheduler --inqueue-limit 10000 --delete-time 3600
使用docker-compose部署nginx
version: '2' services: phantomjs: image: 'binux/pyspider:latest' command: phantomjs cpu_shares: 256 environment: - 'EXCLUDE_PORTS=5000,23333,24444' expose: - '25555' # 暴露端口25555給link到此service的容器 mem_limit: 256m restart: always phantomjs-lb: image: 'dockercloud/haproxy:latest' # 使用haproxy使用負載均衡 links: - phantomjs volumes: - /var/run/docker.sock:/var/run/docker.sock # docker-compose v2版本中haproxy須要指定docker socket(MAC系統中) restart: always fetcher: image: 'binux/pyspider:latest' command: '--message-queue "redis://redis:6379/0" --phantomjs-proxy "phantomjs:80" fetcher --xmlrpc' # fetcher以rpc的方式啓動 cpu_shares: 256 environment: - 'EXCLUDE_PORTS=5000,25555,23333' links: - 'phantomjs-lb:phantomjs' mem_limit: 256m restart: always fetcher-lb: image: 'dockercloud/haproxy:latest' # 使用haproxy使用負載均衡 links: - fetcher volumes: - /var/run/docker.sock:/var/run/docker.sock # docker-compose v2版本中haproxy須要指定docker socket(MAC系統中) restart: always processor: image: 'binux/pyspider:latest' command: '--projectdb "mysql+projectdb://pyspider:py1234@192.168.2.4:33060/projectdb" --message-queue "redis://redis:6379/0" processor' cpu_shares: 256 mem_limit: 256m restart: always result-worker: image: 'binux/pyspider:latest' command: '--taskdb "mysql+taskdb://pyspider:py1234@192.168.2.4:33060/taskdb" --projectdb "mysql+projectdb://pyspider:py1234@192.168.2.4:33060/projectdb" --resultdb "mysql+resultdb://pyspider:py1234@192.168.2.4:33060/resultdb" --message-queue "redis://redis:6379/0" result_worker' cpu_shares: 256 mem_limit: 256m restart: always webui: image: 'binux/pyspider:latest' command: '--taskdb "mysql+taskdb://pyspider:py1234@192.168.2.4:33060/taskdb" --projectdb "mysql+projectdb://pyspider:py1234@192.168.2.4:33060/projectdb" --resultdb "mysql+resultdb://pyspider:py1234@192.168.2.4:33060/resultdb" --message-queue "redis://redis:6379/0" webui --max-rate 3 --max-burst 6 --scheduler-rpc "http://scheduler:23333/" --fetcher-rpc "http://fetcher/"' cpu_shares: 256 environment: - 'EXCLUDE_PORTS=24444,25555,23333' ports: - '15000:5000' # webui的對外的端口爲5000,能夠經過http://localhost:5000訪問webui服務。 links: - 'fetcher-lb:fetcher' # link到其它負載均衡haproxy的服務。 mem_limit: 256m restart: always webui-lb: image: 'dockercloud/haproxy:latest' links: - webui restart: always nginx: image: 'nginx' links: - 'webui-lb:HAPROXY' ports: - '5080:80' volumes: - /volume1/docker/Pyspider/nginx/nginx.conf:/etc/nginx/nginx.conf - /volume1/docker/Pyspider/nginx/conf.d/:/etc/nginx/conf.d/ - /volume1/docker/Pyspider/nginx/sites-enabled/:/etc/nginx/sites-enabled/ restart: always networks: default: external: name: pyspider #指定docker-compose的網絡接口爲:pyspider;實現和docker run方式建立容器的互通。
訪問url:
http://ip:15000git
若是想建立更多的fetcher, result_work, phantomjs容器實例,能夠使用: docker-compose scale phantomjs=2 processor=4 result-worker=2 docker-compose會自動幫你建立2個phantomjs服務,4個processor服務,2個result-worker服務;haproxy會自動實現負載均衡web
參考官方文檔redis