云原生之Docker容器的网络管理

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 云原生之Docker容器的网络管理

一、登录华为ECS云服务器

image.png

二、检查docker环境

[root@ecs-1558 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-10-23 20:47:47 CST; 59s ago
     Docs: https://docs.docker.com
 Main PID: 1790 (dockerd)
    Tasks: 7
   Memory: 25.2M
   CGroup: /system.slice/docker.service
           └─1790 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.409717477+08:00" level=info msg="scheme \"unix\" not re...e=grpc
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.409728241+08:00" level=info msg="ccResolverWrapper: sen...e=grpc
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.409734336+08:00" level=info msg="ClientConn switching b...e=grpc
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.431194886+08:00" level=info msg="Loading containers: start."
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.539531805+08:00" level=info msg="Default bridge (docker...dress"
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.578799852+08:00" level=info msg="Loading containers: done."
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.593295777+08:00" level=info msg="Docker daemon" commit=....10.18
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.593380253+08:00" level=info msg="Daemon has completed i...ation"
Oct 23 20:47:47 ecs-1558 dockerd[1790]: time="2022-10-23T20:47:47.618260430+08:00" level=info msg="API listen on /var/run....sock"
Oct 23 20:47:47 ecs-1558 systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

三、容器的host网络

1.运行host网络类型的容器

[root@ecs-1558 ~]# docker run -itd --network=host centos
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
3773fe30f40d0687262128fb3096e7f9628f734d803ab4c5d7b58f67a2ab0298

2.在容器内查看网络信息

[root@ecs-1558 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
3773fe30f40d   centos    "/bin/bash"   8 minutes ago   Up 8 minutes             sweet_antonelli
[root@ecs-1558 ~]# docker exec -it 3773f /bin/bash
[root@ecs-1558 /]# ip add
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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:23:7a:13 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.152/24 brd 192.168.0.255 scope global dynamic noprefixroute eth0
       valid_lft 85095sec preferred_lft 85095sec
    inet6 fe80::f816:3eff:fe23:7a13/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:7b:ff:1e:be 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
[root@ecs-1558 /]# 

3.宿主机验证网络信息

[root@ecs-1558 ~]# ip add
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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:23:7a:13 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.152/24 brd 192.168.0.255 scope global noprefixroute dynamic eth0
       valid_lft 85054sec preferred_lft 85054sec
    inet6 fe80::f816:3eff:fe23:7a13/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:7b:ff:1e:be 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

4.host网络类型总结

当容器使用host网络模型,容器和宿主机共享网络namespace,因此网络信息完全一致。

四、容器的bridge网络

1.运行bridge网络类型的容器

[root@ecs-1558 ~]# docker run -itd --network=bridge centos
5fa70b91496e84ace5f2810c48ee2d02596ad404d53ac329d42120b05934ee23

2.查看容器内网络

[root@ecs-1558 ~]# docker exec -it 5fa7 /bin/bash
[root@5fa70b91496e /]# ip add
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
4: eth0@if5: <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

3.查看宿主机中默认bridge网络信息

[root@ecs-1558 ~]# docker network inspect bridge 
[
    {
        "Name": "bridge",
        "Id": "ab0ec44cdda6793472ab8d1017d5541c07de7644395233f327962326bb2df5c5",
        "Created": "2022-10-23T20:47:47.539554084+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "5fa70b91496e84ace5f2810c48ee2d02596ad404d53ac329d42120b05934ee23": {
                "Name": "pensive_saha",
                "EndpointID": "9c85f5aec61d08fa81c63eb7e01ff80e0bab6d7ef0a13c5a136cb4a409a2e5f8",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

五、自定义bridge类型的网络

1.自定义bridge类型的网络

[root@ecs-1558 ~]# docker network create --driver bridge --subnet 173.18.0.0/16 --gateway 173.18.0.1 servicebridge01
d245c744d34ea7d6c5ecc8aed885076db2d99f93cd2b505df21ecef3ba331f2a

2.查看自定义网络详细信息

[root@ecs-1558 ~]# docker network inspect d245c744d
[
    {
        "Name": "servicebridge01",
        "Id": "d245c744d34ea7d6c5ecc8aed885076db2d99f93cd2b505df21ecef3ba331f2a",
        "Created": "2022-10-23T21:08:22.767835724+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "173.18.0.0/16",
                    "Gateway": "173.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

3.使用自定义网络运行容器test01

[root@ecs-1558 ~]# docker run -itd --name test01--network=servicebridge01 centos
bc66fd869ffbcdf6478e60683616693d2449e3ac80c4821a979804082c6df9d3

六、测试brideg类型的容器互通情况

1.创建一个默认网络的容器test02

[root@ecs-1558 ~]# docker run -itd  --name test02 centos
97ed154879a674662d21c99b2ae6c787cacb2fc4696bd05432e1489c60a8ec56

2.进入test02容器查看IP地址

[root@ecs-1558 ~]# docker exec -it 97e /bin/bash
[root@97ed154879a6 /]# ip add
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
9: eth0@if10: <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

3.查看test01容器的IP

[root@ecs-1558 ~]# docker exec -it bc66 /bin/bash
[root@bc66fd869ffb /]# ip add
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
7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ad:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 173.18.0.2/16 brd 173.18.255.255 scope global eth0
       valid_lft forever preferred_lft forever

4.将自定义servicebridge01网络挂载到test02

[root@ecs-1558 ~]# docker network connect 97ed154879a6 bc66fd869ffb

5.查看test02容器的ip

[root@ecs-1558 ~]# docker exec -it 97e /bin/bash
[root@97ed154879a6 /]# ip add
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
9: eth0@if10: <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
11: eth1@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ad:12:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 173.18.0.3/16 brd 173.18.255.255 scope global eth1
       valid_lft forever preferred_lft forever
[root@97ed154879a6 /]# 

6.查看两个容器互通情况

[root@97ed154879a6 /]# ping 173.18.0.2 -c 8
PING 173.18.0.2 (173.18.0.2) 56(84) bytes of data.
64 bytes from 173.18.0.2: icmp_seq=1 ttl=64 time=0.064 ms
64 bytes from 173.18.0.2: icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from 173.18.0.2: icmp_seq=3 ttl=64 time=0.042 ms
64 bytes from 173.18.0.2: icmp_seq=4 ttl=64 time=0.044 ms
64 bytes from 173.18.0.2: icmp_seq=5 ttl=64 time=0.043 ms
64 bytes from 173.18.0.2: icmp_seq=6 ttl=64 time=0.056 ms
64 bytes from 173.18.0.2: icmp_seq=7 ttl=64 time=0.043 ms
64 bytes from 173.18.0.2: icmp_seq=8 ttl=64 time=0.045 ms

--- 173.18.0.2 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7000ms
rtt min/avg/max/mdev = 0.042/0.047/0.064/0.010 ms
[root@97ed154879a6 /]# 
相关实践学习
2分钟自动化部署人生模拟器
本场景将带你借助云效流水线Flow实现人生模拟器小游戏的自动化部署
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
相关文章
|
10天前
|
运维 Cloud Native 虚拟化
一文吃透云原生 Docker 容器,建议收藏!
本文深入解析云原生Docker容器技术,涵盖容器与Docker的概念、优势、架构设计及应用场景等,建议收藏。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
一文吃透云原生 Docker 容器,建议收藏!
|
5天前
|
Kubernetes Cloud Native Docker
云原生之旅:从传统架构到容器化服务的演变
随着技术的快速发展,云计算已经从简单的虚拟化服务演进到了更加灵活和高效的云原生时代。本文将带你了解云原生的概念、优势以及如何通过容器化技术实现应用的快速部署和扩展。我们将以一个简单的Python Web应用为例,展示如何利用Docker容器进行打包和部署,进而探索Kubernetes如何管理这些容器,确保服务的高可用性和弹性伸缩。
|
3天前
|
Kubernetes Linux 开发者
深入探索容器化技术——Docker 的实战应用
深入探索容器化技术——Docker 的实战应用
23 5
|
6天前
|
运维 Cloud Native 云计算
云原生之旅:Docker容器化实战
本文将带你走进云原生的世界,深入理解Docker技术如何改变应用部署与运维。我们将通过实际案例,展示如何利用Docker简化开发流程,提升应用的可移植性和伸缩性。文章不仅介绍基础概念,还提供操作指南和最佳实践,帮助你快速上手Docker,开启云原生的第一步。
|
4天前
|
关系型数据库 MySQL Java
【Docker最新版教程】一文带你快速入门Docker常见用法,实现容器编排和自动化部署上线项目
Docker快速入门到项目部署,MySQL部署+Nginx部署+docker自定义镜像+docker网络+DockerCompose项目实战一文搞定!
|
4天前
|
开发者 Docker Python
从零开始:使用Docker容器化你的Python Web应用
从零开始:使用Docker容器化你的Python Web应用
13 1
|
10天前
|
机器学习/深度学习 数据采集 Docker
Docker容器化实战:构建并部署一个简单的Web应用
Docker容器化实战:构建并部署一个简单的Web应用
|
8天前
|
运维 持续交付 虚拟化
深入解析Docker容器化技术的核心原理
深入解析Docker容器化技术的核心原理
28 1
|
10天前
|
JavaScript 开发者 Docker
Docker容器化实战:构建并部署一个简单的Web应用
Docker容器化实战:构建并部署一个简单的Web应用
|
10天前
|
持续交付 开发者 Docker
Docker容器化技术实战指南
Docker容器化技术实战指南
26 1
下一篇
无影云桌面