安装Docker
● Ubuntu:https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/ ● CentOS:https://docs.docker.com/engine/installation/linux/docker-ce/centos/ 注:Docker安装需要网络
1、卸载旧版本的Docker
$ sudo apt-get remove docker docker-engine docker.io
2、设置存储库
更新apt包索引
$ sudo apt-get update
安装软件包以允许apt通过HTTPS使用存储库:
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
添加Docker的官方GPG密钥:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
指定稳定版本的存储库
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
3、安装Docker CE
更新apt包索引。
$ sudo apt-get update
安装最新版本的Docker CE
$ sudo apt-get install docker-ce
通过运行hello-world 映像验证Docker CE是否正确安装。
$ sudo docker run hello-world
4、卸载Docker CE
卸载Docker CE包:
$ sudo apt-get purge docker-ce
主机上的图像,容器,卷或自定义配置文件不会自动删除。删除所有图像,容器和卷:
$ sudo rm -rf /var/lib/docker
配置私有镜像仓库
1、创建或者编辑 /etc/docker/daemon.json
2、 指定私有仓库地址
[root@localhost ~]# echo '{ "insecure-registries":["10.128.222.245:5000"] }' > /etc/docker/daemon.json
[root@localhost ~]# cat /etc/docker/daemon.json
{ "insecure-registries":["仓库服务器IP:5000"] }
3、重载docker
# systemctl restart docker($sudo service docker restart)
创建集群
开放相关端口
firewall-cmd --zone=public --add-port=2377/tcp --permanent
firewall-cmd --zone=public --add-port=7946/tcp --permanent
firewall-cmd --zone=public --add-port=7946/udp --permanent
firewall-cmd --zone=public --add-port=4789/tcp --permanent
firewall-cmd --zone=public --add-port=4789/udp --permanent
firewall-cmd --reload
创建自定义docker_gwbridge网络
docker network create --subnet 192.168.18.0/24 \
--opt com.docker.network.bridge.name=docker_gwbridge \
--opt com.docker.network.bridge.enable_icc=false \
--opt com.docker.network.bridge.enable_ip_masquerade=true \
--internal=false \
docker_gwbridge
创建集群
#命令格式: docker swarm init --listen-addr <MANAGER-IP>:<PORT>
例:
[root@centos-web ~]# docker swarm init --listen-addr 172.18.30.29:2377
Swarm initialized: current node (a60d5c3ttymvtozr46uvk17q4) is now a manager.
查看集群
# docker node ls
ID HOSTNAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS
a60d5c3ttymvtozr46uvk17q4 * centos-web Accepted Ready Active Leader
获取加入集群命令token
#docker swarm join-token manager
docker swarm join \
--token ********* \
172.18.30.29:2377
子节点加入集群
docker swarm join \
--token ********* \
172.18.30.29:2377
删除集群子节点
docker swarm leave --force(节点上)
docker node rm --force(manager上)
创建一个overlay (docker-net)跨主机网络
docker network create --driver overlay --subnet 192.168.1.0/24 --gateway 192.168.1.1 docker-net
- --driver 网络类型
- --subnet 网段
- --gatway 网关
查看网络
docker network ls
NETWORK ID NAME DRIVER SCOPE
5cd6e605ad37 bridge bridge local
4mgivfebxqle docker-net overlay swarm
e2eb737866f1 docker_gwbridge bridge local
b617a9638d1f host host local
6qpbjzglknuy ingress overlay swarm
9a0834cc2206 none null local
发布服务
在自定义的跨主机overlay 网络(docker-net)上创建应用
docker service create --name eureka-server --publish 8761:8761 --replicas 3 --network docker-net 10.128.222.245:5000/eureka-server
- –replicas 副本数量
- –publish 服务发现,端口映射
- –mount 挂载文件
- –name service名称
查看服务
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
5inifvhzzz4m viz replicated 1/1 10.128.222.245:5000/dockersamples/visualizer:latest *:8080->8080/tcp
mbn6zaf5eenj eureka-server replicated 3/3 10.128.222.245:5000/eureka-server:latest *:8761->8761/tcp
ri3gfnqsyvu3 zuul-server replicated 1/1 10.128.222.245:5000/zuul-server:latest *:8081->8081/tcp
slobuh0j6qzi ydmh-api-zdhd replicated 1/1 10.128.222.245:5000/ydmh-api-zdhd:latest *:2219->2219/tcp
其他
docker service ls 查看集群列表
docker service ps server-name 查看集群下所有节点状态
docker service rm server-name 删除集群
docker service inspect --pretty server-name 集群属性
docker service scale server-name=number #扩容集群节点数量
测试服务网络
测试集群节点是否互通
查看manager节点上正在运行的docker 容器
docker@manager:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8266d738d2fc 10.128.222.245:5000/dockersamples/visualizer:latest "npm start" 5 hours ago Up 5 hours (healthy) 8080/tcp viz.1.7ykr77iyq3ji4ofw986yaltsg
48f74f5b6993 10.128.222.245:5000/eureka-server:latest "java -Djava.secur..." 21 hours ago Up 21 hours eureka-server.3.it46m4qld9yutv4j4tl2iqobf
查看work节点上正在运行的docker容器
docker@worker2:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e54adaed4314 10.128.222.245:5000/ydmh-api-zdhd:latest "java -Djava.secur..." 6 hours ago Up 6 hours ydmh-api-zdhd.1.vly3iwnrmszwphu5z3t9hh9bs
5590db969ca5 10.128.222.245:5000/eureka-server:latest "java -Djava.secur..." 21 hours ago Up 21 hours eureka-server.2.v6rphszffshqm31h3kau3db5d
测试manager节点上的容器和work节点上的容器能否通信
docker@manager:~$ docker exec -ti 48f74f5b6993 sh
/ # ping ydmh-api-zdhd.1.vly3iwnrmszwphu5z3t9hh9bs
PING ydmh-api-zdhd.1.vly3iwnrmszwphu5z3t9hh9bs (192.168.1.9): 56 data bytes
64 bytes from 192.168.1.9: seq=0 ttl=64 time=1.017 ms
64 bytes from 192.168.1.9: seq=1 ttl=64 time=0.771 ms
64 bytes from 192.168.1.9: seq=2 ttl=64 time=0.848 ms