之前在 Docker篇(五):容器之間該如何通訊?中,講到了運行多個容器時的網路通信方式,但那些容器都是運行在同一臺物理機的
在實際專案中,我們往往需要部署多套軟體,比如組件需要使用集群化部署,或者一個專案程式本身就依賴了很多組件,為了存盤與運行效率等方面,往往需要跨主機部署,那么,該如何實作跨主機容器之間的網路通訊呢?
哎,你想到的,Docker 也想到啦,或者說本來就存在著一種通用的方案吧
一、理論:Docker 如何實作跨主機網路?
1、認識 Docker Overlay
Overlay 網路是為特定目的在物理(底層)網路之上創建的邏輯網路,Docker 可以創建容器之間的 Overlay 網路,從而實作跨主機的容器之間的溝通
也就是說,只要幾臺物理機之間本身是可以通信的,那么只要在這些機器上建立好一個 Overlay 網路把需要相互通訊的容器,直接部署在這個網路之上,最終的效果就類似于將這些容器部署在同一臺物理機一樣,進行任意通信啦
比如說我們需要實作一個 Elasticsearch 集群,那么我們只需要把各個節點部署在預先創建好的一個 Overlay 網路上就可以實作通信啦
2、稍微具體一點
你可能還會有疑惑,為什么 Overlay 網路就可以實作多臺物理機之間的網路互通呢?實際上它是在 Docker 集群節點間的加入了一層虛擬網路,它有獨立的虛擬網段,Docker 容器發送的請求,會先發送到虛擬子網,再由虛擬子網包裝為宿主機的真實網址進行發送
3、Swarm 是什么?
而今天要介紹的 Docker Swarm,則是 Docker Overlay 網路的一種簡易實作方式,它是 Docker 開發的容器集群管理工具, 與 Docker API 兼容性很好
并且 Linux 中安裝了 Docker,也默認會安裝 Swarm,因此,在這里,我們采用 Swarm 實作 集群間的網路通信
接下來,讓我們通過實戰真正了解一下使用Docker Swarm 如何實作Docker 容器的跨主機通信吧
二、實戰一:實作集群之間的通信
還是拿那個老例子來講一下
在文章Docker篇(五):容器之間該如何通訊?中,我們分別將 Spring Boot 后端程式 druid_demo 和 mariadb 分別運行在不同容器中,成功實作了他們之間的通訊
那么,接下來,我們將他們分別部署在兩臺機器上
機器配置如下:
| 序號 | 節點角色 | ip地址 | 容器名稱 |
|---|---|---|---|
| 1 | manager | 10.10.10.100 | druid_demo |
| 2 | worker | 10.10.10.88 | mariadb |
說明:使用 Swarm 建立跨主機的網路,實際上總共分為如下幾步:
-
在 manager 創建一個
Swarm集群 -
將其他集群分別加進來
-
在 manager 創建一個
Overlay網路 -
啟動各個容器時,指定這個
Overlay網路
具體,往下看
1、在 manager 節點創建 Swarm 集群
執行命令:
docker swarm init --advertise-addr=10.10.10.100
效果如下:
docker swarm init --advertise-addr=10.10.10.100
[root@localhost ~]# docker swarm init --advertise-addr=10.10.10.100
Swarm initialized: current node (maw28ll7mlxuwp47z5c5vo2v1) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2bdwwlo8xvdskil05itg66l63dpi931oqnt5gvehlnf1it1auo-2uvypbiu0hpcn1e06hrhctbe8 10.10.10.100:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
顯示了 worker節點加入到該集群的命令,只需要執行
docker swarm join --token SWMTKN-1-2bdwwlo8xvdskil05itg66l63dpi931oqnt5gvehlnf1it1auo-2uvypbiu0hpcn1e06hrhctbe8 10.10.10.100:2377
即可
2、在 worker 節點上執行命令,將自己加入集群
docker swarm join --token SWMTKN-1-2bdwwlo8xvdskil05itg66l63dpi931oqnt5gvehlnf1it1auo-2uvypbiu0hpcn1e06hrhctbe8 10.10.10.100:2377
![]()
image.png
3、在 manager 節點,查看當前網路集群的節點情況
執行 docker node ls
![]()
image.png
4、在 manager 節點,創建 overlay 網路
docker network create -d overlay --attachable demo
說明:
--attachable 宣告當前創建的網路為:其他節點可以訪問的網路
5、在 worker 節點的網路串列,是否多了這個網路
docker network ls
說明:正常情況,執行完第4步之后,這里就會在原來的基礎上多一個 demo 網路
6、在 worker 節點啟動 mariadb 容器,指定該 overlay 網路
sudo docker run -itd -v /usr/local/mysqldata:/var/lib/mysql -h mariadb --name=mariadb --net=demo --privileged=true mariadb:latest /sbin/init
說明:--net=demo :指定該集群間的網路(overlay ):demo
7、在 manager 節點,啟動 durid_demo 程式
接下來,在 manager 節點,啟動 Spring Boot 后端程式 druid_demo 的容器,并指定 demo 網路
docker run -it -p 8888:8080
-h druid_demo
--name druid_demo
--net=demo
--privileged=true druid_demo:0.0.1-SNAPSHOT /sbin/init
此時,請求介面,驗證網路是否互通
![]()
image.png
介面正常回傳,說明我們已經實作了 druid_demo 應用程式容器與 mariadb 容器之間的通訊
8、退出集群
執行 docker swarm leave,即可實作各節點退出集群網路的操作
那么,現在你知道 Docker 實作跨主機通訊的步驟了
接下來,趁熱打鐵,我們再一起來進行 Elasticsearch (后續簡稱為 ES)集群搭建,真正實踐一番
注意:
如果你之前沒怎么使用過 ES ,也沒有關系,我會推出相應的文章來介紹它,在這里,你只需要掌握 Docker 在多臺機器部署專案的方式以及實作跨主機通信的思想,我們的目的就達到啦
三、實戰二:Elasticsearch集群搭建
在進入集群搭建之前,我們先來看看如何啟動一個單機 ES
1、單機方式
docker run -itd --name elastic -v /usr/local/es/data:/var/lib/elasticsearch -v /usr/local/es/logs:/var/log/elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --restart=always elasticsearch:v7.9.1
說明:
1)-v :指定掛載目錄(分別將 ES 的資料目錄和日志目錄映射到宿主機上,這樣即使容器掛掉了再重啟,日志也不會丟失)
其中,在容器目錄的 /etc/elasticsearch/elasticsearch.yml 中分別配置了 path.data 和 path.logs ,分別為
/var/lib/elasticsearch
/usr/local/es/logs
2)-p:指定映射埠(將容器中的埠映射到了宿主機上)
3)-e:指定配置(指定es當前是以單個節點的方式啟動)
4)--restart=alway:總是會自動重啟
啟動之后,登錄 Es-Head ,發現可以連接上,那么表示啟動成功

image.png
單機模式很簡單,只要分別配置好掛載目錄path.data 和 path.logs ,指定為單機模式啟動就好了
集群模式相應的也不復雜,配置與單機模式基本一致,除了需要部署多臺臺物理機以外,再注意一下各節點之間的相互關聯方式就好了,而這相互關聯方式,則包括:網路的互通 和 確定集群的節點成員
2、集群方式
我們來搭建一個集群模式為 1 master + 3 data 節點,機器分配為:
| 序號 | 節點角色 | ip地址 |
|---|---|---|
| 1 | elastic-master | 10.10.10.88 |
| 2 | elastic-data01 | 10.10.10.76 |
| 3 | elastic-data02 | 10.10.10.77 |
| 4 | elastic-data03 | 10.10.10.78 |
1)配置集群網路 demo
對,就是參考第一步,為三臺機器配置集群網路
其中,網路節點角色如下:
| 序號 | 節點角色 | ip地址 |
|---|---|---|
| 1 | manager | 10.10.10.88 |
| 2 | worker | 10.10.10.76 |
| 3 | worker | 10.10.10.77 |
| 4 | worker | 10.10.10.78 |
2)分別修改各節點的組態檔 elasticsearch.yml
a. elastic-master
vi /usr/local/es/config/elastic-master/elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: elastic-master
node.master: true
node.data: fasle
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["elastic-master", "elastic-data01","elastic-data02","elastic-data03"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["elastic-master"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requi
http.cors.enabled: true
http.cors.allow-origin: "*"
說明:
-
cluster.name: my-application #集群名稱,各個節點保持一致即可
-
node.name: elastic-master #節點名稱,各個節點必須獨一無二
-
node.master: demo #是master節點
-
node.data: demo #不是data節點
-
path.data: /var/lib/elasticsearch #data存盤目錄(無特殊需求,直接使用默認的即可)
-
path.logs: /var/log/elasticsearch #日志存盤目錄(無特殊需求,直接使用默認的即可)
-
discovery.seed_hosts: ["elastic-master", "elastic-data01","elastic-data02","elastic-data03"]
#可發現的節點(配置四個節點的主機名稱或者ip即可,docker部署,建議直接配置主機名稱)
-
cluster.initial_master_nodes: ["elastic-master"] #初始化集群節點,直接配置master節點的主機名即可
b.elastic-data01
vi /usr/local/es/config/elastic-data01/elasticsearch.yml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: elastic-data01
node.master: false
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["elastic-master", "elastic-data01","elastic-data02","elastic-data03"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["elastic-master"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requi
http.cors.enabled: true
http.cors.allow-origin: "*"
說明:
a. elastic-data01 的組態檔,與 elastic-master 的組態檔中,只有以下三項內容不同,其余均相同
node.name: elastic-data01 #節點名稱,各個節點必須獨一無二
node.master: false #不是master節點
node.data: true #是data節點
b. elastic-data02、elastic-data03 的配置,與 elastic-data01 的配置,除了 node.name 的值不一致以外,其余均相同
3)啟動
分別在各個主機上,執行 docker 啟動命令,進行各個節點的啟動
docker run -itd --name elastic-master -h elastic-master --net=demo -v /usr/local/es/data:/var/lib/elasticsearch -v /usr/local/es/logs:/var/log/elasticsearch -v /usr/local/es/plugins:/usr/share/elasticsearch/plugins -v /usr/local/es/config/elastic-master/elasticsearch.yml:/etc/elasticsearch/elasticsearch.yml:ro -p 9200:9200 -p 9300:9300 -e cluster.initial_master_nodes=elastic-master --restart=always elasticsearch:v7.9.1 /sbin/init
docker run -itd --name elastic-data01 -h elastic-data01 --net=demo -v /usr/local/es/data:/var/lib/elasticsearch -v /usr/local/es/logs:/var/log/elasticsearch -v /usr/local/es/plugins:/usr/share/elasticsearch/plugins -v /usr/local/es/config/elastic-data01/elasticsearch.yml:/etc/elasticsearch/elasticsearch.yml:ro -p 9200:9200 -p 9300:9300 -e cluster.initial_master_nodes=elastic-master --restart=always elasticsearch:v7.9.1 /sbin/init
docker run -itd --name elastic-data02 -h elastic-data02 --net=demo -v /usr/local/es/data:/var/lib/elasticsearch -v /usr/local/es/logs:/var/log/elasticsearch -v /usr/local/es/plugins:/usr/share/elasticsearch/plugins -v /usr/local/es/config/elastic-data02/elasticsearch.yml:/etc/elasticsearch/elasticsearch.yml:ro -p 9200:9200 -p 9300:9300 -e cluster.initial_master_nodes=elastic-master --restart=always elasticsearch:v7.9.1 /sbin/init
docker run -itd --name elastic-data03 -h elastic-data03 --net=demo -v /usr/local/es/data:/var/lib/elasticsearch -v /usr/local/es/logs:/var/log/elasticsearch -v /usr/local/es/plugins:/usr/share/elasticsearch/plugins -v /usr/local/es/config/elastic-data03/elasticsearch.yml:/etc/elasticsearch/elasticsearch.yml:ro -p 9200:9200 -p 9300:9300 -e cluster.initial_master_nodes=elastic-master --restart=always relasticsearch:v7.9.1 /sbin/init
4)驗證
請求地址:http://10.10.10.88:9200/_cat/nodes 查看節點串列
![]()
image.png
可以看到,集群中,如期展示了 4 個節點,其中 1 個 master 節點和 3 個 data 節點(帶 * 號的為 master 節點)
集群成功搭建完畢!可喜可賀!
5)專案中集成 ES 配置
經測驗,只需要配置一個 master 節點的地址即可
spring.elasticsearch.rest.uris=http://10.10.10.88:9200
注:這里只是說明了如何搭建一個 ES 集群,實作集群間網路通信的方式,后面會專門有文章,介紹 ES 的理論知識,與具體實戰知識,敬請期待
四、總結
好了,總得來說,Docker 是通過搭建 Overlay 網橋,然后把各個主機的容器都統一放在這個網橋上就實作了 Docker 容器的跨主機通訊
恭喜你又掌握了一個新技能,當然,如果想要真正掌握的話,也建議大家真正實戰一次,畢竟實戰與理解是不同階段,對于知識的掌握程度也是不同級別的哦
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290599.html
標籤:其他
