Docker容器

docker 05day

本文主要是介绍docker 05day,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

05day

自定义网络

#查看docker网络

#网络模式
bridge: 桥接docker(默认)
none:   不配置网络
host:	和宿主机共享网络
container: 容器网络连通!(用的少 局限很大)
#测试
#我们直接启动的命令 --net bridge  这个是docker0
docker run -d -p --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat

#docker 0特点

root@fanwd-virtual-machine:/home/fanw# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
a90f2d21095c15974e69e1f87e2bb4afac8e6fc0e33bf1629fff37399c96ad0f

#查看自定义的网络 
#过程
#--driver bridge 
#--subnet 192.168.0.0/16
#--gateway 192.168.0.1 mynet
root@fanwd-virtual-machine:/home/fanw# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
743f4d60fb2a   bridge    bridge    local
45e0f08bad2d   host      host      local
a90f2d21095c   mynet     bridge    local


#创建一个tomcat 调用 自定义网路
root@fanwd-virtual-machine:/home/fanw# docker run -d -P --name tomcat-net-01 --net mynet tomcat
09c4ec8051ff8d58eca0fb8ca4a0fede1cfb11ea59f53c7f0d569c565804ec3c

#查看
root@fanwd-virtual-machine:/home/fanw# docker inspect mynet
oot@fanwd-virtual-machine:/home/fanw# docker network inspect mynet
[
    {
        "Name": "mynet",
        "Id": "a90f2d21095c15974e69e1f87e2bb4afac8e6fc0e33bf1629fff37399c96ad0f",
        "Created": "2021-03-30T15:13:18.978117034+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
#开启两个容器进行测试
"09c4ec8051ff8d58eca0fb8ca4a0fede1cfb11ea59f53c7f0d569c565804ec3c": {
                "Name": "tomcat-net-01",
                "EndpointID": "5de4836259be19a1e2abd368238fbe1ae9e053f5dd71b7a2162648cfc631b9fa",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "ff3869de8750e112817ec904a4745f66330609cf1367d44072026bedc8cf8ac2": {
                "Name": "tomcat-net-02",
                "EndpointID": "db9f37c94464f539a0991a6709ff810be37a9195650a34ddbf0dbcb57fd1b0ec",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
                
                
root@fanwd-virtual-machine:/home/fanw# docker exec -it tomcat-net-01 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.047 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.047 ms
^C
--- 192.168.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 40ms
rtt min/avg/max/mdev = 0.047/0.057/0.078/0.015 ms



#好处
redis 不同的集群使用不同的网络,保证集群是安全和健康的
mysql 不同的集群使用不同的网络,保证集群是安全和健康的


网络连通

#docker network connect mynet tomcat03 连接


root@fanwd-virtual-machine:/home/fanw# docker run -d -P --name tomcat03 tomcat
d57136a8ca18efa3395a3f84eaf8f464cebb97aa34db5f0989aa65e89fe5cfe5
root@fanwd-virtual-machine:/home/fanw# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                     NAMES
d57136a8ca18   tomcat    "catalina.sh run"   8 seconds ago    Up 6 seconds    0.0.0.0:49155->8080/tcp   tomcat03
ff3869de8750   tomcat    "catalina.sh run"   9 minutes ago    Up 9 minutes    0.0.0.0:49154->8080/tcp   tomcat-net-02
09c4ec8051ff   tomcat    "catalina.sh run"   20 minutes ago   Up 20 minutes   0.0.0.0:49153->8080/tcp   tomcat-net-01
root@fanwd-virtual-machine:/home/fanw# docker exec -it tomcat03 ping tomcat-net-01
ping: tomcat-net-01: Name or service not known

#测试
"09c4ec8051ff8d58eca0fb8ca4a0fede1cfb11ea59f53c7f0d569c565804ec3c": {
                "Name": "tomcat-net-01",
                "EndpointID": "5de4836259be19a1e2abd368238fbe1ae9e053f5dd71b7a2162648cfc631b9fa",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "d57136a8ca18efa3395a3f84eaf8f464cebb97aa34db5f0989aa65e89fe5cfe5": {
                "Name": "tomcat03",
                "EndpointID": "c119b954c90a92313690b51427a53f2ad52cd92e217b24566e7eae88631b012b",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            },
            "ff3869de8750e112817ec904a4745f66330609cf1367d44072026bedc8cf8ac2": {
                "Name": "tomcat-net-02",
                "EndpointID": "db9f37c94464f539a0991a6709ff810be37a9195650a34ddbf0dbcb57fd1b0ec",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""

#成功ping通!
root@fanwd-virtual-machine:/home/fanw# docker exec -it tomcat03 ping tomcat-net-01
PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.054 ms
^C
--- tomcat-net-01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 58ms
rtt min/avg/max/mdev = 0.045/0.058/0.076/0.014 ms

跨网络操作别人 需要connet连通


实战:Redis 集群部署

#先创建一个Redis网络
root@fanwd-virtual-machine:/home/fanw# docker network create redis --subnet 172.38.0.0/16
fa0fe776f64c3e5ff66c37479dbba1def395f9982f1e2e5d4044aed76212386a


 "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.38.0.0/16"
                }
            ]
        },

#创建6个Redis
#用shell脚本
root@fanwd-virtual-machine:/home/fanw# for port in $(seq 1 6); \
> do \
> mkdir -p /mydata/redis/node-${port}/conf
> touch /mydata/redis/node-${port}/conf/redis.conf
> cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
> port 6379
> bind 0.0.0.0
> cluster-enabled yes
> cluster-config-file nodes.conf
> cluster-announce-ip 172.38.0.1${port}
> cluster-announce-port 6379
> cluster-announce-bus-port 16379
> appendonly yes
> EOF
> done

#启动Redis   命令
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
> -v /mydata/redis/node-2/data:/data \
> -v/mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
> -d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf



开启6个Redis

#建立集群

#中间一个ip打错了 redis-5 没有搞上

这篇关于docker 05day的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!