09 : docker 4種網絡類型

重啓docker服務,容器所有退出的解決方法(建議使用第一種)nginx

1: docker run --restart=always (起容器的時候,指定這個參數--restart=always)
2:添加:"live-restore": true (重啓或者stop docker,容器也會活着)
[root@k8s129 ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://aeckruos.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.6.129:5000"],
"live-restore": true
}
[root@k8s129 ~]# systemctl restart dockerdocker

 

---docker 網絡----------------------
docker 網絡類型
1:None: 不爲容器配置任何網絡功能。 --net=none (--network none)
[root@k8s129 ~]# docker run -it --net=none busybox:latest
/ # 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
/ #沒有IP。 json


2 : Container:與另外一個運行中高端容器共享Network Namespace (容器IP地址同樣)
#起一個容器
[root@k8s129 ~]# docker run -d nginx:latest
[root@k8s129 ~]# docker inspect e7255499b1a1 |grep -C 8 "bridge"
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "a263bfd0c6e2fc545cf21d56a5de88f2db8cdb4231a003cd127946b00d47e754",
"EndpointID": "190ff5511a09cb921a7fd9db50f4d69684a97833b27e3a79ed7b7cb3cde20616",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
# 再起一個容器,指定網絡類型是:--net=container (--network container)
[root@k8s129 ~]# docker run -it --net=container:e7255499b1a1 centos:latest
[root@e7255499b1a1 /]# 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
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@e7255499b1a1 /]#
#細心的你必定發現了,這個容器的IP地址竟然和剛纔nginx的容器IP地址同樣
#這個時候80端口是被nginx的容器佔用的,centos這個容器是不能再使用80端口了(若是剛纔咱們的centos鏡像也使用80端口了,
就會發現這個容器經過container網絡方式,容器是起不來的,會一直報錯端口已經被使用)
#這個就是container類型centos

3:   Host: 與主機共享Network Namespace。--net=host(--network host)
[root@k8s129 ~]# docker run -it --net=host centos:latest
[root@k8s129 /]# hostname #其實這個時候咱們已經在容器裏面了,只不過他和宿主機的名字同樣
k8s129
[root@k8s129 /]# ip a #看到的都是宿主機的
#這個時候公用的是宿主機的網絡,網速幾乎和宿主機如出一轍。容器佔用的端口,宿主機是不能再開啓的。網絡


4:   Bridge:docker設置的NAT網絡模型(docker run 容器起來的時候,默認就是橋接)
#容器起來後,會自動橋接到宿主機的莫一塊網卡上面(筆者這裏是docker0)
[root@k8s129 ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:88ff:fe64:d76f prefixlen 64 scopeid 0x20<link>
ether 02:42:88:64:d7:6f txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 656 (656.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0oop

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.129 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 fe80::fe3a:febc:334a:8d2c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:7d:76:a8 txqueuelen 1000 (Ethernet)spa

相關文章
相關標籤/搜索