docker快速搭建分佈式爬蟲pyspider

簡介

pyspider是Python中強大Web爬蟲框架,並且支持分佈式架構。

爲什麼使用docker搭建pyspider

在安裝pyspider時爬過一些坑,比如使用pip install pyspider時,python的版本要求在3.6及以下,因爲async等已經是python3.7的關鍵字;
使用git clone代碼安裝pyspider,python3 setup.py intall,使用過程會遇到ssl證書的問題,總而言之,可能會遇到版本兼容問題。

使用docker部署pyspider

  • docker的安裝不做說明;
  • 直接進入正題。
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:[email protected]:33060:33060/taskdb" --resultdb "mysql+projectdb://pyspider:[email protected]:33060:33060/resultdb" --projectdb "mysql+projectdb://pyspider:[email protected]:33060:33060/projectdb" --message-queue "redis://redis:6379/0" scheduler --inqueue-limit 10000 --delete-time 3600
  • 使用docker-compose部署

    • docker-compose.yml
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:[email protected]: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:[email protected]:33060/taskdb"  --projectdb "mysql+projectdb://pyspider:[email protected]:33060/projectdb" --resultdb "mysql+resultdb://pyspider:[email protected]: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:[email protected]:33060/taskdb"  --projectdb "mysql+projectdb://pyspider:[email protected]:33060/projectdb" --resultdb "mysql+resultdb://pyspider:[email protected]: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方式創建容器的互通。

如果想創建更多的fetcher, result_work, phantomjs容器實例,可以使用: docker-compose scale phantomjs=2 processor=4 result-worker=2 docker-compose會自動幫你創建2個phantomjs服務,4個processor服務,2個result-worker服務;haproxy會自動實現負載均衡

參考官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章