Skip to content

Instantly share code, notes, and snippets.

@handlerww
Created July 22, 2021 04:48
Show Gist options
  • Save handlerww/206f6af42c8b1967d39113cfc38945da to your computer and use it in GitHub Desktop.
Save handlerww/206f6af42c8b1967d39113cfc38945da to your computer and use it in GitHub Desktop.
kind cross cluster
#!/bin/bash
set -eux
SUBNET_A="10.10.0.0/16"
SUBNET_B="10.20.0.0/16"
CONFIG_A=$(cat <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
podSubnet: ${SUBNET_A}
serviceSubnet: 10.96.0.0/12
kubeadmConfigPatches:
- |
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
networking:
dnsDomain: "cluster1.com"
EOF
)
CONFIG_B=$(cat <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
podSubnet: ${SUBNET_B}
serviceSubnet: 10.96.0.0/12
kubeadmConfigPatches:
- |
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
networking:
dnsDomain: "cluster2.com"
EOF
)
kind create cluster --name a --config <(echo "${CONFIG_A}") --wait 60s
kind create cluster --name b --config <(echo "${CONFIG_B}") --wait 60s
DOCKER_IP_A=$(docker inspect -f "{{ .NetworkSettings.Networks.kind.IPAddress }}" a-control-plane)
DOCKER_IP_B=$(docker inspect -f "{{ .NetworkSettings.Networks.kind.IPAddress }}" b-control-plane)
POD_CIDR_A=$(KUBECONFIG="$(kind export kubeconfig --name a)" kubectl get node -ojsonpath='{.items[0].spec.podCIDR}')
POD_CIDR_B=$(KUBECONFIG="$(kind export kubeconfig --name b)" kubectl get node -ojsonpath='{.items[0].spec.podCIDR}')
docker exec a-control-plane ip route add ${POD_CIDR_B} via ${DOCKER_IP_B}
docker exec b-control-plane ip route add ${POD_CIDR_A} via ${DOCKER_IP_A}
cat << EOF | kubectl apply --context=kind-a -f -
apiVersion: v1
kind: Service
metadata:
name: coredns-service
namespace: kube-system
spec:
type: NodePort
selector:
k8s-app: kube-dns
ports:
- name: dns
port: 53
protocol: UDP
targetPort: 53
- name: dns-tcp
port: 53
protocol: TCP
targetPort: 53
- name: metrics
port: 9153
protocol: TCP
targetPort: 9153
EOF
cat << EOF | kubectl apply --context=kind-b -f -
apiVersion: v1
kind: Service
metadata:
name: coredns-service
namespace: kube-system
spec:
type: NodePort
selector:
k8s-app: kube-dns
ports:
- name: dns
port: 53
protocol: UDP
targetPort: 53
- name: dns-tcp
port: 53
protocol: TCP
targetPort: 53
- name: metrics
port: 9153
protocol: TCP
targetPort: 9153
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment