Compartilhar via


Propagar recursos de um cluster de hub do Gerenciador de Frota de Kubernetes do Azure (Frota) para clusters membros

Este artigo fornece uma visão geral de como propagar recursos de um cluster de hub do Gerenciador de Frota de Kubernetes do Azure (Frota) para clusters membros.

Pré-requisitos

Caso você não tenha uma assinatura do Azure, crie uma conta gratuita do Azure antes de começar.

Usar a API ClusterResourcePlacement para propagar recursos para clusters membros

O objeto da API ClusterResourcePlacement é usado para propagar recursos de um cluster de hub para clusters membros. O objeto da API ClusterResourcePlacement especifica os recursos a serem propagados e a política de posicionamento a ser usada ao selecionar clusters membros. O objeto da API ClusterResourcePlacement é criado no cluster de hub e é usado para propagar recursos para clusters membros. Este exemplo demonstra como propagar um namespace para clusters membros usando o objeto da API ClusterResourcePlacement com uma política de posicionamento PickAll.

Para obter mais informações, consulte Propagação de recursos do Kubernetes do cluster de hub para os clusters membros e a documentação de código aberto da Frota.

  1. Criar um namespace para colocar nos clusters membros usando o comando kubectl create namespace. O exemplo a seguir cria um namespace chamado my-namespace:

    kubectl create namespace my-namespace
    
  2. Criar um objeto da API ClusterResourcePlacement no cluster de hub para propagar o namespace para os clusters membros e implantá-lo usando o comando kubectl apply -f. O exemplo a seguir ClusterResourcePlacement cria um objeto chamado crp e usa o namespace my-namespace com uma política de posicionamento PickAll para propagar o namespace para todos os clusters membros:

    kubectl apply -f - <<EOF
    apiVersion: placement.kubernetes-fleet.io/v1
    kind: ClusterResourcePlacement
    metadata:
      name: crp
    spec:
      resourceSelectors:
        - group: ""
          kind: Namespace
          version: v1          
          name: my-namespace
      policy:
        placementType: PickAll
    EOF
    
  3. Verifique o progresso da propagação de recursos usando o comando kubectl get clusterresourceplacement. O exemplo a seguir verifica o status do objeto ClusterResourcePlacement chamado crp:

    kubectl get clusterresourceplacement crp
    

    Seu resultado deve ser semelhante ao seguinte exemplo de saída:

    NAME   GEN   SCHEDULED   SCHEDULEDGEN   APPLIED   APPLIEDGEN   AGE
    crp    2     True        2              True      2            10s
    
  4. Exiba os detalhes do objeto crp usando o comando kubectl describe crp. O exemplo a seguir descreve o objeto ClusterResourcePlacement chamado crp:

    kubectl describe clusterresourceplacement crp
    

    Seu resultado deve ser semelhante ao seguinte exemplo de saída:

    Name:         crp
    Namespace:    
    Labels:       <none>
    Annotations:  <none>
    API Version:  placement.kubernetes-fleet.io/v1
    Kind:         ClusterResourcePlacement
    Metadata:
      Creation Timestamp:  2024-04-01T18:55:31Z
      Finalizers:
        kubernetes-fleet.io/crp-cleanup
        kubernetes-fleet.io/scheduler-cleanup
      Generation:        2
      Resource Version:  6949
      UID:               815b1d81-61ae-4fb1-a2b1-06794be3f986
    Spec:
      Policy:
        Placement Type:  PickAll
      Resource Selectors:
        Group:                 
        Kind:                  Namespace
        Name:                  my-namespace
        Version:               v1
      Revision History Limit:  10
      Strategy:
        Type:  RollingUpdate
    Status:
      Conditions:
        Last Transition Time:   2024-04-01T18:55:31Z
        Message:                found all the clusters needed as specified by the scheduling policy
        Observed Generation:    2
        Reason:                 SchedulingPolicyFulfilled
        Status:                 True
        Type:                   ClusterResourcePlacementScheduled
        Last Transition Time:   2024-04-01T18:55:36Z
        Message:                All 3 cluster(s) are synchronized to the latest resources on the hub cluster
        Observed Generation:    2
        Reason:                 SynchronizeSucceeded
        Status:                 True
        Type:                   ClusterResourcePlacementSynchronized
        Last Transition Time:   2024-04-01T18:55:36Z
        Message:                Successfully applied resources to 3 member clusters
        Observed Generation:    2
        Reason:                 ApplySucceeded
        Status:                 True
        Type:                   ClusterResourcePlacementApplied
      Observed Resource Index:  0
      Placement Statuses:
        Cluster Name:  membercluster1
        Conditions:
          Last Transition Time:  2024-04-01T18:55:31Z
          Message:               Successfully scheduled resources for placement in membercluster1 (affinity score: 0, topology spread score: 0): picked by scheduling policy
          Observed Generation:   2
          Reason:                ScheduleSucceeded
          Status:                True
          Type:                  ResourceScheduled
          Last Transition Time:  2024-04-01T18:55:36Z
          Message:               Successfully Synchronized work(s) for placement
          Observed Generation:   2
          Reason:                WorkSynchronizeSucceeded
          Status:                True
          Type:                  WorkSynchronized
          Last Transition Time:  2024-04-01T18:55:36Z
          Message:               Successfully applied resources
          Observed Generation:   2
          Reason:                ApplySucceeded
          Status:                True
          Type:                  ResourceApplied
        Cluster Name:            membercluster2
        Conditions:
          Last Transition Time:  2024-04-01T18:55:31Z
          Message:               Successfully scheduled resources for placement in membercluster2 (affinity score: 0, topology spread score: 0): picked by scheduling policy
          Observed Generation:   2
          Reason:                ScheduleSucceeded
          Status:                True
          Type:                  ResourceScheduled
          Last Transition Time:  2024-04-01T18:55:36Z
          Message:               Successfully Synchronized work(s) for placement
          Observed Generation:   2
          Reason:                WorkSynchronizeSucceeded
          Status:                True
          Type:                  WorkSynchronized
          Last Transition Time:  2024-04-01T18:55:36Z
          Message:               Successfully applied resources
          Observed Generation:   2
          Reason:                ApplySucceeded
          Status:                True
          Type:                  ResourceApplied
        Cluster Name:            membercluster3
        Conditions:
          Last Transition Time:  2024-04-01T18:55:31Z
          Message:               Successfully scheduled resources for placement in membercluster3 (affinity score: 0, topology spread score: 0): picked by scheduling policy
          Observed Generation:   2
          Reason:                ScheduleSucceeded
          Status:                True
          Type:                  ResourceScheduled
          Last Transition Time:  2024-04-01T18:55:36Z
          Message:               Successfully Synchronized work(s) for placement
          Observed Generation:   2
          Reason:                WorkSynchronizeSucceeded
          Status:                True
          Type:                  WorkSynchronized
          Last Transition Time:  2024-04-01T18:55:36Z
          Message:               Successfully applied resources
          Observed Generation:   2
          Reason:                ApplySucceeded
          Status:                True
          Type:                  ResourceApplied
      Selected Resources:
        Kind:     Namespace
        Name:     my-namespace
        Version:  v1
    Events:
      Type    Reason                     Age   From                                   Message
      ----    ------                     ----  ----                                   -------
      Normal  PlacementScheduleSuccess   108s  cluster-resource-placement-controller  Successfully scheduled the placement
      Normal  PlacementSyncSuccess       103s  cluster-resource-placement-controller  Successfully synchronized the placement
      Normal  PlacementRolloutCompleted  103s  cluster-resource-placement-controller  Resources have been applied to the selected clusters
    

Limpar os recursos

Se você não quiser mais usar o objeto ClusterResourcePlacement, poderá excluí-lo usando o comando kubectl delete. O exemplo a seguir exclui o objeto ClusterResourcePlacement chamado crp:

kubectl delete clusterresourceplacement crp

Próximas etapas

Para saber mais sobre a propagação de recursos, confira os seguintes recursos: