bridge網路實現多個單主機進行通訊

墨陌呦發表於2020-11-02

在這裡插入圖片描述
2、

#建立橋接網路卡
[root@Docker1 ~]# docker network create --driver bridge --subnet 172.100.100.0/24 --gateway 172.100.100.254 my_net
87b5edc787de5b2bb3a6b965ebe24cbf74508dda0b717e956c797bfee12c100d
#建立bbox2、bbox3容器
[root@Docker1 ~]# docker run -itd --name bbox2 --network my_net busybox
a5a44577d384686cf54e79f6fd859f24eaf95703c57e9f14542bf0b8ecd60ebc
[root@Docker1 ~]# docker run -itd --name bbox3 --network my_net busybox
08ba77e89f517b1df468080b16a4ae13b052768c27bb9fceb3937f615135446d
#驗證兩個是否能通訊
#使用bbox2容器檢視是否能與bbox3通訊
[root@Docker1 ~]# docker exec -it bbox2 sh
/ # 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
7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:64:64:01 brd ff:ff:ff:ff:ff:ff
    inet 172.100.100.1/24 brd 172.100.100.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # ping 172.100.100.2
PING 172.100.100.2 (172.100.100.2): 56 data bytes
64 bytes from 172.100.100.2: seq=0 ttl=64 time=0.120 ms
64 bytes from 172.100.100.2: seq=1 ttl=64 time=0.062 ms
64 bytes from 172.100.100.2: seq=2 ttl=64 time=0.081 ms
--- 172.100.100.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.062/0.087/0.120 ms
/ # exit
#使用bbox3容器檢視是否能與bbox2通訊
[root@Docker1 ~]# docker exec -it bbox3 sh
/ # 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
9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:64:64:02 brd ff:ff:ff:ff:ff:ff
    inet 172.100.100.2/24 brd 172.100.100.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # ping 172.100.100.1
PING 172.100.100.1 (172.100.100.1): 56 data bytes
64 bytes from 172.100.100.1: seq=0 ttl=64 time=0.056 ms
64 bytes from 172.100.100.1: seq=1 ttl=64 time=0.062 ms
64 bytes from 172.100.100.1: seq=2 ttl=64 time=0.063 ms
--- 172.100.100.1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.056/0.060/0.063 ms

3、

#建立nginx映象
[root@Docker1 ~]# vim Dockerfile
FROM centos:7
ADD nginx-1.11.5.tar.gz /usr/src/
RUN yum -y install net-tools zlib zlib-devel openssl-devel openssl pcre gcc* vim /sbin/ip
RUN useradd nginx
WORKDIR /usr/src/nginx-1.11.5/
RUN ./configure && gmake && gmake install
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
RUN echo "#/bin/bash" >/nginx.sh
RUN echo "nginx" >> ./nginx.sh
RUN sh ./nginx.sh
EXPOSE 80
[root@Docker1 ~]# docker build -t nginx .
#將nginx新增到連個橋接網路卡中
[root@Docker1 ~]# docker network create --driver bridge myapp_net
8da4e20877abdd6a2c68ecd5ab5bfa834721622243f1e99057b9b437a54aecdb
[root@Docker1 ~]# docker run -itd --name nginx nginx
364c4e3b5aab7d3f6a80381930a62408b5932699a7a0c49f6a31fcc3979d43f6
[root@Docker1 ~]# docker network connect my_net nginx 
[root@Docker1 ~]# docker network connect myapp_net nginx 
#建立bbox1容器
[root@Docker1 ~]# docker run -itd --name bbox1 --network container:nginx busybox
d59bf9efd25552446727b3faf0e3a09941a953b3b0afd996c4f745beb80003e1
#檢視nginx容器中的網路卡是否與bbox1中的網路卡是否相同
[root@Docker1 ~]# docker exec -it nginx sh
sh-4.2# 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
28: eth0@if29: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
30: eth1@if31: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:64:64:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.100.100.3/24 brd 172.100.100.255 scope global eth1
       valid_lft forever preferred_lft forever
32: eth2@if33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth2
       valid_lft forever preferred_lft forever
sh-4.2# exit
exit
[root@Docker1 ~]# docker exec -it bbox1 sh
/ # 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
28: eth0@if29: <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
30: eth1@if31: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:64:64:03 brd ff:ff:ff:ff:ff:ff
    inet 172.100.100.3/24 brd 172.100.100.255 scope global eth1
       valid_lft forever preferred_lft forever
32: eth2@if33: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth2
       valid_lft forever preferred_lft forever

4、

#建立php映象
[root@Docker1 ~]# vim Dockerfile
FROM centos:7
ADD php-5.5.38.tar.gz /usr/src
WORKDIR /usr/src/php-5.5.38
RUN yum -y install gd libxml2-devel libjpeg-devel libpng-devel gcc make vim /sbin/ip
RUN ./configure --prefix=/usr/local/php --with-gd --with-zlib \
--with-config-file-path=/usr/loca/php \
--enable-fpm --enable-mbstring --with-jpeg-dir=/usr/lib && make && make install
WORKDIR /
RUN cp /usr/src/php-5.5.38/php.ini-development /usr/local/php/php.ini
RUN sed -i '/default_charset/c \default_charset = "utf-8"' /usr/local/php/php.ini
RUN sed -i '/short_open_tag/c \short_open_tag = On' /usr/local/php/php.ini
RUN cp /usr/src/php-5.5.38/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
RUN chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm
RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
RUN sed -i '/;pid = run/c \pid = run/php-fpm.pid' /usr/local/php/etc/php-fpm.conf && \
sed -i '/user =/c \user = nginx' /usr/local/php/etc/php-fpm.conf && \
sed -i '/group =/c \group = nginx' /usr/local/php/etc/php-fpm.conf && \
sed -i '/pm.max_children/c \pm.max_children = 50' /usr/local/php/etc/php-fpm.conf && \
sed -i '/pm.start_servers/c \pm.start_servers = 20' /usr/local/php/etc/php-fpm.conf && \
sed -i '/pm.min_spare_servers/c \pm.min_spare_servers = 5' /usr/local/php/etc/php-fpm.conf && \
sed -i '/pm.max_spare_servers/c \pm.max_spare_servers = 35' /usr/local/php/etc/php-fpm.conf
RUN useradd nginx
RUN mkdir /www
RUN echo '<?' >/www/index.php
RUN echo 'phpinfo();' >>/www/index.php
RUN echo '?>' >>/www/index.php
[root@Docker1 ~]# docker build -t php .
#整合nginx和php
#php容器
[root@Docker1 ~]# docker run -itd --name php --network myapp_net php
e748a4c409fb4328bdea47b3bdc72314f2d7947179559b1318d074b1a7589b66
[root@Docker1 ~]# docker exec -it php /bin/sh
sh-4.2# vim /usr/local/php/etc/php-fpm.conf
164 listen = 172.18.0.2:9000	#本機IP
#nginx容器
sh-4.2# vim /usr/local/nginx/conf/nginx.conf
 45             index  index.php index.html index.htm;
 65         location ~ \.php$ {
 66             root           /www/;	#index.php存在的目錄(在php端)
 67             fastcgi_pass   172.18.0.2:9000;	#php的IP
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }
sh-4.2# nginx -s reload
#驗證
sh-4.2# elinks 172.18.0.3/index.php	#本機IP

相關文章