Skip to content

Instantly share code, notes, and snippets.

@seguidor777
Last active September 10, 2024 09:32
Show Gist options
  • Save seguidor777/5cda274ea9e1083bfb9b989d17c241e8 to your computer and use it in GitHub Desktop.
Save seguidor777/5cda274ea9e1083bfb9b989d17c241e8 to your computer and use it in GitHub Desktop.
This script assign static IP to all nodes of a kind cluster
#!/bin/bash
set -e
# Workaround for https://github.com/kubernetes-sigs/kind/issues/2045
all_nodes=$(kind get nodes --name "${CLUSTER_NAME}" | tr "\n" " ")
declare -A nodes_table
ip_template="{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}"
echo "Saving original IPs from nodes"
for node in ${all_nodes}; do
nodes_table["${node}"]=$(docker inspect -f "${ip_template}" "${node}")
echo "${node}: ${nodes_table["${node}"]}"
done
echo "Stopping all nodes and registry"
docker stop ${all_nodes} "${reg_name}" >/dev/null
echo "Re-creating network with user defined subnet"
subnet=$(docker network inspect -f "{{(index .IPAM.Config 0).Subnet}}" "kind")
echo "Subnet: ${subnet}"
gateway=$(docker network inspect -f "{{(index .IPAM.Config 0).Gateway}}" "kind")
echo "Gateway: ${gateway}"
docker network rm "kind" >/dev/null
docker network create --driver bridge --subnet ${subnet} --gateway ${gateway} "kind" >/dev/null
echo "Assigning static IPs to nodes"
for node in "${!nodes_table[@]}"; do
docker network connect --ip ${nodes_table["${node}"]} "kind" "${node}"
echo "Assigning IP ${nodes_table["${node}"]} to node ${node}"
done
echo "Starting all nodes and registry"
docker start ${all_nodes} "${reg_name}" >/dev/null
echo -n "Wait until all nodes are ready "
while :; do
[[ $(kubectl get nodes | grep Ready | wc -l) -eq ${#nodes_table[@]} ]] && break
echo -n "."
sleep 5
done
echo
@huataihuang
Copy link

Thank you, this solution is awesome, save my time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment