Skip to content

Deploying to a VMSS Fleet: Deploy-time Instance Discovery and Image Strategy

Interactive Diagram

새 탭 ↗

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.

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.”

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.

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.

ItemMutable (SSH + systemctl)Immutable (vmss reimage)
Deployment speedParallel execution possible, fastSequential execution is common, slow
Partial failure handlingFailure occurs per instance; pipeline must detect/retry directlyFailure occurs per instance; doesn’t affect other instances
State drift riskRestart failures or interrupted scripts can accumulate version mismatch across instancesCustom data re-runs from scratch each time, leaving little room for drift
Rollback difficultyRelatively simple; redeploy the previous script versionDepends 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.

PlantUML Diagram

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.

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.

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.

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.

PlantUML Diagram

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.

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.