前提条件

操作步骤

  1. 创建并拷贝以下内容到federated-namespace.yaml文件中,并在主集群上cluster1执行kubectl apply -f federated-namespace.yaml命令,创建FederatedNamespace。
    apiVersion: v1
    kind: Namespace
    metadata:
      name: test-namespace
    ---
    apiVersion: types.federation.k8s.io/v1alpha1
    kind: FederatedNamespace
    metadata:
      name: test-namespace
      namespace: test-namespace
    spec:
      placement:
        clusterNames:
        - cluster1
        - cluster2
  2. 创建并拷贝以下内容到federated-deployment.yaml文件中,并在主集群上cluster1执行kubectl apply -f federated-deployment.yaml命令,部署FederatedDeployment。
    apiVersion: types.federation.k8s.io/v1alpha1
    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:
        clusterNames:
        - cluster1
        - cluster2
  3. 执行如下命令,查看联邦集群中各个Deployment详情。
    当看到如下信息时,表示应用部署完成。
    kubectl get deployment -n test-namespace --context cluster1
    NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    test-deployment   2         2         2            2           71s
    kubectl get deployment -n test-namespace --context cluster2
    NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    test-deployment   2         2         2            2           77s