zoukankan      html  css  js  c++  java
  • docker容器网络配置

    Linux内核实现名称空间的创建

    ip netns命令

    可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。

    注意:ip netns命令修改网络配置时需要 sudo 权限。

    可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:

    [root@yc ~]# ip netns help
    Usage: ip netns list //查看
           ip netns add NAME //开辟空间
           ip netns set NAME NETNSID //设置名称空间的sid
           ip [-all] netns delete [NAME] //删除空间
           ip netns identify [PID] 
           ip netns pids NAME //查看空间内任务的pid
           ip [-all] netns exec [NAME] cmd ... //进入空间执行命令
           ip netns monitor //监控
           ip netns list-id //查看id
    

    创建Network Namespace

    通过命令创建一个名为yc的命名空间

    [root@yc ~]# ip netns list
    [root@yc ~]# ip netns add yc
    [root@yc ~]# ip netns list
    yc
    

    新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,该命令会报错(Cannot create namespace file "/var/run/netns/yc": File exists)

    [root@yc ~]# ls /var/run/netns/
    yc
    [root@yc ~]# ip netns add yc
    Cannot create namespace file "/var/run/netns/yc": File exists
    

    注:

    • 直接在netns文件夹内创建文件,无法使用netns list命令识别
    • 对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。

    操作Network Namespace

    ip命令提供了ip netns exec子命令可以在对应的 Network Namespace 中执行命令

    //查看新创建 Network Namespace 的网卡信息
    [root@yc ~]# ip netns exec yc ip a
    1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    
    //通过下面的命令启用lo回环网卡,不启用ping不通
    [root@yc ~]# ip netns exec yc ip link set lo up
    [root@yc ~]# ip netns exec yc ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    

    转移设备

    我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。

    其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的

    veth pair

    veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。
    引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来

    创建veth pair

    [root@yc ~]# ip link add type veth
    [root@yc ~]# ip link show
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
        link/ether 00:0c:29:aa:6d:8d brd ff:ff:ff:ff:ff:ff
    3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
        link/ether 02:42:97:8e:2c:9d brd ff:ff:ff:ff:ff:ff
    4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
        link/ether 92:10:b2:88:9e:f7 brd ff:ff:ff:ff:ff:ff
    5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
        link/ether 5e:dd:68:07:ce:5f brd ff:ff:ff:ff:ff:ff
    
    //可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于“未启用”状态
    

    实现Network Namespace间通信

    下面利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为yc的 Network Namespace,下面再创建一个信息Network Namespace,命名为yc1

    [root@yc ~]# ip netns add yc1
    [root@yc ~]# ip netns list
    yc1
    yc
    

    将veth0加入到yc,将veth1加入到yc1

    [root@yc ~]# ip link set veth0 netns yc
    [root@yc ~]# ip link set veth1 netns yc1
    

    分别为这对veth pair配置上ip地址,并启用它们

    [root@yc ~]# ip netns exec yc ip link set lo up
    [root@yc ~]# ip netns exec yc ip link set veth0 up
    [root@yc ~]# ip netns exec yc ip addr add 1.1.1.1/24 dev veth0
    [root@yc ~]# ip netns exec yc1 ip link set veth1 up
    [root@yc ~]# ip netns exec yc1 ip addr add 1.1.1.2/24 dev veth1
    

    查看这对veth pair的状态

    [root@yc ~]# ip netns exec yc ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    4: veth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
        link/ether 92:10:b2:88:9e:f7 brd ff:ff:ff:ff:ff:ff link-netns yc1
        inet 1.1.1.1/24 scope global veth0
           valid_lft forever preferred_lft forever
        inet6 fe80::9010:b2ff:fe88:9ef7/64 scope link 
           valid_lft forever preferred_lft forever
    
    
    [root@yc ~]# ip netns exec yc1 ip a
    1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    5: veth1@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
        link/ether 5e:dd:68:07:ce:5f brd ff:ff:ff:ff:ff:ff link-netns yc
        inet 1.1.1.2/24 scope global veth1
           valid_lft forever preferred_lft forever
        inet6 fe80::5cdd:68ff:fe07:ce5f/64 scope link 
           valid_lft forever preferred_lft forever
    

    我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。尝试在yc中访问yc1中的ip地址:

    [root@yc ~]# ip netns exec yc ping 1.1.1.2
    PING 1.1.1.2 (1.1.1.2) 56(84) bytes of data.
    64 bytes from 1.1.1.2: icmp_seq=1 ttl=64 time=0.065 ms
    64 bytes from 1.1.1.2: icmp_seq=2 ttl=64 time=0.023 ms
    64 bytes from 1.1.1.2: icmp_seq=3 ttl=64 time=0.024 ms
    64 bytes from 1.1.1.2: icmp_seq=4 ttl=64 time=0.024 ms
    

    veth设备重命名

    重命名需要先停用veth设备

    [root@yc ~]# ip netns exec yc ip link set veth0 down
    [root@yc ~]# ip netns exec yc ip link set dev veth0 name ve0
    [root@yc ~]# ip netns exec yc ip link set ve0 up
    [root@yc ~]# ip netns exec yc ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    4: ve0@if5: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
        link/ether 92:10:b2:88:9e:f7 brd ff:ff:ff:ff:ff:ff link-netns yc1
        inet 1.1.1.1/24 scope global ve0
           valid_lft forever preferred_lft forever
    

    四种网络模式配置

    bridge模式配置

    容器在创建时默认为bridge模式,即不需要添加--network选项

    [root@yc ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    busybox      latest    b97242f89c8a   6 weeks ago   1.23MB
    httpd        latest    683a7aad17d3   7 weeks ago   138MB
    
    [root@yc ~]# docker run -it --rm busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    / # exit
    
    [root@yc ~]# docker run -it --rm --network bridge busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    
    可以看出两种操作方式的结果相同
    

    none模式配置

    [root@yc ~]# docker run -it --rm --network none busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    
    //手动配置和none模式自动配置效果相同
    [root@yc ~]# ip netns add b1
    [root@yc ~]# ip netns exec b1 ip link set lo up
    [root@yc ~]# ip netns exec b1 ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    

    container模式配置

    //启动第一个容器
    [root@yc ~]# docker run -it --rm --name yc2 busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    
    //启动第二个容器
    [root@yc ~]# docker run -it --rm --name yc3 busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
    / # exit
    

    yc2容器的ip地址为172.17.0.2,yc3容器的ip地址为172.17.0.3,由此可见没有共享网络,此时如果我们将yc3容器的启动方式改变一下,就可以使b2容器的ip与b1容器ip一致,也即共享ip,但不共享文件系统。

    [root@yc ~]# docker run -it --rm --name b2 --network container:yc2 busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
           valid_lft forever preferred_lft forever
    
    //在yc2上创建index.html文件
    / # echo 'yanchuang2' > /etc/index.html
    / # cat etc/index.html 
    yanchuang
    
    //在yc3上创建index.html文件
    / # echo 'yanchuang3' > /etc/index.html
    / # cat /etc/index.html 
    yanchuang3
    
    //在yc3容器上部署一个站点
    / # netstat -antl
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       
    / # httpd -h /etc/
    / # netstat -antl
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       
    tcp        0      0 :::80                   :::*                    LISTEN   
    
    //在yc2容器上用本地地址去访问此站点
    / # wget -O - 127.0.0.1
    Connecting to 127.0.0.1 (127.0.0.1:80)
    writing to stdout
    yc3
    -                    100% |***************|     3  0:00:00 ETA
    written to stdout
    由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
    

    host模式配置

    启动容器时直接指明模式为host

    [root@yc ~]# docker run -it --rm --network host busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel qlen 1000
        link/ether 00:0c:29:a5:9f:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.23.138/24 brd 192.168.23.255 scope global dynamic ens32
           valid_lft 1378sec preferred_lft 1378sec
        inet6 fe80::3e15:c05:b9ea:ecda/64 scope link 
           valid_lft forever preferred_lft forever
    3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue 
        link/ether 02:42:a1:f4:46:42 brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:a1ff:fef4:4642/64 scope link 
           valid_lft forever preferred_lft forever
    
    //在这个容器中启动一个http站点,就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
    / # echo 'yanchuang' > /etc/index.html
    / # httpd -h /etc/
    / # netstat -antl
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
    tcp        0      0 192.168.23.138:22      192.168.23.1:52451     ESTABLISHED 
    tcp        0      0 192.168.23.138:22      192.168.23.1:52178     ESTABLISHED 
    tcp        0      0 :::80                   :::*                    LISTEN      
    tcp        0      0 :::22                   :::*                    LISTEN  
    
    [root@cst ~]# curl 192.168.23.138
    yanchuang
    
    //此时再创建一个容器部署http站点端口会冲突
    

    容器的常用操作

    //主机名默认为容器的ID,且无法修改主机名
    [root@localhost ~]# docker run -it --name t1 --network bridge --rm busybox
    / # hostname
    7769d784c6da
    

    在容器启动时注入主机名

    [root@localhost ~]# docker run -it --name t1 --network bridge --hostname wangqing --rm busybox
    / # hostname
    wangqing
    / # cat /etc/hosts
    127.0.0.1       localhost
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    10.0.0.2        wangqing    # 注入主机名时会自动创建主机名到IP的映射关系
    / # cat /etc/resolv.conf 
    # Generated by NetworkManager
    search localdomain
    nameserver 192.168.10.2     # DNS也会自动配置为宿主机的DNS
    / # ping www.baidu.com
    PING www.baidu.com (182.61.200.7): 56 data bytes
    64 bytes from 182.61.200.7: seq=0 ttl=127 time=26.073 ms
    64 bytes from 182.61.200.7: seq=1 ttl=127 time=26.378 ms
    

    手动指定容器要使用的DNS

    [root@localhost ~]# docker run -it --name t1 --network bridge --hostname wangqing --dns 114.114.114.114 --rm busybox
    / # cat /etc/resolv.conf 
    search localdomain
    nameserver 114.114.114.114
    / # nslookup -type=a www.baidu.com
    Server:         114.114.114.114
    Address:        114.114.114.114:53
    
    Non-authoritative answer:
    www.baidu.com   canonical name = www.a.shifen.com
    Name:   www.a.shifen.com
    Address: 182.61.200.6
    Name:   www.a.shifen.com
    Address: 182.61.200.7
    

    手动往/etc/hosts文件中注入主机名到IP地址的映射

    [root@localhost ~]# docker run -it --name t1 --network bridge --hostname wangqing --add-host www.a.com:1.1.1.1 --rm busybox      
    / # cat /etc/hosts
    127.0.0.1       localhost
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    1.1.1.1 www.a.com
    10.0.0.2        yanchuang
    

    开放容器端口

    执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。

    -p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。

    -p选项的使用格式:

    • -p
      • 将指定的容器端口映射至主机所有地址的一个动态端口
    • -p :
      • 将容器端口映射至指定的主机端口
    • -p ::
      • 将指定的容器端口映射至主机指定的动态端口
    • -p ::
      • 将指定的容器端口映射至主机指定的端口
    //-p (containerPort)
    将指定的容器端口映射至主机所有地址的一个动态端口
    [root@cst ~]# docker run -d -p 80 httpd
    20915b4254b61947c9a1b35ac9b6c0af031d90ddc8b1de181e6233627cc1e5de
    [root@cst ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                      PORTS                   NAMES
    20915b4254b6   httpd          "httpd-foreground"   10 seconds ago   Up 9 seconds                0.0.0.0:49153->80/tcp   affectionate_wiles
    9c9915feca33   7b305e823449   "sh"                 46 hours ago     Exited (0) 46 hours ago                             amazing_wiles
    e1113e6ef12c   busybox        "sh"                 47 hours ago     Exited (127) 46 hours ago                           test
    [root@cst ~]#  curl 172.17.0.2
    <html><body><h1>It works!</h1></body></html>
    [root@cst ~]# curl 192.168.23.138:49153
    <html><body><h1>It works!</h1></body></html>
    
    //-p (hostPort):(containerPort)
    
    将容器端口(containerPort)映射至指定的主机端口(hostPort)
    [root@yc ~]# docker run -d -p 789:80 httpd
    fa28a31eeabb9f6ea937bafd62824ac6ce3b17a5ce914295f34886e06b245206
    [root@yc ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                      PORTS                 NAMES
    fa28a31eeabb   httpd          "httpd-foreground"   21 seconds ago   Up 19 seconds               0.0.0.0:789->80/tcp   condescending_bhabha
    [root@yc ~]# ss -antl
    State  Recv-Q  Send-Q   Local Address:Port   Peer Address:Port 
    LISTEN 0       128            0.0.0.0:789         0.0.0.0:*    
    LISTEN 0       128            0.0.0.0:22          0.0.0.0:*    
    LISTEN 0       128               [::]:22             [::]:*  
    
    [root@yc ~]# curl 172.17.0.3
    <html><body><h1>It works!</h1></body></html>
    [root@yc ~]# curl 192.168.23.138:789
    <html><body><h1>It works!</h1></body></html>
    
    //-p (ip)::(containerPort)
    
    将指定的容器端口(containerPort)映射至主机指定(ip)的动态端口
    [root@yc ~]# docker run -d -p 192.168.23.138::80 httpd
    fb8824f1930806685ba2c3c50f8cafbaa8c5b44c89cf998b46d6c9034619f8de
    [root@yc ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                      PORTS                           NAMES
    fb8824f19308   httpd          "httpd-foreground"   6 seconds ago    Up 5 seconds                192.168.23.138:49153->80/tcp   cool_goldberg
    [root@yc ~]# ss -antl
    State  Recv-Q Send-Q   Local Address:Port    Peer Address:Port 
    LISTEN 0      128    192.168.23.138:49153         0.0.0.0:*    
    LISTEN 0      128            0.0.0.0:789          0.0.0.0:*    
    LISTEN 0      128            0.0.0.0:22           0.0.0.0:*    
    LISTEN 0      128               [::]:22              [::]:* 
    
    [root@yc ~]# curl 172.17.0.4
    <html><body><h1>It works!</h1></body></html>
    [root@cst ~]# curl 192.168.23.138:49153
    <html><body><h1>It works!</h1></body></html>
    
    //-p (ip):(hostPort):(containerPort)
    
    将指定的容器端口(containerPort)映射至主机指定(ip)的端口(hostPort)
    
    [root@yc ~]# docker run -d -p 192.168.23.138:80:80 httpd
    74d965cbf519fa038a6ea6ba0516d0f85d130a1dd476adcda842f96bb1c34bc8
    [root@yc ~]# docker ps -a
    CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                      PORTS                           NAMES
    74d965cbf519   httpd          "httpd-foreground"   19 seconds ago   Up 18 seconds               192.168.23.138:80->80/tcp      magical_yalow
    [root@cst ~]# ss -antl
    State  Recv-Q Send-Q   Local Address:Port    Peer Address:Port 
    LISTEN 0      128    192.168.23.138:49153         0.0.0.0:*    
    LISTEN 0      128    192.168.23.138:80            0.0.0.0:*    
    LISTEN 0      128            0.0.0.0:789          0.0.0.0:*    
    LISTEN 0      128            0.0.0.0:22           0.0.0.0:*    
    LISTEN 0      128               [::]:22              [::]:*  
    
    [root@cst ~]# curl 172.17.0.5
    <html><body><h1>It works!</h1></body></html>
    [root@cst ~]# curl 192.168.23.138:80
    <html><body><h1>It works!</h1></body></html>
    

    动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。

    root@cst ~]# docker port 74d965cbf519
    80/tcp -> 192.168.23.138:80
    

    查看iptables防火墙规则详细信息

    自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件

    [root@cst ~]# vim /etc/docker/daemon.json
    {
            "bip": "192.168.10.1/24"
    
    }
    
    [root@cst ~]# systemctl daemon-reload
    [root@cst ~]# systemctl restart docker
    [root@cst ~]# ip a
    
    3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
        link/ether 02:42:b5:1a:f3:89 brd ff:ff:ff:ff:ff:ff
        inet 192.168.10.1/24 brd 192.168.10.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:b5ff:fe1a:f389/64 scope link 
           valid_lft forever preferred_lft forever
    
    //网桥修改后,创建出来的容器虚拟网卡IP地址规律也会改变
    [root@cst ~]# docker run -it --rm busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:0a:02 brd ff:ff:ff:ff:ff:ff
        inet 192.168.10.2/24 brd 192.168.10.255 scope global eth0
           valid_lft forever preferred_lft forever
    / # route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.10.1    0.0.0.0         UG    0      0        0 eth0
    192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    

    核心选项为bip,即bridge ip之意,用于指定docker0网桥自身的IP地址;其它选项可通过此地址计算得出

    docker远程连接

    dockerd守护进程的C/S,其默认仅监听Unix Socket格式的地址(/var/run/docker.sock),如果要使用TCP套接字,则需要修改/etc/docker/daemon.json配置文件,添加如下内容,然后重启docker服务

    "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
    

    在客户端上向dockerd直接传递“-H|--host”选项指定要控制哪台主机上的docker容器

    docker -H 192.168.10.145:2375 ps
    

    docker创建自定义桥

    创建一个额外的自定义桥,区别于docker0

    [root@yc ~]# docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    12969743e83b   bridge    bridge    local
    db813872caeb   host      host      local
    5ff13bc2927a   none      null      local
    
    [root@yc ~]# docker network create -d bridge  --subnet "192.168.1.0/24" --gateway "192.168.1.1" br0
    610f15f8ba634870e1ff4e8de78cb368787060d93b27a5dfc426c8874f4b7d21
    
    [root@yqh ~]# docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    610f15f8ba63   br0       bridge    local
    12969743e83b   bridge    bridge    local
    db813872caeb   host      host      local
    5ff13bc2927a   none      null      local
    
    [root@yc ~]# ip a
    3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
        link/ether 02:42:70:fc:91:bb brd ff:ff:ff:ff:ff:ff
        inet 192.168.10.1/24 brd 192.168.10.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:70ff:fefc:91bb/64 scope link 
           valid_lft forever preferred_lft forever
    13: br-610f15f8ba63: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
        link/ether 02:42:86:b0:ef:52 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.1/24 brd 192.168.1.255 scope global br-610f15f8ba63
           valid_lft forever preferred_lft forever
           
    

    使用新创建的自定义桥来创建容器c2

    [root@yc ~]# docker run -it --name c2 --network br0 busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
           valid_lft forever preferred_lft forever
    

    再创建一个容器c1,使用默认的bridge桥

    [root@yc ~]# docker run -it --name c1 --network bridge busybox
    / # ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:0a:02 brd ff:ff:ff:ff:ff:ff
        inet 192.168.10.2/24 brd 192.168.10.255 scope global eth0
           valid_lft forever preferred_lft forever
    
    //给容器加网络
    [root@yc ~]# docker network connect br0 c1
    
    #容器c1:
    / # ip a
    16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:0a:02 brd ff:ff:ff:ff:ff:ff
        inet 192.168.10.2/24 brd 192.168.10.255 scope global eth0
           valid_lft forever preferred_lft forever
    18: eth1@if19: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:01:03 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.3/24 brd 192.168.1.255 scope global eth1
           valid_lft forever preferred_lft forever
    
    //ping c2测试
    / # ping 192.168.1.2
    PING 192.168.1.2 (192.168.1.2): 56 data bytes
    64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.738 ms
    64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.079 ms
    64 bytes from 192.168.1.2: seq=2 ttl=64 time=0.074 ms
    ^C
    --- 192.168.1.2 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.074/0.297/0.738 ms
    
    //给c2加bridge
    [root@yc ~]# docker network connect bridge c2
    
    #容器c2:
    / # ip a
    14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
           valid_lft forever preferred_lft forever
    20: eth1@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
        link/ether 02:42:c0:a8:0a:03 brd ff:ff:ff:ff:ff:ff
        inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
           valid_lft forever preferred_lft forever
           
    //ping c1测试
    / # ping 192.168.10.2
    PING 192.168.10.2 (192.168.10.2): 56 data bytes
    64 bytes from 192.168.10.2: seq=0 ttl=64 time=0.087 ms
    64 bytes from 192.168.10.2: seq=1 ttl=64 time=0.071 ms
    64 bytes from 192.168.10.2: seq=2 ttl=64 time=0.076 ms
    ^C
    --- 192.168.10.2 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.071/0.078/0.087 ms
    
  • 相关阅读:
    js上拉加载下拉刷新
    CSRF
    Linux 常用命令
    汇编语言:了解寄存器与内存模型
    Node 的fs模块
    pdf转为html查看pdf.js
    centOs升级
    H5新特性监听手机的返回键
    gsap
    使用 iframe + postMessage 实现跨域通信
  • 原文地址:https://www.cnblogs.com/Ycqifei/p/14477594.html
Copyright © 2011-2022 走看看