sigs.k8s.io/cluster-api-provider-azure@v1.14.3/docs/book/src/topics/machinepools.md (about)

     1  # MachinePools
     2  - **Feature status:** Experimental
     3  - **Feature gate:** MachinePool=true
     4  
     5  > In Cluster API (CAPI) v1alpha2, users can create MachineDeployment, MachineSet or Machine custom
     6  > resources. When you create a MachineDeployment or MachineSet, Cluster API components react and
     7  > eventually Machine resources are created. Cluster API's current architecture mandates that a
     8  > Machine maps to a single machine (virtual or bare metal) with the provider being responsible for
     9  > the management of the underlying machine's infrastructure.
    10  
    11  > Nearly all infrastructure providers have a way for their users to manage a group of machines
    12  > (virtual or bare metal) as a single entity. Each infrastructure provider offers their own unique
    13  > features, but nearly all are concerned with managing availability, health, and configuration updates.
    14  
    15  > A MachinePool is similar to a MachineDeployment in that they both define
    16  > configuration and policy for how a set of machines are managed. They Both define a common
    17  > configuration, number of desired machine replicas, and policy for update. Both types also combine
    18  > information from Kubernetes as well as the underlying provider infrastructure to give a view of
    19  > the overall health of the machines in the set.
    20  
    21  > MachinePool diverges from MachineDeployment in that the MachineDeployment controller uses
    22  > MachineSets to achieve the aforementioned desired number of machines and to orchestrate updates
    23  > to the Machines in the managed set, while MachinePool delegates the responsibility of these
    24  > concerns to an infrastructure provider specific resource such as AWS Auto Scale Groups, GCP
    25  > Managed Instance Groups, and Azure Virtual Machine Scale Sets.
    26  
    27  > MachinePool is optional and doesn't replace the need for MachineSet/Machine since not every
    28  > infrastructure provider will have an abstraction for managing multiple machines (i.e. bare metal).
    29  > Users may always opt to choose MachineSet/Machine when they don't see additional value in
    30  > MachinePool for their use case.
    31  
    32  *Source: [MachinePool API Proposal](https://github.com/kubernetes-sigs/cluster-api/blob/bf51a2502f9007b531f6a9a2c1a4eae1586fb8ca/docs/proposals/20190919-machinepool-api.md)*
    33  
    34  ## AzureMachinePool
    35  Cluster API Provider Azure (CAPZ) has experimental support for `MachinePool` through the infrastructure
    36  types `AzureMachinePool` and `AzureMachinePoolMachine`. An `AzureMachinePool` corresponds to a
    37  [Virtual Machine Scale Set](https://learn.microsoft.com/azure/virtual-machine-scale-sets/overview) (VMSS),
    38  which provides the cloud provider-specific resource for orchestrating a group of Virtual Machines. The
    39  `AzureMachinePoolMachine` corresponds to a virtual machine instance within the VMSS.
    40  
    41  ### Orchestration Modes
    42  
    43  Azure Virtual Machine Scale Sets support two orchestration modes: `Uniform` and `Flexible`. CAPZ defaults to `Uniform` mode. See [VMSS Orchestration modes in Azure](https://learn.microsoft.com/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-orchestration-modes) for more information.
    44  
    45  To use `Flexible` mode requires Kubernetes v1.26.0 or later. Ensure that `orchestrationMode` on the `AzureMachinePool` spec is set:
    46  
    47  ```yaml
    48  apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    49  kind: AzureMachinePool
    50  metadata:
    51    name: capz-mp-0
    52  spec:
    53    orchestrationMode: Flexible
    54  ```
    55  
    56  Then, after applying the template to start provisioning, install the [cloud-provider-azure Helm chart](https://github.com/kubernetes-sigs/cloud-provider-azure/tree/master/helm/cloud-provider-azure#readme) to the workload cluster.
    57  
    58  ### Safe Rolling Upgrades and Delete Policy
    59  `AzureMachinePools` provides the ability to safely deploy new versions of Kubernetes, or more generally, changes to the
    60  Virtual Machine Scale Set model, e.g., updating the OS image run by the virtual machines in the scale set. For example,
    61  if a cluster operator wanted to change the Kubernetes version of the `MachinePool`, they would update the `Version`
    62  field on the `MachinePool`, then `AzureMachinePool` would respond by rolling out the new OS image for the specified
    63  Kubernetes version to each of the virtual machines in the scale set progressively cordon, draining, then replacing the
    64  machine. This enables `AzureMachinePools` to upgrade the underlying pool of virtual machines with minimal interruption 
    65  to the workloads running on them.
    66  
    67  `AzureMachinePools` also provides the ability to specify the order of virtual machine deletion.
    68  
    69  #### Describing the Deployment Strategy
    70  Below we see a partially described `AzureMachinePool`. The `strategy` field describes the 
    71  `AzureMachinePoolDeploymentStrategy`. At the time of writing this, there is only one strategy type, `RollingUpdate`, 
    72  which provides the ability to specify delete policy, max surge, and max unavailable.
    73  
    74  - **deletePolicy:** provides three options for order of deletion `Oldest`, `Newest`, and `Random`
    75  - **maxSurge:** provides the ability to specify how many machines can be added in addition to the current replica count
    76    during an upgrade operation. This can be a percentage, or a fixed number.
    77  - **maxUnavailable:** provides the ability to specify how many machines can be unavailable at any time. This can be a 
    78    percentage, or a fixed number.
    79  
    80  ```yaml
    81  apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    82  kind: AzureMachinePool
    83  metadata:
    84    name: capz-mp-0
    85  spec:
    86    strategy:
    87      rollingUpdate:
    88        deletePolicy: Oldest
    89        maxSurge: 25%
    90        maxUnavailable: 1
    91      type: RollingUpdate
    92  ```
    93  
    94  ### AzureMachinePoolMachines
    95  `AzureMachinePoolMachine` represents a virtual machine in the scale set. `AzureMachinePoolMachines` are created by the
    96  `AzureMachinePool` controller and are used to track the life cycle of a virtual machine in the scale set. When a 
    97  `AzureMachinePool` is created, each virtual machine instance will be represented as a `AzureMachinePoolMachine`
    98  resource. A cluster operator can delete the `AzureMachinePoolMachine` resource if they would like to delete a specific
    99  virtual machine from the scale set. This is useful if one would like to manually control upgrades and rollouts through
   100  CAPZ.
   101  
   102  ### Using `clusterctl` to deploy
   103  To deploy a MachinePool / AzureMachinePool via `clusterctl generate` there's a [flavor](https://cluster-api.sigs.k8s.io/clusterctl/commands/generate-cluster.html#flavors)
   104  for that.
   105  
   106  Make sure to set up your Azure environment as described [here](https://cluster-api.sigs.k8s.io/user/quick-start.html).
   107  
   108  ```shell
   109  clusterctl generate cluster my-cluster --kubernetes-version v1.22.0 --flavor machinepool > my-cluster.yaml
   110  ```
   111  
   112  The template used for this [flavor](https://cluster-api.sigs.k8s.io/clusterctl/commands/generate-cluster.html#flavors)
   113  is located [here](https://raw.githubusercontent.com/kubernetes-sigs/cluster-api-provider-azure/main/templates/cluster-template-machinepool.yaml).
   114  
   115  ### Example MachinePool, AzureMachinePool and KubeadmConfig Resources
   116  Below is an example of the resources needed to create a pool of Virtual Machines orchestrated with
   117  a Virtual Machine Scale Set.
   118  ```yaml
   119  ---
   120  apiVersion: cluster.x-k8s.io/v1beta1
   121  kind: MachinePool
   122  metadata:
   123    name: capz-mp-0
   124  spec:
   125    clusterName: capz
   126    replicas: 2
   127    template:
   128      spec:
   129        bootstrap:
   130          configRef:
   131            apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
   132            kind: KubeadmConfig
   133            name: capz-mp-0
   134        clusterName: capz
   135        infrastructureRef:
   136          apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
   137          kind: AzureMachinePool
   138          name: capz-mp-0
   139        version: v1.22.0
   140  ---
   141  apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
   142  kind: AzureMachinePool
   143  metadata:
   144    name: capz-mp-0
   145  spec:
   146    location: westus2
   147    strategy:
   148      rollingUpdate:
   149        deletePolicy: Oldest
   150        maxSurge: 25%
   151        maxUnavailable: 1
   152      type: RollingUpdate
   153    template:
   154      osDisk:
   155        diskSizeGB: 30
   156        managedDisk:
   157          storageAccountType: Premium_LRS
   158        osType: Linux
   159      sshPublicKey: ${YOUR_SSH_PUB_KEY}
   160      vmSize: Standard_D2s_v3
   161  ---
   162  apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
   163  kind: KubeadmConfig
   164  metadata:
   165    name: capz-mp-0
   166  spec:
   167    files:
   168    - content: |
   169        {
   170          "cloud": "AzurePublicCloud",
   171          "tenantId": "tenantID",
   172          "subscriptionId": "subscriptionID",
   173          "aadClientId": "clientID",
   174          "aadClientSecret": "secret",
   175          "resourceGroup": "capz",
   176          "securityGroupName": "capz-node-nsg",
   177          "location": "westus2",
   178          "vmType": "vmss",
   179          "vnetName": "capz-vnet",
   180          "vnetResourceGroup": "capz",
   181          "subnetName": "capz-node-subnet",
   182          "routeTableName": "capz-node-routetable",
   183          "loadBalancerSku": "Standard",
   184          "maximumLoadBalancerRuleCount": 250,
   185          "useManagedIdentityExtension": false,
   186          "useInstanceMetadata": true
   187        }
   188      owner: root:root
   189      path: /etc/kubernetes/azure.json
   190      permissions: "0644"
   191    joinConfiguration:
   192      nodeRegistration:
   193        name: '{{ ds.meta_data["local_hostname"] }}'
   194  ```