Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion scripts/install-cilium.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,62 @@ if kubectl get daemonset -n kube-system cilium &>/dev/null; then
fi
fi

# Ensure the portmap CNI plugin binary is available BEFORE running
# 'cilium install'. cni.chainingMode=portmap makes cilium-agent drop a
# conflist that references portmap as soon as the agent pod is up; if
# portmap is missing from /opt/cni/bin at that moment, kubelet retries
# every pending pod (coredns, traefik, local-path-provisioner — none
# use hostPort but all traverse the CNI chain on every ADD) against
# the missing plugin and they fall into sandbox-creation backoff
# (capped at 5 min). Cilium only installs cilium-cni into its binPath
# (default /opt/cni/bin) — not portmap — so we copy it from k3s's
# bundled CNI bin dir, which is already populated by the time this
# script runs. No new network dependency.
#
# sudo test -x (not [ -x ... ]) so a hardened parent-dir mode that
# blocks the invoking user's traverse surfaces as a real permissions
# error instead of a misleading "not found".
log "Verifying portmap CNI plugin binary is available..."
CNI_BIN_DIR="/opt/cni/bin"
if ! sudo test -x "$CNI_BIN_DIR/portmap"; then
K3S_CNI_BIN_DIR="/var/lib/rancher/k3s/data/current/bin"
if sudo test -x "$K3S_CNI_BIN_DIR/portmap"; then
log " portmap missing from ${CNI_BIN_DIR}; copying from ${K3S_CNI_BIN_DIR}..."
sudo mkdir -p "$CNI_BIN_DIR"
sudo cp "$K3S_CNI_BIN_DIR/portmap" "$CNI_BIN_DIR/portmap"
elif sudo test -e "$K3S_CNI_BIN_DIR/portmap"; then
error "portmap exists at ${K3S_CNI_BIN_DIR}/portmap but is not executable."
error "Check filesystem and SELinux permissions on the k3s data dir."
exit 1
elif sudo test -e "$CNI_BIN_DIR/portmap"; then
error "portmap exists at ${CNI_BIN_DIR}/portmap but is not executable,"
error "and no fallback binary was found at ${K3S_CNI_BIN_DIR}/portmap."
error "Check file mode (expected 0755) on ${CNI_BIN_DIR}/portmap."
exit 1
else
error "portmap CNI plugin binary not found at ${CNI_BIN_DIR}/portmap or ${K3S_CNI_BIN_DIR}/portmap."
error "cni.chainingMode=portmap needs portmap at ${CNI_BIN_DIR}/ to handle hostPort —"
error "without it, pods with hostPort: stay in ContainerCreating indefinitely."
error "Install the standard CNI plugins (e.g. 'apt-get install -y containernetworking-plugins'"
error "and copy /usr/lib/cni/portmap to ${CNI_BIN_DIR}/portmap)."
exit 1
fi
fi
# Smoke-test the binary so wrong-arch or truncated copies fail here
# instead of as cryptic CNI ADD errors at first pod schedule. Modern
# portmap (e.g. v1.5.1 shipped by k3s and containernetworking-plugins)
# exits 0 with a "CNI portmap plugin vX.Y.Z" banner on stdout when
# invoked with no CNI_* env vars; older versions print a CNI-spec
# error to stderr. The 2>&1 merge means either path matches. A broken
# binary (wrong arch, truncated) fails with ENOEXEC/segfault and
# produces no "CNI" token on either stream.
if ! sudo "$CNI_BIN_DIR/portmap" </dev/null 2>&1 | grep -q CNI; then
error "portmap binary at ${CNI_BIN_DIR}/portmap failed smoke test."
error "Likely wrong architecture or a corrupted copy — remove it and re-run."
exit 1
fi
log "portmap CNI plugin binary OK at ${CNI_BIN_DIR}/portmap."

if [ "$SKIP_INSTALL" -eq 0 ]; then
# Detect arch
ARCH=$(uname -m)
Expand Down Expand Up @@ -123,11 +179,20 @@ if [ "$SKIP_INSTALL" -eq 0 ]; then
# can blackhole host connectivity entirely. The legacy/iptables datapath
# keeps Cilium's eBPF on cilium_* interfaces and pod veths only, and still
# provides full L3/L4 NetworkPolicy enforcement.
#
# cni.chainingMode=portmap chains the standard portmap CNI plugin after
# Cilium. With kubeProxyReplacement=false Cilium does not implement
# Kubernetes hostPort itself, and without portmap chaining hostPort
# mappings (e.g. orchestrator's 9849/9850 in the local overlay) are
# silently dropped — pods serve fine inside the cluster but the mapped
# ports never bind on the node, so Claude Code's MCP client at
# http://localhost:9850/mcp gets connection-refused.
CILIUM_INSTALL_ARGS=(
--version "$CILIUM_VERSION"
--set kubeProxyReplacement=false
--set bpf.masquerade=false
--set bpf.hostLegacyRouting=true
--set cni.chainingMode=portmap
)
log "Running 'cilium install ${CILIUM_INSTALL_ARGS[*]}'..."
"$CILIUM_BIN" install "${CILIUM_INSTALL_ARGS[@]}"
Expand All @@ -148,10 +213,12 @@ fi
# live cluster's config without re-installing.
log "Verifying cilium-config matches expected conservative datapath..."
verify_failed=0
chaining_mode_failed=0
for kv in \
'kube-proxy-replacement:false' \
'enable-bpf-masquerade:false' \
'enable-host-legacy-routing:true'; do
'enable-host-legacy-routing:true' \
'cni-chaining-mode:portmap'; do
key="${kv%%:*}"
want="${kv##*:}"
# kubectl jsonpath returns the value directly (empty string if the key
Expand All @@ -161,10 +228,19 @@ for kv in \
if [ "$got" != "$want" ]; then
error "cilium-config[$key] = '$got', expected '$want'"
verify_failed=1
if [ "$key" = "cni-chaining-mode" ]; then
chaining_mode_failed=1
fi
fi
done
if [ "$verify_failed" -ne 0 ]; then
error "The cilium-cli may have silently overridden a --set flag during install."
if [ "$chaining_mode_failed" -eq 1 ]; then
error "cni-chaining-mode mismatch suggests this is a Cilium install from before"
error "#2713. The supported remediation is 'make k3s-teardown && make k3s-setup'"
error "— the chainingMode value cannot be changed by editing cilium-config on a"
error "live cluster, as the agent only reads it at startup."
fi
error "Run 'kubectl -n kube-system get cm cilium-config -o yaml' to inspect."
exit 1
fi
Expand Down
Loading