[Kubernetes] ๐ kubeadm ์ธ์ฆ์ ์๋ ๊ฐฑ์ : systemd ํ์ด๋จธ๋ก ๋ง๋ฃ ๋ฐฉ์งํ๊ธฐ
kubeadm์ผ๋ก ๊ตฌ์ถํ Kubernetes ํด๋ฌ์คํฐ์ ์ธ์ฆ์๋ฅผ systemd ํ์ด๋จธ์ ์๋น์ค๋ก ์๋ ๊ฐฑ์ ํ๋ ๋ฐฉ๋ฒ์ ๋จ๊ณ๋ณ๋ก ์ ๋ฆฌํ์ต๋๋ค. ๋ง๋ฃ 30์ผ ์ ๊ฐ์ง ํ kubeadm certs renew all๋ก 1๋ ์ฐ์ฅํฉ๋๋ค.
kubeadm์ผ๋ก ์ค์นํ Kubernetes ํด๋ฌ์คํฐ์ ์ปจํธ๋กค ํ๋ ์ธ ์ธ์ฆ์๋ ๊ธฐ๋ณธ ์ ํจ๊ธฐ๊ฐ์ด 1๋
์
๋๋ค. ์ด์ ์ค์ธ ํด๋ฌ์คํฐ์์ ์ธ์ฆ์๊ฐ ๋ง๋ฃ๋๋ฉด API ์๋ฒ, ์ปจํธ๋กค๋ฌ ๋งค๋์ , ์ค์ผ์ค๋ฌ๊ฐ ํต์ ์ ๋ฉ์ถ๋ฉด์ ํด๋ฌ์คํฐ ์ ์ฒด๊ฐ ๋ง๋น๋ฉ๋๋ค. ์ด ๊ธ์์๋ systemd ํ์ด๋จธ๋ก ๋ง๋ฃ 30์ผ ์ ์ ์๋์ผ๋ก kubeadm certs renew all์ ์คํํ์ฌ ์ธ์ฆ์๋ฅผ ๋ฌด์ค๋จ์ผ๋ก ๊ฐฑ์ ํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํฉ๋๋ค.
๐ kubeadm ์ธ์ฆ์๋?
kubeadm์ ํด๋ฌ์คํฐ ๋ถํธ์คํธ๋ฉ ์ ์ปจํธ๋กค ํ๋ ์ธ ๊ตฌ์ฑ์์๊ฐ ์ฌ์ฉํ๋ TLS ์ธ์ฆ์๋ฅผ ์๋์ผ๋ก ์์ฑํฉ๋๋ค. ๋ชจ๋ ์ธ์ฆ์๋ /etc/kubernetes/pki/ ๊ฒฝ๋ก์ ์ ์ฅ๋ฉ๋๋ค.
| ๊ตฌ๋ถ | ์ธ์ฆ์ | ์ฉ๋ |
|---|---|---|
| CA | ca.crt, ca.key | ํด๋ฌ์คํฐ ๋ฃจํธ CA (์ ํจ๊ธฐ๊ฐ 10๋ ) |
| API Server | apiserver.crt | kube-apiserver HTTPS |
| API Server (kubelet) | apiserver-kubelet-client.crt | apiserver โ kubelet ํต์ |
| Front Proxy | front-proxy-*.crt | Aggregation Layer |
| etcd | etcd/*.crt | etcd ๋ฉค๋ฒ ๊ฐ + ํด๋ผ์ด์ธํธ ํต์ |
| kubeconfig ๋ด ์ธ์ฆ์ | admin.conf, controller-manager.conf, scheduler.conf | ์ปจํธ๋กค ํ๋ ์ธ ์ปดํฌ๋ํธ ์ธ์ฆ |
Tip: CA ์ธ์ฆ์๋ 10๋ ์ด์ง๋ง ๊ทธ ์ธ leaf ์ธ์ฆ์๋ ๋ชจ๋ 1๋ ์ ๋๋ค. CA๊ฐ ๋ง๋ฃ๋๋ฉด ํด๋ฌ์คํฐ ์ฌ์ค์น ์์ค์ ์์ ์ด ํ์ํ๋, leaf ์ธ์ฆ์ ๊ฐฑ์ ์ CA ๋ง๋ฃ์ผ๋ ํจ๊ป ํ์ธํด์ผ ํฉ๋๋ค.
๐ ์ธ์ฆ์ ๋ง๋ฃ์ผ ํ์ธํ๊ธฐ
kubeadm์ผ๋ก ํ์ธ
kubeadm certs check-expiration ๋ช
๋ น์ผ๋ก ๋ชจ๋ ์ธ์ฆ์์ ๋ง๋ฃ์ผ์ ํ ๋ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
1
sudo kubeadm certs check-expiration
์ถ๋ ฅ ์์:
1
2
3
4
5
6
7
8
9
10
11
12
13
CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
admin.conf May 26, 2027 03:00 UTC 364d ca no
apiserver May 26, 2027 03:00 UTC 364d ca no
apiserver-kubelet-client May 26, 2027 03:00 UTC 364d ca no
controller-manager.conf May 26, 2027 03:00 UTC 364d ca no
etcd-server May 26, 2027 03:00 UTC 364d etcd-ca no
front-proxy-client May 26, 2027 03:00 UTC 364d front-proxy-ca no
scheduler.conf May 26, 2027 03:00 UTC 364d ca no
CERTIFICATE AUTHORITY EXPIRES RESIDUAL TIME
ca May 24, 2036 03:00 UTC 9y
etcd-ca May 24, 2036 03:00 UTC 9y
front-proxy-ca May 24, 2036 03:00 UTC 9y
openssl๋ก ๊ฐ๋ณ ํ์ธ
ํน์ ์ธ์ฆ์๋ง ๋น ๋ฅด๊ฒ ํ์ธํ๊ณ ์ถ๋ค๋ฉด openssl์ ์ฌ์ฉํฉ๋๋ค.
1
2
sudo openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -enddate
# notAfter=May 26 03:00:00 2027 GMT
๐ ๏ธ ์๋ ๊ฐฑ์ ๋ฐฉ๋ฒ
๊ธด๊ธ ์ํฉ์์๋ kubeadm certs renew all๋ก ์ฆ์ ๊ฐฑ์ ํ ์ ์์ต๋๋ค.
1
2
3
4
5
6
7
8
9
10
# 1. ๋ชจ๋ ์ธ์ฆ์ ๊ฐฑ์ (๋จ์ ๊ธฐ๊ฐ๊ณผ ๋ฌด๊ดํ๊ฒ 1๋
์ฐ์ฅ)
sudo kubeadm certs renew all
# 2. ์ปจํธ๋กค ํ๋ ์ธ Pod ์ฌ์์ (์ ์ Pod์ด๋ฏ๋ก ์ปจํ
์ด๋๋ง ์ฌ๊ธฐ๋)
sudo crictl pods --namespace kube-system \
--name 'kube-apiserver-*|kube-controller-manager-*|kube-scheduler-*|etcd-*' -q \
| xargs sudo crictl rmp -f
# 3. admin.conf ๋ฐฑ์
ํ .kube/config ๊ฐฑ์
sudo cp /etc/kubernetes/admin.conf /root/.kube/config
โ ๏ธ kubelet ์ธ์ฆ์(
/var/lib/kubelet/pki/kubelet-client-current.pem)๋ ๋ณ๋๋ก ์๋ ํ์ ๋ฉ๋๋ค.kubelet.conf์rotateCertificates: true๊ฐ ์ค์ ๋์ด ์๋์ง ํ์ธํ์ธ์.
โฐ systemd ํ์ด๋จธ๋ก ์๋ ๊ฐฑ์ ๊ตฌ์ฑํ๊ธฐ
์๋ ๊ฐฑ์ ์ ํด๋จผ ์๋ฌ๋ก ๋๋ฝ๋๊ธฐ ์ฝ์ต๋๋ค. ๋งค์ฃผ ์์์ผ ์๋ฒฝ 3์์ ๋ง๋ฃ์ผ์ ์ ๊ฒํ๊ณ , 30์ผ ๋ฏธ๋ง์ด๋ฉด ์๋์ผ๋ก ๊ฐฑ์ ํ๋ systemd ํ์ด๋จธ๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
๋์ ํ๋ฆ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
k8s-certs-renew.timer (๋งค์ฃผ ์ 03:00)
โ ํธ๋ฆฌ๊ฑฐ
โผ
k8s-certs-renew.service (oneshot)
โ ExecStart
โผ
/usr/local/bin/kube-scripts/k8s-certs-renew.sh
โ
โโ kubeadm certs check-expiration
โโ ๋ง๋ฃ๊น์ง 30์ผ ๋ฏธ๋ง์ด๋ฉด:
โ โโ kubeadm certs renew all
โ โโ crictl๋ก ์ปจํธ๋กค ํ๋ ์ธ Pod ์ฌ๊ธฐ๋
โ โโ admin.conf โ /root/.kube/config ๋ณต์ฌ
โโ apiserver ๊ธฐ๋ ๋๊ธฐ (6443 ํฌํธ)
1๏ธโฃ ๊ฐฑ์ ์คํฌ๋ฆฝํธ ์์ฑ
/usr/local/bin/kube-scripts/k8s-certs-renew.sh ํ์ผ์ ์์ฑํฉ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
sudo mkdir -p /usr/local/bin/kube-scripts
sudo tee /usr/local/bin/kube-scripts/k8s-certs-renew.sh > /dev/null <<'EOF'
#!/bin/bash
kubeadmCerts='/usr/local/bin/kubeadm certs'
getCertValidDays() {
local earliestExpireDate
earliestExpireDate=$(${kubeadmCerts} check-expiration \
| grep -o "[A-Za-z]\{3,4\}\s\w\w,\s[0-9]\{4,\}\s\w*:\w*\s\w*\s*" \
| xargs -I {} date -d {} +%s | sort | head -n 1)
local today
today="$(date +%s)"
echo -n $(( (earliestExpireDate - today) / (24 * 60 * 60) ))
}
echo "## Expiration before renewal ##"
${kubeadmCerts} check-expiration
if [ "$(getCertValidDays)" -lt 30 ]; then
echo "## Renewing certificates managed by kubeadm ##"
${kubeadmCerts} renew all
echo "## Restarting control plane pods managed by kubeadm ##"
crictl pods --namespace kube-system \
--name 'kube-scheduler-*|kube-controller-manager-*|kube-apiserver-*|etcd-*' -q \
| xargs crictl rmp -f
echo "## Updating /root/.kube/config ##"
cp /etc/kubernetes/admin.conf /root/.kube/config
fi
echo "## Waiting for apiserver to be up again ##"
until printf "" 2>>/dev/null >>/dev/tcp/127.0.0.1/6443; do sleep 1; done
echo "## Expiration after renewal ##"
${kubeadmCerts} check-expiration
EOF
sudo chmod +x /usr/local/bin/kube-scripts/k8s-certs-renew.sh
Tip:
kubeadm๋ฐ์ด๋๋ฆฌ ๊ฒฝ๋ก๋ ํ๊ฒฝ๋ง๋ค ๋ค๋ฆ ๋๋ค.which kubeadm์ผ๋ก ํ์ธ ํ ์คํฌ๋ฆฝํธ์kubeadmCerts๋ณ์๋ฅผ ์กฐ์ ํ์ธ์. (/usr/bin/kubeadm๋๋/usr/local/bin/kubeadm)
2๏ธโฃ systemd ์๋น์ค ์์ฑ
/etc/systemd/system/k8s-certs-renew.service ํ์ผ์ ์์ฑํฉ๋๋ค.
1
2
3
4
5
6
7
8
sudo tee /etc/systemd/system/k8s-certs-renew.service > /dev/null <<EOF
[Unit]
Description=Renew K8S control plane certificates
[Service]
Type=oneshot
ExecStart=/usr/local/bin/kube-scripts/k8s-certs-renew.sh
EOF
3๏ธโฃ systemd ํ์ด๋จธ ์์ฑ
/etc/systemd/system/k8s-certs-renew.timer ํ์ผ์ ์์ฑํฉ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
sudo tee /etc/systemd/system/k8s-certs-renew.timer > /dev/null <<EOF
[Unit]
Description=Timer to renew K8S control plane certificates
[Timer]
OnCalendar=Mon *-*-* 03:00:00
Unit=k8s-certs-renew.service
[Install]
WantedBy=multi-user.target
EOF
4๏ธโฃ ํ์ด๋จธ ํ์ฑํ
1
2
sudo systemctl daemon-reload
sudo systemctl enable --now k8s-certs-renew.timer
5๏ธโฃ ํ์ด๋จธ ์ํ ํ์ธ
1
2
3
4
5
# ๋ค์ ์คํ ์๊ฐ ํ์ธ
systemctl list-timers k8s-certs-renew.timer
# ์๋น์ค ๋ก๊ทธ ํ์ธ
journalctl -u k8s-certs-renew.service --no-pager
์ถ๋ ฅ ์์:
1
2
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2026-06-01 03:00:00 UTC 5d 10h - - k8s-certs-renew.timer k8s-certs-renew.service
๐งช ๋์ ํ ์คํธ
์ค์ ๋ง๋ฃ๊ฐ ์๋ฐํ์ง ์์๋ ์๋์ผ๋ก ์๋น์ค๋ฅผ ์คํํด ๋์์ ๊ฒ์ฆํ ์ ์์ต๋๋ค.
1
2
3
4
5
# ์๋ ์คํ (renew ๋ถ๊ธฐ๋ ํ์ง ์๊ณ check-expiration๋ง ์ถ๋ ฅ๋จ)
sudo systemctl start k8s-certs-renew.service
# ๊ฒฐ๊ณผ ํ์ธ
journalctl -u k8s-certs-renew.service -f
๋ง๋ฃ 30์ผ ๋ฏธ๋ง ์กฐ๊ฑด์ ๊ฐ์ ๋ก ๋ง๋ค๊ณ ์ถ๋ค๋ฉด ์คํฌ๋ฆฝํธ์ if [ "$(getCertValidDays)" -lt 30 ] ๋ถ๋ถ์ ์์๋ก -lt 9999๋ก ๋ณ๊ฒฝํ ๋ค ์คํ โ ๋์ ํ์ธ ํ ์๋ณตํฉ๋๋ค.
โ ๏ธ ์ด์ ์ ์ฃผ์์ฌํญ
| ํญ๋ชฉ | ๋ด์ฉ |
|---|---|
| HA ํด๋ฌ์คํฐ | ๋ชจ๋ ์ปจํธ๋กค ํ๋ ์ธ ๋ ธ๋์ ๋์ผํ ํ์ด๋จธ๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค. ๋ ธ๋๋ณ๋ก ์ธ์ฆ์๊ฐ ๋ฐ๋ก ๋ฐ๊ธ๋์ด ์์ต๋๋ค. |
| CA ๋ง๋ฃ | CA ์ธ์ฆ์๋ kubeadm certs renew all๋ก ๊ฐฑ์ ๋์ง ์์ต๋๋ค. 10๋
๋ง๋ฃ๊ฐ ๊ฐ๊น์์ง๋ฉด ๋ณ๋ ์ ์ฐจ๊ฐ ํ์ํฉ๋๋ค. |
| kubeconfig ์ฌ์ฉ์ | /root/.kube/config๋ง ๊ฐฑ์ ๋ฉ๋๋ค. ๋ค๋ฅธ ์ฌ์ฉ์๊ฐ ์ฌ์ฉํ๋ kubeconfig๋ ๋ณ๋๋ก ์
๋ฐ์ดํธํด์ผ ํฉ๋๋ค. |
| ์ธ๋ถ ๊ด๋ฆฌ ์ธ์ฆ์ | EXTERNALLY MANAGED=yes๋ก ํ์๋๋ ์ธ์ฆ์๋ ๊ฐฑ์ ๋์ง ์์ต๋๋ค. (์: Vault, cert-manager ๋ฐ๊ธ) |
| etcd ๋ฐฑ์ | ๊ฐฑ์ ์์ ์ ๋ฐ๋์ etcd ์ค๋ ์ท์ ํ๋ณดํฉ๋๋ค. |
โ ์์ฃผ ๋ฌป๋ ์ง๋ฌธ
Q. kubeadm์ผ๋ก ์ค์นํ์ง ์์ ํด๋ฌ์คํฐ์๋ ์ ์ฉ ๊ฐ๋ฅํ๊ฐ์?
์๋์. ์ด ์คํฌ๋ฆฝํธ๋ kubeadm certs renew ๋ช
๋ น์ ์์กดํฉ๋๋ค. kubespray๋ก ์ค์นํ ํด๋ฌ์คํฐ๋ kubeadm ๊ธฐ๋ฐ์ด๋ผ ์ ์ฉ ๊ฐ๋ฅํ์ง๋ง, k3s๋ RKE2์ฒ๋ผ ์์ฒด ์ธ์ฆ์ ๊ด๋ฆฌ ๋ฉ์ปค๋์ฆ์ด ์๋ ๋ฐฐํฌํ์ ๊ฐ ๋๊ตฌ๊ฐ ์ ๊ณตํ๋ ํ์ ๋ฐฉ์์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
Q. ์ธ์ฆ์ ์ ํจ๊ธฐ๊ฐ์ 1๋ ๋ณด๋ค ๊ธธ๊ฒ ์ค์ ํ ์ ์๋์?
๊ฐ๋ฅํฉ๋๋ค. kubeadm init ์ --cert-validity-period ํ๋๊ทธ(v1.31+)๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ClusterConfiguration์์ certificateValidityPeriod๋ฅผ ์ค์ ํฉ๋๋ค. ๋ค๋ง ๋ณด์ ๋ชจ๋ฒ ์ฌ๋ก๋ ์งง์ ์ฃผ๊ธฐ๋ก ์์ฃผ ๊ฐฑ์ ํ๋ ๊ฒ์ด๋ผ, 1๋
์ ์ ์งํ๊ณ ์๋ํ๋ฅผ ๋์
ํ๋ ํธ์ด ๊ถ์ฅ๋ฉ๋๋ค.
Q. ๊ฐฑ์ ์ ํด๋ฌ์คํฐ๊ฐ ์ค๋จ๋๋์?
์ปจํธ๋กค ํ๋ ์ธ Pod ์ฌ๊ธฐ๋ ์ ์ ์ด๊ฐ API ์๋ต์ด ์ง์ฐ๋ ์ ์์ง๋ง, ์ํฌ๋ก๋(Pod)์๋ ์ํฅ์ด ์์ต๋๋ค. HA ํด๋ฌ์คํฐ๋ผ๋ฉด ๋ ธ๋๋ณ ํ์ด๋จธ๋ฅผ ๋์ผ ์๊ฐ์ผ๋ก ์ค์ ํ์ง ๋ง๊ณ 5~10๋ถ ๊ฐ๊ฒฉ์ผ๋ก ๋ถ์ฐํ๋ ๊ฒ์ด ์์ ํฉ๋๋ค.
Q. kubelet ์ธ์ฆ์๋ ์ด๋ป๊ฒ ๊ฐฑ์ ๋๋์?
kubelet ์ธ์ฆ์๋ RotateKubeletClientCertificate ๊ธฐ๋ฅ ๊ฒ์ดํธ(๊ธฐ๋ณธ ํ์ฑํ)์ ์ํด ์๋ ํ์ ๋ฉ๋๋ค. kubeadm certs renew๊ฐ ๊ด์ฌํ์ง ์์ต๋๋ค. /var/lib/kubelet/pki/ ์๋์ kubelet-client-current.pem ์ฌ๋ณผ๋ฆญ ๋งํฌ๊ฐ ์ฃผ๊ธฐ์ ์ผ๋ก ๊ฐฑ์ ๋ฉ๋๋ค.
Q. ๊ฐฑ์ ํ kubectl ๋ช
๋ น์ด ์คํจํฉ๋๋ค.
/etc/kubernetes/admin.conf์ ์ธ์ฆ์๋ ๊ฐฑ์ ๋์ง๋ง ์ฌ์ฉ์ ํ์ ~/.kube/config๊ฐ ๊ฐฑ์ ๋์ง ์์ ๊ฒฝ์ฐ์
๋๋ค. ๋ค์ ๋ช
๋ น์ผ๋ก ๋๊ธฐํํฉ๋๋ค.
1
2
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config