|
| 1 | +<!-- BEGIN MUNGE: UNVERSIONED_WARNING --> |
| 2 | + |
| 3 | +<!-- BEGIN STRIP_FOR_RELEASE --> |
| 4 | + |
| 5 | +<img src="http://kubernetes.io/img/warning.png" alt="WARNING" |
| 6 | + width="25" height="25"> |
| 7 | +<img src="http://kubernetes.io/img/warning.png" alt="WARNING" |
| 8 | + width="25" height="25"> |
| 9 | +<img src="http://kubernetes.io/img/warning.png" alt="WARNING" |
| 10 | + width="25" height="25"> |
| 11 | +<img src="http://kubernetes.io/img/warning.png" alt="WARNING" |
| 12 | + width="25" height="25"> |
| 13 | +<img src="http://kubernetes.io/img/warning.png" alt="WARNING" |
| 14 | + width="25" height="25"> |
| 15 | + |
| 16 | +<h2>PLEASE NOTE: This document applies to the HEAD of the source tree</h2> |
| 17 | + |
| 18 | +If you are using a released version of Kubernetes, you should |
| 19 | +refer to the docs that go with that version. |
| 20 | + |
| 21 | +<strong> |
| 22 | +The latest 1.0.x release of this document can be found |
| 23 | +[here](http://releases.k8s.io/release-1.0/examples/sharing-clusters/README.md). |
| 24 | + |
| 25 | +Documentation for other releases can be found at |
| 26 | +[releases.k8s.io](http://releases.k8s.io). |
| 27 | +</strong> |
| 28 | +-- |
| 29 | + |
| 30 | +<!-- END STRIP_FOR_RELEASE --> |
| 31 | + |
| 32 | +<!-- END MUNGE: UNVERSIONED_WARNING --> |
| 33 | + |
| 34 | +# Sharing Clusters |
| 35 | + |
| 36 | +This example demonstrates how to access one kubernetes cluster from another. It only works if both clusters are running on the same network, on a cloud provider that provides a private ip range per network (eg: GCE, GKE, AWS). |
| 37 | + |
| 38 | +## Setup |
| 39 | + |
| 40 | +Create a cluster in US (you don't need to do this if you already have a running kubernetes cluster) |
| 41 | + |
| 42 | +```shell |
| 43 | +$ cluster/kube-up.sh |
| 44 | +``` |
| 45 | + |
| 46 | +Before creating our second cluster, lets have a look at the kubectl config: |
| 47 | + |
| 48 | +```yaml |
| 49 | +apiVersion: v1 |
| 50 | +clusters: |
| 51 | +- cluster: |
| 52 | + certificate-authority-data: REDACTED |
| 53 | + server: https://104.197.84.16 |
| 54 | + name: <clustername_us> |
| 55 | +... |
| 56 | +current-context: <clustername_us> |
| 57 | +... |
| 58 | +``` |
| 59 | + |
| 60 | +Now spin up the second cluster in Europe |
| 61 | + |
| 62 | +```shell |
| 63 | +$ ./cluster/kube-up.sh |
| 64 | +$ KUBE_GCE_ZONE=europe-west1-b KUBE_GCE_INSTANCE_PREFIX=eu ./cluster/kube-up.sh |
| 65 | +``` |
| 66 | + |
| 67 | +Your kubectl config should contain both clusters: |
| 68 | + |
| 69 | +```yaml |
| 70 | +apiVersion: v1 |
| 71 | +clusters: |
| 72 | +- cluster: |
| 73 | + certificate-authority-data: REDACTED |
| 74 | + server: https://146.148.25.221 |
| 75 | + name: <clustername_eu> |
| 76 | +- cluster: |
| 77 | + certificate-authority-data: REDACTED |
| 78 | + server: https://104.197.84.16 |
| 79 | + name: <clustername_us> |
| 80 | +... |
| 81 | +current-context: kubernetesdev_eu |
| 82 | +... |
| 83 | +``` |
| 84 | + |
| 85 | +And kubectl get nodes should agree: |
| 86 | + |
| 87 | +``` |
| 88 | +$ kubectl get nodes |
| 89 | +NAME LABELS STATUS |
| 90 | +eu-minion-0n61 kubernetes.io/hostname=eu-minion-0n61 Ready |
| 91 | +eu-minion-79ua kubernetes.io/hostname=eu-minion-79ua Ready |
| 92 | +eu-minion-7wz7 kubernetes.io/hostname=eu-minion-7wz7 Ready |
| 93 | +eu-minion-loh2 kubernetes.io/hostname=eu-minion-loh2 Ready |
| 94 | +
|
| 95 | +$ kubectl config use-context <clustername_us> |
| 96 | +$ kubectl get nodes |
| 97 | +NAME LABELS STATUS |
| 98 | +kubernetes-minion-5jtd kubernetes.io/hostname=kubernetes-minion-5jtd Ready |
| 99 | +kubernetes-minion-lqfc kubernetes.io/hostname=kubernetes-minion-lqfc Ready |
| 100 | +kubernetes-minion-sjra kubernetes.io/hostname=kubernetes-minion-sjra Ready |
| 101 | +kubernetes-minion-wul8 kubernetes.io/hostname=kubernetes-minion-wul8 Ready |
| 102 | +``` |
| 103 | + |
| 104 | +## Testing reachability |
| 105 | + |
| 106 | +For this test to work we'll need to create a service in europe: |
| 107 | + |
| 108 | +``` |
| 109 | +$ kubectl config use-context <clustername_eu> |
| 110 | +$ kubectl create -f /tmp/secret.json |
| 111 | +$ kubectl create -f examples/https-nginx/nginx-app.yaml |
| 112 | +$ kubectl exec -it my-nginx-luiln -- echo "Europe nginx" >> /usr/share/nginx/html/index.html |
| 113 | +$ kubectl get ep |
| 114 | +NAME ENDPOINTS |
| 115 | +kubernetes 10.240.249.92:443 |
| 116 | +nginxsvc 10.244.0.4:80,10.244.0.4:443 |
| 117 | +``` |
| 118 | + |
| 119 | +Just to test reachability, we'll try hitting the Europe nginx from our initial US central cluster. Create a basic curl pod in the US cluster: |
| 120 | + |
| 121 | +```yaml |
| 122 | +apiVersion: v1 |
| 123 | +kind: Pod |
| 124 | +metadata: |
| 125 | + name: curlpod |
| 126 | +spec: |
| 127 | + containers: |
| 128 | + - image: radial/busyboxplus:curl |
| 129 | + command: |
| 130 | + - sleep |
| 131 | + - "360000000" |
| 132 | + imagePullPolicy: IfNotPresent |
| 133 | + name: curlcontainer |
| 134 | + restartPolicy: Always |
| 135 | +``` |
| 136 | +
|
| 137 | +And test that you can actually reach the test nginx service across continents |
| 138 | +
|
| 139 | +``` |
| 140 | +$ kubectl config use-context <clustername_us> |
| 141 | +$ kubectl -it exec curlpod -- /bin/sh |
| 142 | +[ root@curlpod:/ ]$ curl http://10.244.0.4:80 |
| 143 | +Europe nginx |
| 144 | +``` |
| 145 | + |
| 146 | +## Granting access to the remote cluster |
| 147 | + |
| 148 | +We will grant the US cluster access to the Europe cluster. Basically we're going to setup a secret that allows kubectl to function in a pod running in the US cluster, just like it did on our local machine in the previous step. First create a secret with the contents of the current .kube/config: |
| 149 | + |
| 150 | +```shell |
| 151 | +$ kubectl config use-context <clustername_eu> |
| 152 | +$ go run ./make_secret.go --kubeconfig=$HOME/.kube/config > /tmp/secret.json |
| 153 | +$ kubectl config use-context <clustername_us> |
| 154 | +$ kubectl create -f /tmp/secret.json |
| 155 | +``` |
| 156 | + |
| 157 | +Create a kubectl pod that uses the secret, in the US cluster. |
| 158 | + |
| 159 | +```json |
| 160 | +{ |
| 161 | + "kind": "Pod", |
| 162 | + "apiVersion": "v1", |
| 163 | + "metadata": { |
| 164 | + "name": "kubectl-tester" |
| 165 | + }, |
| 166 | + "spec": { |
| 167 | + "volumes": [ |
| 168 | + { |
| 169 | + "name": "secret-volume", |
| 170 | + "secret": { |
| 171 | + "secretName": "kubeconfig" |
| 172 | + } |
| 173 | + } |
| 174 | + ], |
| 175 | + "containers": [ |
| 176 | + { |
| 177 | + "name": "kubectl", |
| 178 | + "image": "bprashanth/kubectl:0.0", |
| 179 | + "imagePullPolicy": "Always", |
| 180 | + "env": [ |
| 181 | + { |
| 182 | + "name": "KUBECONFIG", |
| 183 | + "value": "/.kube/config" |
| 184 | + } |
| 185 | + ], |
| 186 | + "args": [ |
| 187 | + "proxy", "-p", "8001" |
| 188 | + ], |
| 189 | + "volumeMounts": [ |
| 190 | + { |
| 191 | + "name": "secret-volume", |
| 192 | + "mountPath": "/.kube" |
| 193 | + } |
| 194 | + ] |
| 195 | + } |
| 196 | + ] |
| 197 | + } |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +And check that you can access the remote cluster |
| 202 | + |
| 203 | +```shell |
| 204 | +$ kubectl config use-context <clustername_us> |
| 205 | +$ kubectl exec -it kubectl-tester bash |
| 206 | + |
| 207 | +kubectl-tester $ kubectl get nodes |
| 208 | +NAME LABELS STATUS |
| 209 | +eu-minion-0n61 kubernetes.io/hostname=eu-minion-0n61 Ready |
| 210 | +eu-minion-79ua kubernetes.io/hostname=eu-minion-79ua Ready |
| 211 | +eu-minion-7wz7 kubernetes.io/hostname=eu-minion-7wz7 Ready |
| 212 | +eu-minion-loh2 kubernetes.io/hostname=eu-minion-loh2 Ready |
| 213 | +``` |
| 214 | + |
| 215 | +For a more advanced example of sharing clusters, see the [service-loadbalancer](../../contrib/service-loadbalancer/README.md) |
| 216 | + |
| 217 | + |
| 218 | +<!-- BEGIN MUNGE: GENERATED_ANALYTICS --> |
| 219 | +[]() |
| 220 | +<!-- END MUNGE: GENERATED_ANALYTICS --> |
0 commit comments