溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

docker快速搭建分布式爬蟲pyspider

發(fā)布時間:2020-08-28 11:32:14 來源:網(wǎng)絡(luò) 閱讀:4401 作者:喵來個魚 欄目:編程語言
簡介

pyspider是Python中強大Web爬蟲框架,并且支持分布式架構(gòu)。

為什么使用docker搭建pyspider

在安裝pyspider時爬過一些坑,比如使用pip install pyspider時,python的版本要求在3.6及以下,因為async等已經(jīng)是python3.7的關(guān)鍵字;
使用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: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部署

    • 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使用負(fù)載均衡
    links:
      - phantomjs
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # docker-compose v2版本中haproxy需要指定docker socket(MAC系統(tǒng)中)
    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使用負(fù)載均衡
    links:
      - fetcher
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # docker-compose v2版本中haproxy需要指定docker socket(MAC系統(tǒng)中)
    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服務(wù)。
    links:
      - 'fetcher-lb:fetcher' # link到其它負(fù)載均衡haproxy的服務(wù)。
    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的網(wǎng)絡(luò)接口為:pyspider;實現(xiàn)和docker run方式創(chuàng)建容器的互通。
  • 訪問url:
    http://ip:15000

  • web ui
    docker快速搭建分布式爬蟲pyspider

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

參考官方文檔

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI