# 安装federation-v1

## 准备三个集群

准备三个kubernetes集群，它们的用处分别为

* `f-cluster`：用来部署kubefed的组件, 192.168.1.103
* `a-cluster`：联邦下的一个集群 192.168.1.101
* `b-cluster`：联邦下的一个集群 192.168.1.102

## 安装kubectl

安装好kubectl并设置好kubeconfig，使用其能连上三个kubernetes集群，kubeconfig文件的内容应该如下：

```
apiVersion: v1
kind: Config
preferences: {}

clusters:
  - name: f-cluster
    cluster:
      server: https://192.168.1.103:6443
      certificate-authority-data: xxxxxxxxxxx
  - name: a-cluster
    cluster:
      server: https://192.168.1.101:6443
      certificate-authority-data: xxxxxxxxxxx
  - name: b-cluster
    cluster:
      server: https://192.168.1.102:6443
      certificate-authority-data: xxxxxxxxxxx

users:
  - name: f-admin
    user: 
      client-key-data: xxxxxxxxxxx
      client-certificate-data: xxxxxxxxxxxxx
  - name: a-admin
    user: 
      client-key-data: xxxxxxxxxxx
      client-certificate-data: xxxxxxxxxxxxx
  - name: b-admin
    user: 
      client-key-data: xxxxxxxxxxx
      client-certificate-data: xxxxxxxxxxxxx

contexts:
  - name: f-context
    context:
      cluster: f-cluster
      user: f-admin
  - name: a-context
    context:
      cluster: a-cluster
      user: a-admin
  - name: b-context
    context:
      cluster: b-cluster
      user: b-admin

current-context: f-context
```

执行命令`kubectl config get-clusters`查看集群信息

```
$ kubectl config getc-clusters
NAME
b-cluster
f-cluster
a-cluster
```

执行`kubectl config get-contexts`查看上下文信息

```
$ kubectl config get-contexts
CURRENT   NAME        CLUSTER     AUTHINFO   NAMESPACE
          a-context   a-cluster   a-admin    
          b-context   b-cluster   b-admin    
*         f-context   f-cluster   f-admin
```

## 部署coredns-provider

```
### deperecated
coredns将数据存储在etcd中，所以我们先部署etcd集群，这里使用helm来部署：

$ helm install --namespace federation --name etcd-operator stable/etcd-operator
$ helm upgrade --namespace federation --set cluster.enabled=true etcd-operator stable/etcd-operator


部署成功后，便可以在集群中通过`http://etcd-cluster.federation:2379`来访问该etcd服务
```

coredns将数据存储在etcd中，所以我们先部署etcd集群，这里我们额外先部署好，假设coredns的etcd的URL为`http://192.168.1.103:3379`：

接下来，我们也用helm来部署coredns服务：

```
$ helm install --namespace federation --name coredns -f Values.yaml stable/coredns
```

`Values.yaml`文件的内容如下：

```
isClusterService: false
serviceType: "NodePort"
plugins:
  kubernetes:
    enabled: false
  etcd:
    enabled: true
    zones:
    - "example.com."
    endpoint: "http://192.168.1.103:3379"
```

检查coredns与etcd的pod有正常运行

## 获取kubefed

从github上下载kubefed的压缩包（`https://github.com/kubernetes-retired/federation/releases`），比如我们下载的是`release_v1.9.0-alpha.3_federation-client-linux-amd64.tar`然后解压

```
$ tar -xzvf release_v1.9.0-alpha.3_federation-client-linux-amd64.tar
$ sudo cp federation/client/bin/kubefed /usr/local/bin
$ sudo chmod +x /usr/local/bin/kubefed
```

## 部署kubefed的control plane

control-plane的apiserver需要使用etcd存储数据，我们先提前安装好一个etcd集群供它使用，假设etcd服务的地址为`http://192.168.1.103:4379`

接下来，你们执行以下命令来创建一个名字叫federation的集群联邦

```
$ kubefed init federation \
  --host-cluster-context=f-context \
  --dns-provider="coredns" \
  --dns-zone-name="example.com." \
  --dns-provider-config="coredns-provider.conf" \
  --api-server-service-type="NodePort" \
  --etcd-servers="http://192.168.1.103:4379"
```

其中`coredns-provider.conf`的内容如下：

```
[Global]
etcd-endpoints = http://192.168.1.103:3379
zones = example.com.
```

## 添加集群

执行以下命令添加两个集群到联邦中

```
$ kubefed join a-cluster --cluster-context=a-context --host-cluster-context=f-context

$ kubefed join b-cluster --cluster-context=b-context --host-cluster-context=f-context
```

然后执行以下命令查看联邦中的集群

```
$ kubectl get cluster
```

## 发布Deployment

通过联邦apiserver发布一个Deployment

```
apiVersion: v1
kind: Deployment
metadata: 
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
```

```
$ kubectl create -f nginx-deployment.ymal
```

查看a集群中的pod数目

```
$ kubectl get pod --context=a-context
```

查看b集群中的pod数目（在b集群上执行）

```
$ kubectl get pod --context=b-context
```

## 跨集群服务发现

to be continued

## 跨集群负载均衡

to be continued

## FAQ

Q: 集群之间的网络怎么打通？

## Reference

* [https://www.kubernetes.org.cn/2987.html](https://www.kubernetes.org.cn/2987.html)
* [https://yq.aliyun.com/articles/401059](https://yq.aliyun.com/articles/401059)
* [https://www.yangcs.net/posts/federation/](https://www.yangcs.net/posts/federation/)
