Skip to content

Commit 00a50e0

Browse files
rosskirkpateljohnson92
authored andcommitted
set NOFILE limits for systemd, improve error handling for pre-kubeadminit.sh
Signed-off-by: Ross Kirkpatrick <rosskirkpat@outlook.com>
1 parent 1bd9622 commit 00a50e0

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

scripts/pre-kubeadminit.sh

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
#!/bin/bash
22
set -euo pipefail
3+
34
DEFAULT_CONTAINERD_VERSION=1.7.24
45
DEFAULT_CNI_PLUGIN_VERSIONS=1.6.2
56
CONTAINERD_VERSION="${CONTAINERD_VERSION:=$DEFAULT_CONTAINERD_VERSION}"
67
CNI_PLUGIN_VERSIONS="${CNI_PLUGIN_VERSIONS:=$DEFAULT_CNI_PLUGIN_VERSIONS}"
8+
PATCH_VERSION=${1#[v]}
9+
VERSION=${PATCH_VERSION%.*}
710

811
# setup containerd config
9-
mkdir -p -m 755 /etc/containerd
12+
if ! mkdir -p /etc/containerd ; then
13+
echo "Error: Failed to create directory /etc/containerd" >&2
14+
exit 1
15+
fi
16+
chmod 0755 /etc/containerd
17+
1018
cat > /etc/containerd/config.toml << EOF
1119
version = 2
1220
imports = ["/etc/containerd/conf.d/*.toml"]
@@ -25,15 +33,25 @@ EOF
2533

2634
chmod 644 /etc/containerd/config.toml
2735

28-
mkdir -p -m 755 /etc/modules-load.d
36+
if ! mkdir -p /etc/modules-load.d ; then
37+
echo "Error: Failed to create directory /etc/modules-load.d" >&2
38+
exit 1
39+
fi
40+
chmod 0755 /etc/modules-load.d
41+
2942
cat > /etc/modules-load.d/k8s.conf << EOF
3043
overlay
3144
br_netfilter
3245
EOF
3346

3447
chmod 644 /etc/modules-load.d/k8s.conf
3548

36-
mkdir -p -m 755 /etc/sysctl.d
49+
if ! mkdir -p /etc/sysctl.d ; then
50+
echo "Error: Failed to create directory /etc/sysctl.d" >&2
51+
exit 1
52+
fi
53+
chmod 0755 /etc/sysctl.d
54+
3755
cat > /etc/sysctl.d/k8s.conf << EOF
3856
net.bridge.bridge-nf-call-iptables = 1
3957
net.bridge.bridge-nf-call-ip6tables = 1
@@ -47,6 +65,20 @@ modprobe overlay
4765
modprobe br_netfilter
4866
sysctl --system
4967

68+
if ! mkdir -p /etc/systemd/system.conf.d ; then
69+
echo "Error: Failed to create directory /etc/systemd/system.conf.d" >&2
70+
exit 1
71+
fi
72+
chmod 0755 /etc/systemd/system.conf.d
73+
74+
cat > /etc/systemd/system.conf.d/override.conf << EOF
75+
[Manager]
76+
# Set sane defaults for the NOFILE limits to support high-performance workloads:
77+
# - Soft limit (65535): Suitable for most containerized applications.
78+
# - Hard limit (1048576): Allows scaling for high-demand scenarios.
79+
DefaultLimitNOFILE=65535:1048576
80+
EOF
81+
5082
# containerd service
5183
cat > /usr/lib/systemd/system/containerd.service << EOF
5284
[Unit]
@@ -68,6 +100,7 @@ RestartSec=5
68100
# in the kernel. We recommend using cgroups to do container-local accounting.
69101
LimitNPROC=infinity
70102
LimitCORE=infinity
103+
LimitNOFILE=infinity
71104
72105
# Comment TasksMax if your systemd version does not supports it.
73106
# Only systemd 226 and above support this version.
@@ -96,7 +129,12 @@ RestartSec=10
96129
WantedBy=multi-user.target
97130
EOF
98131

99-
mkdir -p /usr/lib/systemd/system/kubelet.service.d
132+
if ! mkdir -p /usr/lib/systemd/system/kubelet.service.d ; then
133+
echo "Error: Failed to create directory /usr/lib/systemd/system/kubelet.service.d" >&2
134+
exit 1
135+
fi
136+
chmod 0755 /usr/lib/systemd/system/kubelet.service.d
137+
100138
cat > /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf << EOF
101139
# Note: This dropin only works with kubeadm and kubelet v1.11+
102140
[Service]
@@ -116,36 +154,40 @@ swapoff -a
116154
# check for required tools and only install missing tools
117155
REQUIRED_TOOLS=(runc socat conntrack ethtool iptables)
118156
INSTALL_TOOLS=()
119-
for tool in ${REQUIRED_TOOLS[*]}; do
157+
for tool in "${REQUIRED_TOOLS[@]}"; do
120158
echo "checking for ${tool}"
121-
if [ ! -x "$(command -v ${tool})" ]; then
159+
if [ ! -x "$(command -v "${tool}")" ]; then
122160
echo "${tool} is missing"
123-
INSTALL_TOOLS+=(${tool})
161+
INSTALL_TOOLS+=("${tool}")
124162
fi
125163
done
126164
export DEBIAN_FRONTEND=noninteractive
127165
apt-get update -y
128-
apt-get install -y ${INSTALL_TOOLS[*]}
166+
if [ "${#INSTALL_TOOLS[@]}" -gt 0 ]; then
167+
apt-get install -y "${INSTALL_TOOLS[@]}"
168+
fi
129169

130170
# install containerd
131171
curl -L "https://github.com/containerd/containerd/releases/download/v${CONTAINERD_VERSION}/containerd-${CONTAINERD_VERSION}-linux-amd64.tar.gz" | tar -C /usr/local -xz
132172

133173
# install cni plugins
134-
mkdir -p /opt/cni/bin
174+
if ! mkdir -p /opt/cni/bin ; then
175+
echo "Error: Failed to create directory /opt/cni/bin" >&2
176+
exit 1
177+
fi
178+
135179
curl -L "https://github.com/containernetworking/plugins/releases/download/v${CNI_PLUGIN_VERSIONS}/cni-plugins-linux-amd64-v${CNI_PLUGIN_VERSIONS}.tgz" | tar -C /opt/cni/bin -xz
136180
chown -R root:root /opt/cni
137181

138-
PATCH_VERSION=${1#[v]}
139-
VERSION=${PATCH_VERSION%.*}
140-
141182
# install crictl
142183
curl -L "https://github.com/kubernetes-sigs/cri-tools/releases/download/v${VERSION}.0/crictl-v${VERSION}.0-linux-amd64.tar.gz" | tar -C /usr/local/bin -xz
143184

144185
# install kubeadm,kubelet,kubectl
145186
cd /usr/local/bin
146-
curl -L --remote-name-all https://dl.k8s.io/release/$1/bin/linux/amd64/{kubeadm,kubelet}
187+
curl -L --remote-name-all "https://dl.k8s.io/release/$1/bin/linux/amd64/{kubeadm,kubelet}"
147188
curl -LO "https://dl.k8s.io/release/v${VERSION}.0/bin/linux/amd64/kubectl"
148189
chmod +x {kubeadm,kubelet,kubectl}
190+
149191
# reload systemd to pick up containerd & kubelet settings
150192
systemctl daemon-reload
151193
systemctl enable --now containerd kubelet

0 commit comments

Comments
 (0)