|
| 1 | +Networking in Docker: |
| 2 | +===================== |
| 3 | + Containers present unique challenges when it comes to networking. Docker includes multiple built-in solutions to these networking challenges. |
| 4 | + Docker implements container networking using a framework called the Container Networking Model (CNM) and manages the networking for containers. |
| 5 | + |
| 6 | +The CNM utilizes the following concepts: |
| 7 | + |
| 8 | + Sandbox: An isolated unit containing all networking components associated with a single container. Usually a Linux network namespace. |
| 9 | + |
| 10 | + Endpoint: Connects a sandbox to a network. Each sandbox/container can have any number of endpoints, but has exactly one endpoint for each network it is connected to. |
| 11 | + |
| 12 | + Network: A collection of endpoints connected to one another. |
| 13 | + |
| 14 | + Network Driver: Handles the actual implementation of the CNM concepts. |
| 15 | + |
| 16 | + IPAM Driver: IPAM means IP Address management. Automatically allocates subnets and IP Addresses for networks and endpoints. |
| 17 | + |
| 18 | +Network Drivers: |
| 19 | +================ |
| 20 | + Docker includes several built-in network drivers, know as Native Network Drivers. |
| 21 | + These network drivers implement the concepts described in the CNM. |
| 22 | + |
| 23 | + The Native Network Drivers are: |
| 24 | + 1) host |
| 25 | + 2) bridge |
| 26 | + 3) overlay |
| 27 | + 4) macvlan |
| 28 | + 5) none |
| 29 | + |
| 30 | + with docker run we can use --net flag to attach network driver to container(s). |
| 31 | + |
| 32 | + The Host Network Driver: |
| 33 | + ------------------------ |
| 34 | + The Host Network Driver allows containers to use the host's network stack direclty. |
| 35 | + 1) Containers use the host's networking resources direclty |
| 36 | + 2) No sandboxes, all containers on the host using the hsot driver share the same network namespace |
| 37 | + 3) no two containers can use the same port(s) |
| 38 | + UseCases: Simple and easy setup, one or only few containers on a single host. |
| 39 | + |
| 40 | + The Bridge Network Driver: |
| 41 | + -------------------------- |
| 42 | + The Bridge Network Driver uses Linux bridge networks to provice connectivity between containers on the same host. |
| 43 | + 1) This is the default driver for containers running on a single host (i.e, not in a swarm) |
| 44 | + 2) Creates a Linux Bridge for each Docker Network |
| 45 | + 3) Creates a default Linux bridge network called docker0. Containers automatically connect to this if no other network is specified |
| 46 | + UseCases: isolated networking among containers ona single host. |
| 47 | + |
| 48 | + The Overlay Network Driver: |
| 49 | + --------------------------- |
| 50 | + The Overlay Network Driver provides connectivity between containers across multiple Docker hosts, i.e. with Docker swarm. |
| 51 | + 1) Uses a VXLAN data plane, which allows the underlying network infrastructure (underlay) to route data between hosts in a way that is transparent to the containers themselves. |
| 52 | + 2) Automatically configures network interfaces, bridges, etx. on each hosts as needed. |
| 53 | + UseCases: Networking between containers in a swarm |
| 54 | + |
| 55 | + The macvlan Network Driver: |
| 56 | + --------------------------- |
| 57 | + The macvlan Network Driver offers a more lightweight implementation by connecting container interfaces directly to host interfaces. |
| 58 | + 1) Uses direct association with Linux interfaces instead of a bridge interface. |
| 59 | + 2) Harder to configure and greater dependency between macvlan and the external network. |
| 60 | + 3) More lightweight and less latency. |
| 61 | + UseCases: When there is a need for extremely low latency, or a need for containers with IP addresses in teh external subnet. |
| 62 | + |
| 63 | + The None Network Driver: |
| 64 | + ------------------------ |
| 65 | + The None Network Driver does not provide any networking implementation. |
| 66 | + 1) Container is completely isolated from other containers and the host. |
| 67 | + 2) if you want networking with the None driver, you must set everything up manually. |
| 68 | + 3) None does create a separate networking namespace for each container, but no interfaces or endpoints. |
| 69 | + UseCases: When there is no need for container networking or you want to set all of the networking up yourself. |
| 70 | + |
| 71 | +Managing Networks: |
| 72 | +================== |
| 73 | + We can create and manager our own networks with the "docker network" commands. if we do not specify a network driver, bridge will be used by default. |
| 74 | + |
| 75 | + docker newtork ls |
| 76 | + docker network create NETWORK ( create a bridge network by default ) |
| 77 | + docker network create --driver bridge NETWORK |
| 78 | + docker network create --driver overlay NETWORK |
| 79 | + docker network inspect NETWORK |
| 80 | + docker network rm NETWORK |
| 81 | + |
| 82 | + docker network connect NETWORK CONTAINER |
| 83 | + docker network disconnect NETWORK CONTAINER |
| 84 | + |
| 85 | +Embedded DNS: |
| 86 | +============= |
| 87 | + Docker networks implements an embedded DNS server, allowing containers and services to locate and communicate with one another. |
| 88 | + Containers can communicate with other containers and services using the serice or container name, or network alias. |
| 89 | + |
| 90 | + docker run --network-alias ALIAS |
| 91 | + docker network connect --alias ALIAS |
| 92 | + |
| 93 | + Example: |
| 94 | + Create a container with a network alias and communicate with it from another container using both the name and the alias. |
| 95 | + docker network create my-net |
| 96 | + docker run -d --name my-net-nginx --network my-net --network-alias my-nginx-alias nginx |
| 97 | + docker exec my-net-busybox curl my-net-nginx2:80 |
| 98 | + docker exec my-net-busybox curl my-nginx-alias:80 |
| 99 | + |
| 100 | + Create a container and provide a network alias with the docker network connect command. |
| 101 | + docker run -d --name my-net-nginx2 nginx |
| 102 | + docker network connect --alias another-alias my-net my-net-nginx3 |
| 103 | + docker exec my-net-busybox curl another-alias:80 |
| 104 | + |
| 105 | +Publishing Ports for Services: |
| 106 | +============================== |
| 107 | +Host vs. Ingress |
| 108 | + Docker Swarm supports two modes for publishing ports for services. |
| 109 | + |
| 110 | + Ingress: |
| 111 | + 1) The default, used if no mode is specified. |
| 112 | + 2) Uses a routing mesh. The published port listens on every node in the cluster, and trasparently directs incoming traffic to any task that is part of the service, on any node. |
| 113 | + |
| 114 | + publish a service port host mode: |
| 115 | + docker service create -p 8081:80 --name nginx_ingress_pub nginx |
| 116 | + |
| 117 | + Host: |
| 118 | + 1) Publishes the port directly on the host where a task is running. |
| 119 | + 2) cannot have multiple replicas on the same node if you use a static port. |
| 120 | + 3) Traffic to the published port on the node goes directly to the task running on that specific node. |
| 121 | + |
| 122 | + publish a service port host mode: |
| 123 | + docker service create -p mode=host,published=8082,target=80 --name nginx_host_pub nginx |
0 commit comments