다시 로드를 사용한 시스템 업그레이드 바이너리

다시 로드를 사용한 시스템 업그레이드 바이너리

SIGUSR2가 주어지면 가동 중지 시간 없이 새 프로세스를 포크하고 정상적으로 생성하는 프로그램이 있습니다(기존 모든 부모 소켓을 자식에게 전달하고 부모를 종료함). 따라서 일반적으로 systemd가 없으면 다음과 같습니다.

cp newbinary coredns
kill -s USR2 oldpid

ExecReload시스템에는 이것에 따른 것만 있습니다답변

즉, systemd는 기본 서비스가 진정한 재로드 기능을 지원하는 경우에만 "재로드"를 구현하기를 원합니다. 즉, 서비스를 종료하고 다시 시작하지 않거나 서비스가 PID를 변경하지 않는 재로드입니다. 즉, systemd는 존재하는 기능만 반영하려고 합니다.

이것이 시스템 파일과 같을 수 있습니까?

PIDFile=/tmp/coredns.pid
Type=forking
ExecStart=./coredns -pidfile /tmp/coredns.pid
ExecStop=kill `/tmp/coredns.pid`
ExecReload=/bin/kill -s USR2 `cat /tmp/coredns.pid`

문제는 을 수행할 때 sudo systemctl reload coredns시스템화된다는 것입니다.

  1. pid가 바뀐거 알아?
  2. 아직도 로그와 프로세스 모니터링을 제대로 처리하고 있나요?

답변1

좋습니다. 그렇지 않으면 forking시작할 oneoff때 멈춥니다. 유형은 이어야 하며 simple제대로 작동합니다.

echo '[Unit]
Description=Custom CoreDNS DNS server
After=network.target
[Service]
PermissionsStartOnly=true
LimitNOFILE=1048576
LimitNPROC=512
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
WorkingDirectory=/tmp/1
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/1/start.sh
ExecStop=/tmp/1/kill.sh
ExecReload=/tmp/1/reload.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target 
' | sudo tee /usr/lib/systemd/system/coredns.service
sudo systemctl daemon-reload 

스크립트 만들기:

mkdir -p /tmp/1
echo '#!/usr/bin/bash
kill -s USR2 `cat /tmp/1/coredns.pid`
' > /tmp/1/reload.sh
echo '#!/usr/bin/bash
kill -s USR2 `cat /tmp/1/coredns.pid`
' > /tmp/1/kill.sh
echo '#!/usr/bin/bash
/tmp/1/coredns -pidfile /tmp/1/coredns.pid -conf=/tmp/1/Corefile
' > /tmp/1/start.sh
chmod a+x /tmp/1/*.sh
chmod 777 /tmp/1 # just so that no need to set user permission on systemd
cp Corefile coredns /tmp/1/

제대로 시작할 수 있습니다:

sudo systemctl start coredns

# on journalctl:
Apr 20 18:50:06 pop2204 systemd[1]: Started Custom CoreDNS DNS server.
Apr 20 18:50:06 pop2204 start.sh[1553313]: whoami Name called
Apr 20 18:50:06 pop2204 start.sh[1553313]: whoami Name called
Apr 20 18:50:06 pop2204 start.sh[1553313]: whoami Name called
Apr 20 18:50:06 pop2204 start.sh[1553313]: .:1053
Apr 20 18:50:06 pop2204 start.sh[1553313]: CoreDNS-1.9.4
Apr 20 18:50:06 pop2204 start.sh[1553313]: linux/amd64, go1.20.3, 81159d2f-dirty

이전 바이너리를 제거한 다음(직접 복사하면 텍스트 파일이 사용 중이므로 오류 발생) 새 바이너리를 복사합니다.

rm /tmp/1/coredns
cp coredns /tmp/1/coredns
sudo systemctl reload coredns

# from journalctl:
Apr 20 18:52:03 pop2204 systemd[1]: Reloading Custom CoreDNS DNS server...
Apr 20 18:52:03 pop2204 start.sh[1553313]: [INFO] SIGUSR2: Upgrading
Apr 20 18:52:03 pop2204 start.sh[1553313]: [INFO] Upgrading
Apr 20 18:52:03 pop2204 systemd[1]: Reloaded Custom CoreDNS DNS server.
Apr 20 18:52:03 pop2204 start.sh[1553726]: whoami Name called 2
Apr 20 18:52:03 pop2204 start.sh[1553726]: whoami Name called 2
Apr 20 18:52:03 pop2204 start.sh[1553726]: whoami Name called 2
Apr 20 18:52:03 pop2204 start.sh[1553726]: .:1053
Apr 20 18:52:03 pop2204 start.sh[1553313]: [INFO] Upgrade finished
Apr 20 18:52:03 pop2204 start.sh[1553726]: CoreDNS-1.9.4
Apr 20 18:52:03 pop2204 start.sh[1553726]: linux/amd64, go1.20.3, 81159d2f-dirty

관련 정보