Prerequisites

Procedure

  1. Create the federated-namespace.yaml file and copy the following code to the file. Then, run the kubectl apply -f federated-namespace.yaml command in the host cluster cluster1 to create a federated namespace.
    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. Create the federated-deployment.yaml file and copy the following code to the file. Then, run the kubectl apply -f federated-deployment.yaml command in the host cluster cluster1 to create a federated deployment.
    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. Run the corresponding commands to check the status of the deployments in the cluster federation.
    If the following results are returned, the application is deployed in the cluster federation properly:
    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