Docker之叢集服務
四、叢集服務
1.Compose
在registry節點利用compose相關知識編寫WordPress的建立檔案,使用compose版本為2,建立名為xd_db的服務,引用映象為mysql:latest,設定容器自動啟動執行,設定資料卷db_data與資料庫儲存路徑實現對映,設定資料庫密碼為wp_xiandian;建立服務為xd_wp,引用映象為wordpress:latest,此服務依賴xd_db服務,開放埠8888為wrodpress的80對映埠,最後設定wordpress的資料庫連線地址和密碼。完成後執行並檢視docker-compose.yml檔案,待容器執行完成後查詢docker的容器執行狀態,將以上操作命令及執行結果填入命令列介面。
# docker-compose up -d Creating opt_xd_db_1 Creating opt_xd_wp_1 # cat docker-compose.yml services: xd_db: image: mysql:latest volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: wp_xiandian xd_wp: depends_on: - xd_db image: wordpress:latest ports: - "8888:80" restart: always environment: WORDPRESS_DB_HOST: xd_db:3306 WORDPRESS_DB_PASSWORD: wp_xiandian volumes: db_data: # docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e955e781ac41 wordpress:latest "docker-entrypoint.sh" 5 minutes ago Up 5 minutes 0.0.0.0:8888->80/tcp opt_xd_wp_1 12a3d98cbd9d mysql:latest "docker-entrypoint.sh" 5 minutes ago Up 5 minutes 3306/tcp opt_xd_db_1 52745024501e ubuntu:14.04.3 "/bin/bash" 3 hours ago Up 3 hours reverent_bassi 49cb289d6e73 nginx:latest "/bin/bash" 23 hours ago Up 23 hours 0.0.0.0:32772->80/tcp, 0.0.0.0:32771->443/tcp nginxweb 0fa6b8c9ff49 mysql:latest "docker-entrypoint.sh" 24 hours ago Up 23 hours 0.0.0.0:32770->3306/tcp mysqldb 940568599bb2 192.168.200.12:5000/registry:latest "/entrypoint.sh /etc/" 5 weeks ago Up 23 hours 0.0.0.0:5000->5000/tcp registry |
2.Consult發現
(1)建立
在registry節點利用consul相關知識建立配置 consul 叢集,設定registry節點為cluster leader,將server和client節點加入該叢集身份為server,叢集名稱為xd_center,registry節點名稱為cluster_server,server節點名稱為cluster_client1,client節點為cluster_client2,完成後查詢該叢集列表,將以上操作命令及執行結果填入命令列介面。
# consul agent -server -data-dir /home/data_consul -client 0.0.0.0 -bind=192.168.200.10 -node=cluster_client1 -dc=xd_center & # consul agent -server -data-dir /home/data_consul -client 0.0.0.0 -bind=192.168.200.11 -node=cluster_client2 -dc=xd_center & # consul join 192.168.200.10 # consul join 192.168.200.11 # consul members 2017/03/29 11:24:33 [INFO] agent.rpc: Accepted client: 127.0.0.1:48197 Node Address Status Type Build Protocol DC cluster_client1 192.168.200.10:8301 alive server 0.6.3 2 xd_center cluster_client2 192.168.200.11:8301 alive server 0.6.3 2 xd_center cluster_server 192.168.200.12:8301 alive server 0.6.3 2 xd_center |
(2)查詢
在registry完成後查詢consul叢集列表,將以上操作命令及執行結果填入命令列介面。
# consul members 2017/03/29 11:24:33 [INFO] agent.rpc: Accepted client: 127.0.0.1:48197 Node Address Status Type Build Protocol DC cluster_client1 192.168.200.10:8301 alive server 0.6.3 2 xd_center cluster_client2 192.168.200.11:8301 alive server 0.6.3 2 xd_center cluster_server 192.168.200.12:8301 alive server 0.6.3 2 xd_center |