Deploying to a VMSS Fleet: Deploy-time Instance Discovery and Image Strategy
Overview
Section titled âOverviewâDeploying against a fixed list of servers, visiting each one in order, works fine when you operate a handful of VMs directly. Once you move to VMSS (Virtual Machine Scale Set), that premise breaks. Scale-out keeps creating new instances, and scale-in or auto-repair keeps removing existing ones, so âthe set of instances that should receive this deploymentâ is no longer a fixed value, it has to be re-queried at deploy time.
This article focuses on how that discovery problem gets solved. There are broadly two approaches: a mutable one that queries the private IPs of live instances and restarts a service over SSH, and an immutable one that queries instance IDs and sequentially recreates (reimages) them. On top of these sit the Image pipeline (Compute Gallery) and Auto Scaling policy, which together complete the overall deployment design.
Where a static inventory breaks
Section titled âWhere a static inventory breaksâA traditional deployment script usually assumes: âthe target server list is known ahead of time, and it stays valid for the next deployment too.â On VMSS this assumption fails for two reasons.
First, scale-out/in constantly changes which instances are alive. When scale-out fires because a CPU threshold is crossed, new instances get added; when traffic drops and scale-in fires, some instances get removed. An instance that existed yesterday may not exist today, and vice versa.
Second, which instance gets removed by a scale-in policy depends on the policy in effect, and isnât easy to predict. The default policy first balances across availability zones, then balances across fault domains, and finally removes the instance with the highest instance ID. With the OldestVM policy the oldest instance becomes the removal target; with NewestVM, the most recently created instance does. This selection logic has nothing to do with an IP or instance ID list hardcoded ahead of time in a deployment pipeline. In the end, the only reliable option is to ask the API again, at deploy time, for âthe set of instances that are currently alive.â
Two deploy-time discovery patterns
Section titled âTwo deploy-time discovery patternsâMutable: query IPs, then SSH + systemctl restart
Section titled âMutable: query IPs, then SSH + systemctl restartâThe first approach queries the network interface API of the VMSS at deploy time to get the private IPs of live instances, then SSHes into each IP, downloads the deployment script, and restarts the service with systemctl restart. The instance itself is kept as-is; only the application running on top of it changes, hence it can be called a mutable in-place update.
This approach lends itself naturally to parallel deployment. Once the IP list is obtained, independent deployment tasks can run against each IP concurrently, shortening overall deployment time. On the other hand, partial failure happens at the instance level. If SSH fails or the service restart fails on two out of ten instances, the other eight move to the new version while two remain on the old one. Detecting this state and retrying or rolling back is something the pipeline has to handle itself.
Immutable: query instance IDs, then vmss reimage
Section titled âImmutable: query instance IDs, then vmss reimageâThe second approach queries the VMSS instance list API at deploy time to get the IDs of live instances, then runs a reimage command against those IDs sequentially. Reimage recreates an instance from a specified image version, so the initialization script defined in custom data runs again from scratch on every reimage. The instance appears to hold the same slot, but internally itâs reborn entirely, hence it can be called an immutable approach.
This approach is sequential by default. Reimaging multiple instances at once risks a sudden, large drop in available service capacity, so recreating one instance at a time before moving to the next is the common pattern. Overall deployment time tends to be longer than the mutable approach as a result. However, if one instanceâs reimage fails, only that instance remains in its previous state, the state of instances already recreated isnât disturbed. And since each instance re-runs custom data from scratch every time, leftover temp files or process state from a prior deployment donât accumulate.
Trade-offs between the two approaches
Section titled âTrade-offs between the two approachesâ| Item | Mutable (SSH + systemctl) | Immutable (vmss reimage) |
|---|---|---|
| Deployment speed | Parallel execution possible, fast | Sequential execution is common, slow |
| Partial failure handling | Failure occurs per instance; pipeline must detect/retry directly | Failure occurs per instance; doesnât affect other instances |
| State drift risk | Restart failures or interrupted scripts can accumulate version mismatch across instances | Custom data re-runs from scratch each time, leaving little room for drift |
| Rollback difficulty | Relatively simple; redeploy the previous script version | Depends on the Image pipeline, must reimage against a prior Image version |
Itâs hard to say either side is always superior. If deployment frequency is high and application deployment units are small, the speed of the mutable approach is favorable; if you want to keep instance state fully reproducible, the immutable approach is favorable. That said, the immutable approach isnât self-contained. For a reimage to count as âthe latest deployment,â the image version being reimaged to must itself contain the latest application, which requires a separate pipeline to build that image.
An Image pipeline is what makes reimage count as a deployment
Section titled âAn Image pipeline is what makes reimage count as a deploymentâThe core premise of the immutable approach is: âreimaging brings up the latest version.â For that premise to hold, the image the VMSS references must itself contain the latest application execution automation. The pipeline for this generally follows this flow.
Preparing the golden VM splits into two ways. One is registering a systemd service ahead of time so the service starts automatically after boot; the other is putting an initialization script into custom data so application execution automation runs every time the instance boots. The systemd approach has the advantage of clear service-state management (start/stop/restart), while the custom data approach re-runs the script on every instance creation and reimage, which pairs well with immutable deployment.
From this prepared VM you take a Snapshot, build a Managed Image from that Snapshot, and register that Managed Image as a new Image Version under an Image Definition in the Compute Gallery. The VMSS is created or updated to reference this Image Version, and from then on the reimage command becomes an operation that recreates instances against this referenced version. In other words, âdeploymentâ in the immutable approach actually splits into two stages: (1) building a new Image Version containing the new application version and updating the VMSS to reference it, and (2) reimaging individual instances to that version. Both stages have to be in place for a single reimage to complete a deployment.
Interaction with Auto Scaling
Section titled âInteraction with Auto ScalingâDeploy-time discovery only guarantees the state right after deployment finishes, it doesnât control the state changes Auto Scaling causes afterward. Two drift paths are worth examining.
When scale-out fires, new instances are created by running the custom data of whatever image the VMSS currently references. If that image contains the latest application version, the new instance is born up to date. The problem case is when deployment has been happening via the mutable approach (SSH + systemctl restart). In that case the image itself was never updated, so a new instance created by scale-out starts from the version predating the application deployment. A version mismatch appears between instances that already received the SSH-based deployment and instances newly created afterward.
When scale-in fires, which instance gets removed depends on the scale-in policy. Depending on whether the default policy, OldestVM, or NewestVM is used, the situation right after a deployment can get entangled differently. For example, if scale-in occurs while only some instances have finished sequential reimage in the immutable approach, the NewestVM policy may pick the instance that just finished reimage (and is therefore the most recently recreated) as the removal target, while the OldestVM policy may pick an older instance that hasnât received the reimage yet. Because either policy can lead to an unintended outcome when deployment progress overlaps with a scale-in event, itâs safer to pause scale-in or apply instance protection during a deployment whenever possible.
Honest limits in practice
Section titled âHonest limits in practiceâThe SSH + systemctl approach is simple to implement and fast to deploy, but the burden of managing partial failures grows as instance count increases. At a scale of ten or twenty instances, accumulating individual errors (SSH connection failures, transient network drops, service restart timeouts) makes it cumbersome to track âhow many instances are currently on the latest versionâ from pipeline logs alone. The point where this burden becomes unmanageable, or where a requirement emerges to keep instance state fully reproducible, is the signal to consider switching to immutable reimage.
That said, switching to the immutable approach doesnât make every problem disappear. It introduces the new burden of operating a separate Image pipeline, sequential reimage lengthens overall deployment time, and the interaction with Auto Scaling (the drift issues covered above) still needs to be managed separately. In other words, this isnât a relationship where one fully replaces the other; itâs a trade-off relationship where the choice shifts depending on deployment frequency, instance scale, and the required level of state reproducibility.
Discovery-to-deployment execution flow
Section titled âDiscovery-to-deployment execution flowâBoth approaches share a common structure in the end: âquery who is currently alive at deploy time, then run the deployment against that set.â Put into a single flow, it looks like this.
The CI pipeline asks the Azure API for the current live instance set every time a deployment starts, then runs the deployment through either the mutable or the immutable path based on that result. Either way, aggregating each instanceâs success or failure into a final deployment verdict is a required last step.
Closing
Section titled âClosingâDeploying to VMSS ultimately means asking, every single time, âwhat is alive right now.â A static inventory canât answer that question, deploy-time discovery takes its place. Mutable in-place update and immutable reimage are just two different ways of consuming that discovery result, and neither is always the right answer. If you choose the immutable approach, a Compute Gallery-based Image pipeline becomes its prerequisite, and whichever approach you use, the drift Auto Scaling creates and the selection logic of the scale-in policy remain separate concerns that the deployment design has to address.