swarm 发布服务

1、发布服务

2、发布服务

服务发现:Swarm模式内置DNS组件,自动为每个服务分配DNS记录,然后服务的DNS名称在集群内的服务直接分发请求。
负载均衡:在Swarm集群中创建服务时,Ingress网络会自动为其分配一个虚拟IP(VIP),在DNS解析时返回VIP,流入该VIP的流量将自动发送(IPVS)该服务的所以健康任务(容器)。

 3、服务发现与负载均衡

 docker service create 
  --name my-web 
  --publish published=8080,target=80 
  --replicas 2 
  nginx


4、访问测试

可以看到每个节点都可以访问

获取虚拟ip

[root@manager ~]# docker service inspect -f '{{json .Endpoint.VirtualIPs}}' my-web
[{"NetworkID":"jni0oot2t4vmpk6tip3fkj4w3","Addr":"10.255.0.5/16"}]

配置文件存储

https://docs.docker.com/engine/swarm/ingress/#using-the-routing-mesh

1、生成一个基本的Nginx配置文件
# cat site.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
2、将site.conf保存到docker配置中
# docker config create site.conf site.conf
# docker config ls
3、创建一个Nginx并应用这个配置
# docker service create
--name nginx
--config source=site.conf,target=/etc/nginx/conf.d/site.conf
--publish 8080:80
nginx

原文地址:https://www.cnblogs.com/hellojackyleon/p/8383770.html