Skip to content

Commit

Permalink
pre-fetching sandbox image during bootstrap in containerd runtime (#730)
Browse files Browse the repository at this point in the history
Co-authored-by: Sinha <[email protected]>
  • Loading branch information
ravisinha0506 and ravisinha0506 authored Aug 10, 2021
1 parent 73693cd commit 5d109a1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 3 deletions.
8 changes: 7 additions & 1 deletion files/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,18 @@ fi
if [[ "$CONTAINER_RUNTIME" = "containerd" ]]; then
sudo mkdir -p /etc/containerd
sudo mkdir -p /etc/cni/net.d
sudo sed -i s,SANDBOX_IMAGE,$PAUSE_CONTAINER,g /etc/eks/containerd/containerd-config.toml
sudo mv /etc/eks/containerd/containerd-config.toml /etc/containerd/config.toml
sudo mv /etc/eks/containerd/sandbox-image.service /etc/systemd/system/sandbox-image.service
sudo mv /etc/eks/containerd/kubelet-containerd.service /etc/systemd/system/kubelet.service
sudo chown root:root /etc/systemd/system/kubelet.service
sudo chown root:root /etc/systemd/system/sandbox-image.service
systemctl daemon-reload
systemctl enable containerd
systemctl start containerd
systemctl restart containerd
systemctl enable sandbox-image
systemctl start sandbox-image

elif [[ "$CONTAINER_RUNTIME" = "dockerd" ]]; then
mkdir -p /etc/docker
bash -c "/sbin/iptables-save > /etc/sysconfig/iptables"
Expand Down
3 changes: 3 additions & 0 deletions files/containerd-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ address = "/run/dockershim.sock"
[plugins."io.containerd.grpc.v1.cri".containerd]
default_runtime_name = "runc"

[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "SANDBOX_IMAGE"

[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"

Expand Down
4 changes: 2 additions & 2 deletions files/kubelet-containerd.service
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/kubernetes/kubernetes
After=containerd.service
Requires=containerd.service
After=containerd.service sandbox-image.service
Requires=containerd.service sandbox-image.service

[Service]
ExecStartPre=/sbin/iptables -P FORWARD ACCEPT -w 5
Expand Down
27 changes: 27 additions & 0 deletions files/pull-sandbox-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

### fetching sandbox image from /etc/containerd/config.toml
sandbox_image=$(awk -F'[ ="]+' '$1 == "sandbox_image" { print $2 }' /etc/containerd/config.toml)
region=$(echo "$sandbox_image" | cut -f4 -d ".")
ecr_password=$(aws ecr get-login-password --region $region)
API_RETRY_ATTEMPTS=5

for attempt in `seq 0 $API_RETRY_ATTEMPTS`; do
rc=0
if [[ $attempt -gt 0 ]]; then
echo "Attempt $attempt of $API_RETRY_ATTEMPTS"
fi
### pull sandbox image from ecr
### username will always be constant i.e; AWS
sudo ctr --address=/run/dockershim.sock --namespace k8s.io image pull $sandbox_image --user AWS:$ecr_password
rc=$?;
if [[ $rc -eq 0 ]]; then
break
fi
if [[ $attempt -eq $API_RETRY_ATTEMPTS ]]; then
exit $rc
fi
jitter=$((1 + RANDOM % 10))
sleep_sec="$(( $(( 5 << $((1+$attempt)) )) + $jitter))"
sleep $sleep_sec
done
12 changes: 12 additions & 0 deletions files/sandbox-image.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=pull sandbox image defined in containerd config.toml
# pulls sandbox image using ctr tool
After=containerd.service
Requires=containerd.service

[Service]
Type=oneshot
ExecStart=/etc/eks/containerd/pull-sandbox-image.sh

[Install]
WantedBy=multi-user.target
8 changes: 8 additions & 0 deletions log-collector-script/linux/eks-log-collector.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ COMMON_DIRECTORIES=(
storage
var_log
networking
sandbox-image # eks
ipamd # eks
sysctls # eks
kubelet # eks
Expand Down Expand Up @@ -258,6 +259,7 @@ collect() {
get_networking_info
get_cni_config
get_docker_logs
get_sandboxImage_info
}

pack() {
Expand Down Expand Up @@ -551,6 +553,12 @@ get_containerd_info() {
ok
}

get_sandboxImage_info() {
try "Collect sandbox-image daemon information"
timeout 75 journalctl -u sandbox-image > "${COLLECT_DIR}"/sandbox-image/sandbox-image-log.txt 2>&1 || echo -e "\tTimed out, ignoring \"sandbox-image info output \" "
ok
}

get_docker_info() {
try "collect Docker daemon information"

Expand Down
3 changes: 3 additions & 0 deletions scripts/install-worker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ else
fi

sudo mv $TEMPLATE_DIR/kubelet-containerd.service /etc/eks/containerd/kubelet-containerd.service
sudo mv $TEMPLATE_DIR/sandbox-image.service /etc/eks/containerd/sandbox-image.service
sudo mv $TEMPLATE_DIR/pull-sandbox-image.sh /etc/eks/containerd/pull-sandbox-image.sh
sudo chmod 777 /etc/eks/containerd/pull-sandbox-image.sh

cat <<EOF | sudo tee -a /etc/modules-load.d/containerd.conf
overlay
Expand Down

0 comments on commit 5d109a1

Please sign in to comment.