|
| 1 | +## Setup DNS for two clusters |
| 2 | + |
| 3 | +This example shows how to simply configure three k8s clusters to know each other. |
| 4 | +Can be skipped if clusters setupped with external DNS. |
| 5 | + |
| 6 | +## Run |
| 7 | + |
| 8 | +Expose dns service for first cluster |
| 9 | +```bash |
| 10 | +kubectl --kubeconfig=$KUBECONFIG1 expose service kube-dns -n kube-system --port=53 --target-port=53 --protocol=TCP --name=exposed-kube-dns --type=LoadBalancer |
| 11 | +``` |
| 12 | + |
| 13 | +Wait for assigning IP address (note: you should see IP address in logs. If you dont see repeat this): |
| 14 | +```bash |
| 15 | +kubectl --kubeconfig=$KUBECONFIG1 get services exposed-kube-dns -n kube-system -o go-template='{{index (index (index (index .status "loadBalancer") "ingress") 0) "ip"}}' |
| 16 | +ip1=$(kubectl --kubeconfig=$KUBECONFIG1 get services exposed-kube-dns -n kube-system -o go-template='{{index (index (index (index .status "loadBalancer") "ingress") 0) "ip"}}') |
| 17 | +if [[ $ip1 == *"no value"* ]]; then |
| 18 | + ip1=$(kubectl --kubeconfig=$KUBECONFIG1 get services exposed-kube-dns -n kube-system -o go-template='{{index (index (index (index .status "loadBalancer") "ingress") 0) "hostname"}}') |
| 19 | + ip1=$(dig +short $ip1 | head -1) |
| 20 | +fi |
| 21 | +echo Selected externalIP: $ip1 for cluster1 |
| 22 | +``` |
| 23 | + |
| 24 | +Expose dns service for the second cluster: |
| 25 | +```bash |
| 26 | +kubectl --kubeconfig=$KUBECONFIG2 expose service kube-dns -n kube-system --port=53 --target-port=53 --protocol=TCP --name=exposed-kube-dns --type=LoadBalancer |
| 27 | +``` |
| 28 | + |
| 29 | +Wait for assigning IP address (note: you should see IP address in logs. If you dont see repeat this): |
| 30 | +```bash |
| 31 | +kubectl --kubeconfig=$KUBECONFIG2 get services exposed-kube-dns -n kube-system -o go-template='{{index (index (index (index .status "loadBalancer") "ingress") 0) "ip"}}' |
| 32 | +ip2=$(kubectl --kubeconfig=$KUBECONFIG2 get services exposed-kube-dns -n kube-system -o go-template='{{index (index (index (index .status "loadBalancer") "ingress") 0) "ip"}}') |
| 33 | +if [[ $ip2 == *"no value"* ]]; then |
| 34 | + ip2=$(kubectl --kubeconfig=$KUBECONFIG2 get services exposed-kube-dns -n kube-system -o go-template='{{index (index (index (index .status "loadBalancer") "ingress") 0) "hostname"}}') |
| 35 | + ip2=$(dig +short $ip2 | head -1) |
| 36 | +fi |
| 37 | +echo Selected externalIP: $ip2 for cluster2 |
| 38 | +``` |
| 39 | + |
| 40 | +Add DNS forwarding from cluster1 to cluster2: |
| 41 | +```bash |
| 42 | +cat > configmap.yaml <<EOF |
| 43 | +apiVersion: v1 |
| 44 | +kind: ConfigMap |
| 45 | +metadata: |
| 46 | + name: coredns |
| 47 | + namespace: kube-system |
| 48 | +data: |
| 49 | + Corefile: | |
| 50 | + .:53 { |
| 51 | + errors |
| 52 | + health { |
| 53 | + lameduck 5s |
| 54 | + } |
| 55 | + ready |
| 56 | + kubernetes cluster.local in-addr.arpa ip6.arpa { |
| 57 | + pods insecure |
| 58 | + fallthrough in-addr.arpa ip6.arpa |
| 59 | + ttl 30 |
| 60 | + } |
| 61 | + k8s_external my.cluster1 |
| 62 | + prometheus :9153 |
| 63 | + forward . /etc/resolv.conf { |
| 64 | + max_concurrent 1000 |
| 65 | + } |
| 66 | + loop |
| 67 | + reload 5s |
| 68 | + } |
| 69 | + my.cluster2:53 { |
| 70 | + forward . ${ip2}:53 { |
| 71 | + force_tcp |
| 72 | + } |
| 73 | + } |
| 74 | +EOF |
| 75 | +kubectl --kubeconfig=$KUBECONFIG1 apply -f configmap.yaml |
| 76 | +cat > custom-configmap.yaml <<EOF |
| 77 | +apiVersion: v1 |
| 78 | +kind: ConfigMap |
| 79 | +metadata: |
| 80 | + name: coredns-custom |
| 81 | + namespace: kube-system |
| 82 | +data: |
| 83 | + server.override: | |
| 84 | + k8s_external my.cluster2 |
| 85 | + proxy1.server: | |
| 86 | + my.cluster2:53 { |
| 87 | + forward . ${ip2}:53 { |
| 88 | + force_tcp |
| 89 | + } |
| 90 | + } |
| 91 | +EOF |
| 92 | + |
| 93 | +kubectl --kubeconfig=$KUBECONFIG1 apply -f custom-configmap.yaml |
| 94 | +``` |
| 95 | + |
| 96 | +Add DNS forwarding from cluster2 to cluster1: |
| 97 | +```bash |
| 98 | +cat > configmap.yaml <<EOF |
| 99 | +apiVersion: v1 |
| 100 | +kind: ConfigMap |
| 101 | +metadata: |
| 102 | + name: coredns |
| 103 | + namespace: kube-system |
| 104 | +data: |
| 105 | + Corefile: | |
| 106 | + .:53 { |
| 107 | + errors |
| 108 | + health { |
| 109 | + lameduck 5s |
| 110 | + } |
| 111 | + ready |
| 112 | + kubernetes cluster.local in-addr.arpa ip6.arpa { |
| 113 | + pods insecure |
| 114 | + fallthrough in-addr.arpa ip6.arpa |
| 115 | + ttl 30 |
| 116 | + } |
| 117 | + k8s_external my.cluster2 |
| 118 | + prometheus :9153 |
| 119 | + forward . /etc/resolv.conf { |
| 120 | + max_concurrent 1000 |
| 121 | + } |
| 122 | + loop |
| 123 | + reload 5s |
| 124 | + } |
| 125 | + my.cluster1:53 { |
| 126 | + forward . ${ip1}:53 { |
| 127 | + force_tcp |
| 128 | + } |
| 129 | + } |
| 130 | +EOF |
| 131 | +kubectl --kubeconfig=$KUBECONFIG2 apply -f configmap.yaml |
| 132 | +cat > custom-configmap.yaml <<EOF |
| 133 | +apiVersion: v1 |
| 134 | +kind: ConfigMap |
| 135 | +metadata: |
| 136 | + name: coredns-custom |
| 137 | + namespace: kube-system |
| 138 | +data: |
| 139 | + server.override: | |
| 140 | + k8s_external my.cluster1 |
| 141 | + proxy1.server: | |
| 142 | + my.cluster1:53 { |
| 143 | + forward . ${ip1}:53 { |
| 144 | + force_tcp |
| 145 | + } |
| 146 | + } |
| 147 | +EOF |
| 148 | +kubectl --kubeconfig=$KUBECONFIG2 apply -f custom-configmap.yaml |
| 149 | +``` |
| 150 | + |
| 151 | +## Cleanup |
| 152 | + |
| 153 | +```bash |
| 154 | +kubectl --kubeconfig=$KUBECONFIG1 delete service -n kube-system exposed-kube-dns |
| 155 | +kubectl --kubeconfig=$KUBECONFIG2 delete service -n kube-system exposed-kube-dns |
| 156 | +``` |
| 157 | + |
0 commit comments