Skip to content

Instantly share code, notes, and snippets.

@lixianyang
Forked from seguidor777/kind_static_ips.sh
Created February 22, 2022 14:49
Show Gist options
  • Save lixianyang/b0460127e019e111537080da69977d61 to your computer and use it in GitHub Desktop.
Save lixianyang/b0460127e019e111537080da69977d61 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
@lixianyang
Copy link
Author

Yes, you are right.

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