Most traditional container networking has been handled by bridges and Network Address Translation (NAT), or by binding to the host’s network in some fashion. As the role of containers has evolved, so have the use cases for more dynamic network configurations. One such use case is users who want their containers to lease IP addresses from existing DHCP servers on their network infrastructure, with no changes to the container.
There are a couple of ways to resolve this issue, depending on your circumstances. The easiest is to use the macvlan
and dhcp
plugins that ship with containernetworking-plugins
.
Note: This solution only applies to rootfull containers. Rootless containers do not use containernetworking-plugins
.
About macvlan
The macvlan
plugin is part of the containernetworking-plugins
package. According to its description, macvlan
is similar to a switch that is connected to a host networking interface. Using virtual interfaces in combination with the host interface, each device can have a distinct MAC address. This fact allows existing DHCP servers on the existing network to interact with the devices and assign addresses.
The macvlan
plugin also requires the use of the dhcp
containernetworking-plugin
. The dhcp
plugin is a proxy DHCP client on behalf of the container.
Creating a CNI configuration file
To set up this scenario, you must first create a new container network interface (CNI) configuration file manually. Before creating the file, you must know which host interface will be used to bind against. This information can be found using IP tools or ifconfig
.
In the example below, the host network interface being bound to is enp0s31f6
. The other critical field is the network name; in this case, it is example
. This is the interface that connects to the target network:
$ cat /etc/cni/net.d/90-example.conflist
{
"cniVersion": "0.4.0",
"name": "example",
"plugins": [
{
"type": "macvlan",
"master": "enp0s31f6",
"ipam": {
"type": "dhcp"
}
}
]
}
Running the DHCP plugin
As mentioned earlier, the macvlan
and dhcp
containernetworking-plugins
act in concert. The dhcp
plugin is a proxy DHCP client for the container because most container images lack a DHCP client to interact with a DHCP server. While plugins can be manually run, you will usually automate the procedure. Both options are given below.
To manually run the plugin, run:
$ sudo /usr/libexec/cni/dhcp daemon &
When it comes to automating this procedure, because the dhcp
plugin uses a socket, its use can be automated nicely with systemd
. You simply need a socket and service file.
The socket file is as follows:
$ cat /usr/lib/systemd/system/io.podman.dhcp.socket
[Unit]
Description=DHCP Client for CNI
[Socket]
ListenStream=%t/cni/dhcp.sock
SocketMode=0600
[Install]
WantedBy=sockets.target
And the service file is:
$ cat /usr/lib/systemd/system/io.podman.dhcp.service
[Unit]
Description=DHCP Client CNI Service
Requires=io.podman.dhcp.socket
After=io.podman.dhcp.socket
[Service]
Type=simple
ExecStart=/usr/libexec/cni/dhcp daemon
TimeoutStopSec=30
KillMode=process
[Install]
WantedBy=multi-user.target
Also=io.podman.dhcp.socket
You only need to enable and start the socket. From there, systemd
will handle the rest:
$ sudo systemctl --now enable io.podman.dhcp.socket
Pulling it all together
Before using the macvlan
plugin, make sure that the dhcp
plugin is running. To use the network-enabled with macvlan
, simply designate it as the network to be used with the container using the --network
flag.
Consider the following example of running an Alpine container and checking the IP address of the network interface eth0
:
$ sudo podman run -it --rm --network example alpine ip addr show eth0
2: eth0@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 2a:53:89:31:01:73 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.222/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::2853:89ff:fe31:173/64 scope link
valid_lft forever preferred_lft forever
In the case of the example, the host network is 192.168.1.0/24. Note how the eth0
interface for the container leases an IP address of 192.168.1.122. Be warned, this configuration may exhaust the available DHCP addresses if you use a large number of short-lived containers and a DHCP server with long leases.
Note: Remember, the purpose of the dhcp
plugin is to be a proxy DHCP client for container images that do not have some sort of DHCP client installed (like dhclient
). If you would like to skip using the plugin, you can build a container image that contains a DHCP client and ensure that it is executed when the container image runs.
Alternate approaches to macvlan
As stated above, there are multiple ways to allow containers to lease IP addresses on a host’s network. While the macvlan
approach is the simplest, you can use a bridge-based approach using dhcp
's IP Address Management (IPAM) method. However, the complex configuration of the dhcp-relays
to allow interactions between the host, containers, and DHCP server is beyond the scope of this document.
Need more on networking? Download the Linux networking cheat sheet.
저자 소개
Brent Baude has been working in hardware and software engineering for 27 years as an employee of IBM and Red Hat. Of late, he has specialized in Containers. His current role is that of Podman Architect.
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.