In Part 1, we created a working BIND container with local data storage. We can make changes on the local system that will get picked up in the running container. In this part, we’ll explore how we can manage the service from the host with systemctl
.
In Part 1, we installed two OCI hooks, oci-register-machine
and oci-systemd-hook
. OCI hooks are executed either before the container process is executed (prestart) or after shut down (poststop). These two hooks provide the integration points to systemd on the host. We used machinectl
in the previous post to copy files out of our test container, but we didn’t look at what else this integration can do for us.
Using machinectl list
, we see what containers systemd knows about. The MACHINE field holds the machine-id created by oci-register-machine
from the container ID. We can use that ID with both machinectl
and systemctl
.
With machinectl status
we can see some interesting things about our container, like IP address, OS release info, and the systemd hierarchy running. Since this is from the host view, you’ll see the host PID for /sbin/init
not PID 1.
[root@rhel7-host bind]# machinectl status 47d2c93035cafb1174dedd924cfa4308 47d2c93035cafb1174dedd924cfa4308(47d2c93035cafb1174dedd924cfa4308) Since: Mon 2017-05-15 12:01:19 EDT; 3h 58min ago Leader: 2734 (systemd) Service: docker; class container Root: /var/lib/docker/devicemapper/mnt/5d897b599c6b003df74968f73ad56 Address: 172.17.0.2 fe80::42:acff:fe11:2 OS: Red Hat Enterprise Linux Server 7.3 (Maipo) Unit: docker-47d2c93035cafb1174dedd924cfa4308ebb8b924bc0b0b661f60ffe ├─2734 /sbin/init └─system.slice ├─named.service │ └─2788 /usr/sbin/named -u named ├─dbus.service │ └─2777 /bin/dbus-daemon --system --address=systemd: --nofo └─systemd-journald.service └─2766 /usr/lib/systemd/systemd-journald
CONTAINER SYSTEMD
We can also get status in the container via systemctl
on the host by specifying the -M
option. You can query the overall systemd status, the process status, and interact with the process as if it were on the host.
To see the internal container state and check for failed or queued jobs:
[root@rhel7-host bind]# systemctl -M 47d2c93035cafb1174dedd924cfa4308 status
To see the status of the named
service in container:
[root@rhel7-host bind]# systemctl -M 47d2c93035cafb1174dedd924cfa4308 status named
To see what actions are available to us via the unit file, you can use show
service. Let’s grep for Exec to see what’s available.
[root@rhel7-host bind]# systemctl -M 47d2c93035cafb1174dedd924cfa4308 show named | grep Exec ExecStart={ path=/usr/sbin/named ; argv[]=/usr/sbin/named -u named $OPTIONS ; ignore_errors=no ; start_time=[Mon 2017-05-15 12:01:20 EDT] ; stop_time=[Mon 2017-05-15 12:01:20 EDT] ; pid=26 ; code=exited ; status=0 } ExecReload={ path=/bin/sh ; argv[]=/bin/sh -c /usr/sbin/rndc reload > /dev/null 2>&1 || /bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 } ExecStop={ path=/bin/sh ; argv[]=/bin/sh -c /usr/sbin/rndc stop > /dev/null 2>&1 || /bin/kill -TERM $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }
The named
unit file exposes a start, stop, and reload action. We also can use reload-or-restart
and let systemd determine what to do instead of searching the show
output.
[root@rhel7-host bind]# systemctl -M 47d2c93035cafb1174dedd924cfa4308 reload-or-restart named [root@rhel7-host bind]# systemctl -M 47d2c93035cafb1174dedd924cfa4308 status named named.service - Berkeley Internet Name Domain (DNS) Loaded: loaded (/usr/lib/systemd/system/named.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2017-05-15 12:01:20 EDT; 4h 32min ago Process: 54 ExecReload=/bin/sh -c /usr/sbin/rndc reload > /dev/null 2>&1 || /bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS) Process: 26 ExecStart=/usr/sbin/named -u named $OPTIONS (code=exited, status=0/SUCCESS) Process: 24 ExecStartPre=/bin/bash -c if [ ! "$DISABLE_ZONE_CHECKING" == "yes" ]; then /usr/sbin/named-checkconf -z /etc/named.conf; else echo "Checking of zone files is disabled"; fi (code=exited, status=0/SUCCESS) Main PID: 27 (khugepaged) CGroup: /system.slice/docker-47d2c93035cafb1174dedd924cfa4308ebb8b924bc0b0b661f60ffee3e4bf715.scope/system.slice/named.service └─2788 /usr/sbin/named -u named ‣ 27 [khugepaged]
Be sure to note, if you stop the service running in the container with systemctl
, that doesn’t stop the container. We’re directly manipulating the service in these commands not the container. You could use systemctl -M halt
to stop the container.
CREATING SYSTEMD UNIT
Now that we’ve looked at manually manipulating the service from systemctl
, the only thing left is to start the container when the host starts, so we’re never without DNS. We’ll create a simple unit file for the service, and then enable it on the host.
[root@rhel7-host ~]# vi named-container.service [Unit] Description=Containerized BIND service Requires=docker.service After=docker.service [Service] Restart=on-failure RestartSec=10 ExecStart=/usr/bin/docker start -a %p ExecStop=-/usr/bin/docker stop -t 2 %p [Install] WantedBy=multi-user.target
Since the name of the unit matches the name of the container we built, we use the %p
specifier to pass the right argument. In this case, that’s named-container
. If you wanted different names for the unit and container, you could replace the %p
with the name of the container. Copy the unit file to /etc/systemd/system
and we can manipulate the container like a service on the host.
Let’s use restart
so we know the container is managed by systemd and not already running from a previous step, then we can check the status of BIND inside the container.
[root@rhel7-host bind]# cp named-container.service /etc/systemd/system/ [root@rhel7-host bind]# systemctl enable named-container Created symlink from /etc/systemd/system/multi-user.target.wants/named-container.service to /etc/systemd/system/named-container.service. [root@rhel7-host bind]# systemctl restart named-container [root@rhel7-host bind]# systemctl status named-container named-container.service - Containerized BIND service Loaded: loaded (/etc/systemd/system/named-container.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2017-05-10 16:45:44 EDT; 26s ago Main PID: 23455 (docker-current) Memory: 5.9M CGroup: /system.slice/named-container.service └─23455 /usr/bin/docker-current start -a named-container May 10 16:45:44 rhel7-host systemd[1]: Started Containerized BIND service. May 10 16:45:44 rhel7-host systemd[1]: Starting Containerized BIND service... [root@rhel7-host bind]# systemctl -M 47d2c93035cafb1174dedd924cfa4308 status named named.service - Berkeley Internet Name Domain (DNS) Loaded: loaded (/usr/lib/systemd/system/named.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2017-05-10 16:44:52 EDT; 1min 49s ago Process: 26 ExecStart=/usr/sbin/named -u named $OPTIONS (code=exited, status=0/SUCCESS) Process: 24 ExecStartPre=/bin/bash -c if [ ! "$DISABLE_ZONE_CHECKING" == "yes" ]; then /usr/sbin/named-checkconf -z /etc/named.conf; else echo "Checking of zone files is disabled"; fi (code=exited, status=0/SUCCESS) Main PID: 27 (khugepaged) CGroup: /system.slice/docker-47d2c93035cafb1174dedd924cfa4308ebb8b924bc0b0b661f60ffee3e4bf715.scope/system.slice/named.service └─23438 /usr/sbin/named -u named ‣ 27 [khugepaged]
There we have it, a containerized BIND service that can be updated independently of any other services of the system, yet easily managed and updated from the host.
LOOKING AHEAD
This concept of a system service container isn’t quite the same as what some others are calling “system containers”. System containers are simply stand alone containers that provide services. The distinction isn’t between apps and OS containers, but if the application in the container benefits from distribution and orchestration. Typically, BIND doesn’t need to scale up and down based on usage, so it wouldn’t benefit from a full orchestration configuration.
System containers are something we think can be very useful. In the Container Catalog you'll find some supported examples, like etcd. This container uses the atomic
command found on the Red Hat Enterprise Linux Atomic Host to install and configure the service. These system containers are built somewhat differently than we did here, using runc
to launch the container. There are definite benefits to the approach and you can read more about the install and run process for etcd in the official documentation.
If you'd like to see how the upstream work is progressing on creating and managing these sorts of system containers, you can take a look at the Project Atomic repository.
Additional Resources:
GitHub repository for accompanying files: https://github.com/nzwulfin/named-container
저자 소개
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
오리지널 쇼
엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리
제품
- Red Hat Enterprise Linux
- Red Hat OpenShift Enterprise
- Red Hat Ansible Automation Platform
- 클라우드 서비스
- 모든 제품 보기
툴
체험, 구매 & 영업
커뮤니케이션
Red Hat 소개
Red Hat은 Linux, 클라우드, 컨테이너, 쿠버네티스 등을 포함한 글로벌 엔터프라이즈 오픈소스 솔루션 공급업체입니다. Red Hat은 코어 데이터센터에서 네트워크 엣지에 이르기까지 다양한 플랫폼과 환경에서 기업의 업무 편의성을 높여 주는 강화된 기능의 솔루션을 제공합니다.