本文介绍如何创建联邦应用。

前提条件

操作步骤

  1. 创建名为federated-namespace.yaml文件并拷贝以下内容至这个文件中。
    apiVersion: v1
    kind: Namespace
    metadata:
      name: test-namespace
    ---
    apiVersion: types.kubefed.io/v1beta1
    kind: FederatedNamespace
    metadata:
      name: test-namespace
      namespace: test-namespace
    spec:
      placement:
        clusters:
            - name: cluster1
            - name: cluster2
  2. 在主集群cluster1上通过CloudShell执行命令kubectl apply -f federated-namespace.yaml,创建FederatedNamespace。有关主集群cluster1的创建信息,请参见部署联邦集群
  3. 创建名为federated-deployment.yaml文件并拷贝以下内容至文件中。
    apiVersion: types.kubefed.io/v1beta1
    kind: FederatedDeployment
    metadata:
      name: test-deployment
      namespace: test-namespace
    spec:
      template:
        metadata:
          labels:
            app: nginx
        spec:
          replicas: 2
          selector:
            matchLabels:
              app: nginx
          template:
            metadata:
              labels:
                app: nginx
            spec:
              containers:
              - image: nginx
                name: nginx
                resources:
                  limits:
                    cpu: 500m
                  requests:
                    cpu: 200m
      placement:
        clusters:
            - name: cluster1
            - name: cluster2
  4. 在主集群cluster1上通过CloudShell执行命令kubectl apply -f federated-deployment.yaml,部署FederatedDeployment。

执行结果

查看联邦集群中各个Deployment详情。
执行以下命令,查看cluster1的Deployment详情:
kubectl get deployment -n test-namespace --context cluster1
返回结果如下:
NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
test-deployment   2         2         2            2           71s
执行以下命令,查看cluster2的Deployment详情:
kubectl get deployment -n test-namespace --context cluster2
返回结果如下:
NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
test-deployment   2         2         2            2           77s