Docker篇(七): 如何实现 Docker 容器 的跨主机通讯?
nanshan 2024-12-28 14:31 16 浏览 0 评论
大家好,我是杰哥
之前在 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地址容器名称1manager10.10.10.100druid_demo2worker10.10.10.88mariadb
说明:使用 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地址1elastic-master10.10.10.882elastic-data0110.10.10.763elastic-data0210.10.10.774elastic-data0310.10.10.78
1)配置集群网络 demo
对,就是参考第一步,为三台机器配置集群网络
其中,网络节点角色如下:
序号节点角色ip地址1manager10.10.10.882worker10.10.10.763worker10.10.10.774worker10.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 容器的跨主机通讯
恭喜你又掌握了一个新技能。当然,如果想要真正掌握的话,也建议大家真正实战一次,毕竟实战与理解是不同阶段,对于知识的掌握程度也是不同级别的哦
嗯,就这样。每天学习一点,时间会见证你的强大~
相关推荐
- 0722-6.2.0-如何在RedHat7.2使用rpm安装CDH(无CM)
-
文档编写目的在前面的文档中,介绍了在有CM和无CM两种情况下使用rpm方式安装CDH5.10.0,本文档将介绍如何在无CM的情况下使用rpm方式安装CDH6.2.0,与之前安装C5进行对比。环境介绍:...
- ARM64 平台基于 openEuler + iSula 环境部署 Kubernetes
-
为什么要在arm64平台上部署Kubernetes,而且还是鲲鹏920的架构。说来话长。。。此处省略5000字。介绍下系统信息;o架构:鲲鹏920(Kunpeng920)oOS:ope...
- 生产环境starrocks 3.1存算一体集群部署
-
集群规划FE:节点主要负责元数据管理、客户端连接管理、查询计划和查询调度。>3节点。BE:节点负责数据存储和SQL执行。>3节点。CN:无存储功能能的BE。环境准备CPU检查JDK...
- 在CentOS上添加swap虚拟内存并设置优先级
-
现如今很多云服务器都会自己配置好虚拟内存,当然也有很多没有配置虚拟内存的,虚拟内存可以让我们的低配服务器使用更多的内存,可以减少很多硬件成本,比如我们运行很多服务的时候,内存常常会满,当配置了虚拟内存...
- 国产深度(deepin)操作系统优化指南
-
1.升级内核随着deepin版本的更新,会自动升级系统内核,但是我们依旧可以通过命令行手动升级内核,以获取更好的性能和更多的硬件支持。具体操作:-添加PPAs使用以下命令添加PPAs:```...
- postgresql-15.4 多节点主从(读写分离)
-
1、下载软件[root@TX-CN-PostgreSQL01-252software]#wgethttps://ftp.postgresql.org/pub/source/v15.4/postg...
- Docker 容器 Java 服务内存与 GC 优化实施方案
-
一、设置Docker容器内存限制(生产环境建议)1.查看宿主机可用内存bashfree-h#示例输出(假设宿主机剩余16GB可用内存)#Mem:64G...
- 虚拟内存设置、解决linux内存不够问题
-
虚拟内存设置(解决linux内存不够情况)背景介绍 Memory指机器物理内存,读写速度低于CPU一个量级,但是高于磁盘不止一个量级。所以,程序和数据如果在内存的话,会有非常快的读写速度。但是,内存...
- Elasticsearch性能调优(5):服务器配置选择
-
在选择elasticsearch服务器时,要尽可能地选择与当前业务量相匹配的服务器。如果服务器配置太低,则意味着需要更多的节点来满足需求,一个集群的节点太多时会增加集群管理的成本。如果服务器配置太高,...
- Es如何落地
-
一、配置准备节点类型CPU内存硬盘网络机器数操作系统data节点16C64G2000G本地SSD所有es同一可用区3(ecs)Centos7master节点2C8G200G云SSD所有es同一可用区...
- 针对Linux内存管理知识学习总结
-
现在的服务器大部分都是运行在Linux上面的,所以,作为一个程序员有必要简单地了解一下系统是如何运行的。对于内存部分需要知道:地址映射内存管理的方式缺页异常先来看一些基本的知识,在进程看来,内存分为内...
- MySQL进阶之性能优化
-
概述MySQL的性能优化,包括了服务器硬件优化、操作系统的优化、MySQL数据库配置优化、数据库表设计的优化、SQL语句优化等5个方面的优化。在进行优化之前,需要先掌握性能分析的思路和方法,找出问题,...
- Linux Cgroups(Control Groups)原理
-
LinuxCgroups(ControlGroups)是内核提供的资源分配、限制和监控机制,通过层级化进程分组实现资源的精细化控制。以下从核心原理、操作示例和版本演进三方面详细分析:一、核心原理与...
- linux 常用性能优化参数及理解
-
1.优化内核相关参数配置文件/etc/sysctl.conf配置方法直接将参数添加进文件每条一行.sysctl-a可以查看默认配置sysctl-p执行并检测是否有错误例如设置错了参数:[roo...
- 如何在 Linux 中使用 Sysctl 命令?
-
sysctl是一个用于配置和查询Linux内核参数的命令行工具。它通过与/proc/sys虚拟文件系统交互,允许用户在运行时动态修改内核参数。这些参数控制着系统的各种行为,包括网络设置、文件...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- linux 查询端口号 (58)
- docker映射容器目录到宿主机 (66)
- 杀端口 (60)
- yum更换阿里源 (62)
- internet explorer 增强的安全配置已启用 (65)
- linux自动挂载 (56)
- 禁用selinux (55)
- sysv-rc-conf (69)
- ubuntu防火墙状态查看 (64)
- windows server 2022激活密钥 (56)
- 无法与服务器建立安全连接是什么意思 (74)
- 443/80端口被占用怎么解决 (56)
- ping无法访问目标主机怎么解决 (58)
- fdatasync (59)
- 405 not allowed (56)
- 免备案虚拟主机zxhost (55)
- linux根据pid查看进程 (60)
- dhcp工具 (62)
- mysql 1045 (57)
- 宝塔远程工具 (56)
- ssh服务器拒绝了密码 请再试一次 (56)
- ubuntu卸载docker (56)
- linux查看nginx状态 (63)
- tomcat 乱码 (76)
- 2008r2激活序列号 (65)