目錄
- 起點(diǎn)
- 搭建swarm
- 編寫docker-compose.yml
- 部署服務(wù)
- 感想
1. 起點(diǎn)
學(xué)習(xí)爬蟲有一段時(shí)間了叨咖,使用過(guò)Scrapy, 就想試試其它的爬蟲框架瘩例,選擇pyspider也是因?yàn)橄胪ㄟ^(guò)pyspider了解一下分布式爬蟲,由于docker技術(shù)的成熟甸各,也就順理成章的選擇docker來(lái)完成這件事垛贤。
2. 搭建swarm
創(chuàng)建節(jié)點(diǎn)
創(chuàng)建三個(gè)docker machine:
$ docker-machine create --driver virtualbox manager1
$ docker-machine create --driver virtualbox worker1
$ docker-machine create --driver virtualbox worker2
執(zhí)行如下命令可查看新創(chuàng)建的docker machine, 以及docker machine對(duì)用的IP地址
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v17.05.0-ce
manager1 - virtualbox Running tcp://192.168.99.101:2376 v17.05.0-ce
worker1 - virtualbox Running tcp://192.168.99.102:2376 v17.05.0-ce
worker2 - virtualbox Running tcp://192.168.99.103:2376 v17.05.0-ce
創(chuàng)建swarm
登陸 manager1:
$ docker-machine ssh manager1
執(zhí)行如下命令,創(chuàng)建一個(gè)新的swarm
docker@manager1:~$ docker swarm init --advertise-addr 192.168.99.101
Swarm initialized: current node (wpf2jcvhhvfosv3c9ac6c50dh) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-69wvyxsrnjtm11z38eus20tm0z9cof2ks9khzyv7fdo8it0dln-drdoszuykjp1uvhmn2spaa8vj \
192.168.99.101:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
將節(jié)點(diǎn)加入swarm
登陸worker1, 將worker1加入swarm:
docker@worker1:~$ docker swarm join \
> --token SWMTKN-1-69wvyxsrnjtm11z38eus20tm0z9cof2ks9khzyv7fdo8it0dln-drdoszuykjp1uvhmn2spaa8vj \
> 192.168.99.101:2377
This node joined a swarm as a worker.
登陸worker2, 將worker2加入swarm:
docker@worker2:~$ docker swarm join \
> --token SWMTKN-1-69wvyxsrnjtm11z38eus20tm0z9cof2ks9khzyv7fdo8it0dln-drdoszuykjp1uvhmn2spaa8vj \
> 192.168.99.101:2377
This node joined a swarm as a worker.
查看當(dāng)前swarm狀態(tài)
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
k926754fhudg5tu51rnlp2fdj worker2 Ready Active
q1seyrwugtdceqd515tmp8ph3 worker1 Ready Active
wpf2jcvhhvfosv3c9ac6c50dh * manager1 Ready Active Leader
至此趣倾,三個(gè)節(jié)點(diǎn)的swarm已經(jīng)創(chuàng)建完成聘惦。
3. 編寫docker-compose.yml
docker-compose.yml請(qǐng)參考
注意事項(xiàng)
- compose file使用version: 3
- 部署多replicas時(shí),不需要使用HAproxy的Load Balance儒恋,swarm服務(wù)本身自帶VIP部凑,如:
-
command中需要連接其它服務(wù)時(shí),配置參數(shù)不可寫在文件中碧浊,如scheduler需要連接mysql, redis等,不能使用config.json文件
command: -c config.json scheduler
在使用stack deploy時(shí)瘟仿,docker-compose file中 link, depends_on等命令被忽略箱锐,具體信息請(qǐng)參考compose file版本3
所有服務(wù)使用同一網(wǎng)絡(luò)段,如cars
-
如果需要連接遠(yuǎn)端mysql, redis修改連接地址即可劳较,如
command: '--taskdb "mysql+taskdb://root:root@10.208.20.94:3306/taskdb" --resultdb "mysql+resultdb://root:root@10.208.20.94:3306/resultdb" --projectdb "mysql+projectdb://root:root@10.208.20.94:3306/projectdb" --message-queue "redis://10.208.20.94:6379/db" webui --max-rate 10 --max-burst 3 --scheduler-rpc "http://scheduler:23333/" --fetcher-rpc "http://fetcher/"'
4. 部署服務(wù)
部署
登陸manager1, 執(zhí)行如下命令:
docker@manager1:~$ docker stack deploy -c docker-compose.yml myspider
注意事項(xiàng)
swarm服務(wù)部署沒(méi)有嚴(yán)格的順序驹止,所以會(huì)出現(xiàn)mysql, redis服務(wù)啟動(dòng)較晚,在service部署要設(shè)置restart_policy, 如
docker@manager1:~$ docker stack deploy -c docker-compose.yml myspider
Creating network myspider_cars
Creating service myspider_fetcher
Creating service myspider_processor
Creating service myspider_result-worker
Creating service myspider_webui
Creating service myspider_redis
Creating service myspider_mysql
Creating service myspider_scheduler
Creating service myspider_phantomjs
5. 感想
由于本身對(duì)swarm stack缺乏經(jīng)驗(yàn)观蜗,中途一度想放棄swarm, 轉(zhuǎn)而采用k8s臊恋,還好堅(jiān)持下來(lái),所以也就有了這篇記錄墓捻,列出了當(dāng)前踩的一些坑抖仅。