Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> เครื่องเสมือน

เชี่ยวชาญ Docker Swarm: คู่มือสำหรับผู้เริ่มต้น Clustering ด้วย Docker Engine

聽起來像 punk rock band แต่เป็น brand new orchestration mechanism หรือ improvement ของ orchestration ใน Docker สรุปสั้น: older Docker version manual setup Swarm create Docker clusters Starting 1.12 Docker engine native implementation seamless clustering setup เหตุผลที่เราอยู่ที่นี่

Tutorial นี้ลองให้ taste Docker orchestration ไม่ใช่ all-inclusive (bed & breakfast) หรือ all-knowing แต่มีสิ่งที่ embark clustering journey ตามมา

Technology Overview

Shame rehash detailed/useful Docker documentation article outline brief overview: Docker ถ้าอยากใช้ more than single server as Docker host แต่ want same logical entity = clustering

Start cluster of one initiate swarm on host = manager of cluster Technically consensus group of one node Mathematical logic: Raft algorithm Manager responsible scheduling tasks Tasks delegated to worker nodes once/if join swarm Governed by Node API Hate word API but must use here

Service API second component allows manager nodes create distributed services on all nodes in swarm Services replicated (spread across cluster balancing mechanisms) หรือ global (instance running each node)

More at work but good enough primed/pumped Hands-on stuff Target platform: CentOS 7.2 interesting เพราะเวลาทดสอบมีแค่ Docker 1.10 ใน repos ต้อง manually upgrade framework use swarm จะ discuss separate tutorial Follow-up guide: join new nodes asymmetric setup with Fedora Assume correct setup in place get cluster service up running

Setup Image & Service

Setup load-balanced Apache service multiple instances serving content via single IP standard Highlights typical reasons cluster config: availability, redundancy, horizontal scaling, performance ต้อง consider networking/storage แต่ beyond immediate scope

Dockerfile template available official repo under httpd minimal setup underway Details download images create own in intro guide linked beginning tutorial

docker build -t my-apache2 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM httpd:2.4
Trying to pull repository docker.io/library/httpd ...
2.4: Pulling from docker.io/library/httpd

8ad8b3f87b37: Pull complete
c95e1f92326d: Pull complete
96e8046a7a4e: Pull complete
00a0d292c371: Pull complete
3f7586acab34: Pull complete
Digest: sha256:3ad4d7c4f1815bd1c16788a57f81b413...a915e50a0d3a4
Status: Downloaded newer image for docker.io/httpd:2.4
---> fe3336dd034d
Step 2 : COPY ../public-html/ /usr/local/apache2/htdocs/
...

ก่อนไปต่อ start single instance see container created no errors connect Web server Establish that create distributed service

docker run -dit --name my-running-app my-apache2

Check IP address punch browser see what gives

Swarm Initiation and Setup

Next step: get swarm going basic command underway similar Docker blog example:

docker service create --name frontend --replicas 5 -p 80:80/tcp my-apache2:latest

What here? Create service frontend 5 container instances bind hostPort 80 containerPort 80 use fresh Apache image แต่ทำได้ error:

docker service create --name frontend --replicas 5 -p 80:80/tcp my-apache2:latest
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

Meaning: not setup current host/node as swarm manager Need init swarm or join existing Since don't have one yet initialize now:

docker swarm init
Swarm initialized: current node (dm58mmsczqemiikazbfyfwqpd) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join \
--token SWMTKN-1-4ofd46a2nfyvrqwu8w5oeetukrbylyznxla
9srf9vxkxysj4p8-eu5d68pu5f1ci66s7w4wjps1u \
10.0.2.15:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow instructions.

Output self-explanatory Created swarm New nodes need correct token join swarm IP address/port identified for firewall rules Can add managers too Rerun service create:

docker service create --name frontend --replicas 5 -p 80:80/tcp my-apache2:latest
6lrx1vhxsar2i50is8arh4ud1

Test Connectivity

Check service actually works Similar Vagrant/coreOS concepts almost identical different implementation Same idea Check docker ps right output Multiple replicas created service

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
cda532f67d55 my-apache2:latest "httpd-foreground"
2 minutes ago Up 2 minutes 80/tcp frontend.1.2sobjfchdyucschtu2xw6ms9a
75fe6e0aa77b my-apache2:latest "httpd-foreground"
2 minutes ago Up 2 minutes 80/tcp frontend.4.ag77qtdeby9fyvif5v6c4zcpc
3ce824d3151f my-apache2:latest "httpd-foreground"
2 minutes ago Up 2 minutes 80/tcp frontend.2.b6fqg6sf4hkeqs86ps4zjyq65
eda01569181d my-apache2:latest "httpd-foreground"
2 minutes ago Up 2 minutes 80/tcp frontend.5.0rmei3zeeh8usagg7fn3olsp4
497ef904e381 my-apache2:latest "httpd-foreground"
2 minutes ago Up 2 minutes 80/tcp frontend.3.7m83qsilli5dk8rncw3u10g5a

Tested different non-default ports works well Leeway connect server get response localhost or docker interface IP with correct port Example port 1080

(ภาพประกอบ)

Very rough simple beginning Real challenge: optimized scalable services but require proper technical use case Use docker info docker service (inspect|ps) commands learn cluster behavior

Possible Problems

May encounter small/not-so-small issues playing Docker/swarm e.g. SELinux complain illegal errors/warnings shouldn't impede much

(ภาพประกอบ)

Docker service is not a docker command

Run necessary command start replicated service error: docker: 'service' is not a docker command Means wrong Docker version (check -v) Fix in follow-up tutorial

docker service create --name frontend --replicas 5 -p 80:80/tcp my-apache2:latest
docker: 'service' is not a docker command.

Docker tag not recognized

May see error:

docker service create -name frontend -replicas 5 -p 80:80/tcp my-apache2:latest
Error response from daemon: rpc error: code = 3 desc = ContainerSpec: "-name" is not a valid repository/tag

Several discussion threads error may be innocent Copied command from browser hyphens not parsed correctly As simple as that

More Reading

More to say topic including Swarm implementation pre-Docker 1.12 current Docker engine version Please not lazy spend time reading:

  • Docker Swarm overview (standalone Swarm installations)
  • Build a Swarm cluster for production (standalone setups)
  • Install and create a Docker Swarm (standalone setups)
  • Docker engine swarm overview (version 1.12)
  • Getting started with swarm mode (version 1.12)

สรุป (Conclusion)

Nothing too grand at this point but believe useful Covers key concepts overview how swarm mode works what it does successfully downloaded/created own Web server image run several clustered instances Single node for now expand future Also tackled common problems

Hope interesting Combined previous Docker work decent understand images, networking stack, storage, now clusters Warming up Enjoy see you soon fresh new tutorials on Docker Just can't contain [sic] myself.

Cheers.