|
| 1 | +--- |
| 2 | +draft: false |
| 3 | +date: 2025-01-02 |
| 4 | +authors: |
| 5 | + - rfernandezdo |
| 6 | +categories: |
| 7 | + - Azure Services |
| 8 | +tags: |
| 9 | + - Azure App Service |
| 10 | + - Deployment Slots |
| 11 | + - DevOps |
| 12 | +--- |
| 13 | + |
| 14 | +# Azure App Service Deployment Slots: Despliegues sin downtime |
| 15 | + |
| 16 | +## Resumen |
| 17 | + |
| 18 | +Los Deployment Slots en Azure App Service te permiten desplegar nuevas versiones de tu aplicación sin tiempo de inactividad. Voy al grano: es la forma más sencilla de implementar blue-green deployments en Azure. |
| 19 | + |
| 20 | +## ¿Qué son los Deployment Slots? |
| 21 | + |
| 22 | +Los Deployment Slots son entornos en vivo dentro de tu App Service donde puedes desplegar diferentes versiones de tu aplicación. Cada slot: |
| 23 | + |
| 24 | +- Tiene su propia URL |
| 25 | +- Puede tener configuración independiente |
| 26 | +- Permite swap instantáneo entre slots |
| 27 | +- Comparte el mismo plan de App Service |
| 28 | + |
| 29 | +## Caso de uso típico |
| 30 | + |
| 31 | +```bash |
| 32 | +# Variables |
| 33 | +RG="my-rg" |
| 34 | +APP_NAME="my-webapp" |
| 35 | +LOCATION="westeurope" |
| 36 | + |
| 37 | +# Crear App Service Plan (mínimo Standard) |
| 38 | +az appservice plan create \ |
| 39 | + --name ${APP_NAME}-plan \ |
| 40 | + --resource-group $RG \ |
| 41 | + --location $LOCATION \ |
| 42 | + --sku S1 |
| 43 | + |
| 44 | +# Crear App Service |
| 45 | +az webapp create \ |
| 46 | + --name $APP_NAME \ |
| 47 | + --resource-group $RG \ |
| 48 | + --plan ${APP_NAME}-plan |
| 49 | + |
| 50 | +# Crear slot de staging |
| 51 | +az webapp deployment slot create \ |
| 52 | + --name $APP_NAME \ |
| 53 | + --resource-group $RG \ |
| 54 | + --slot staging |
| 55 | +``` |
| 56 | + |
| 57 | +## Workflow de despliegue |
| 58 | + |
| 59 | +**1. Desplegar a staging:** |
| 60 | + |
| 61 | +```bash |
| 62 | +# Desplegar código al slot staging |
| 63 | +az webapp deployment source config-zip \ |
| 64 | + --resource-group $RG \ |
| 65 | + --name $APP_NAME \ |
| 66 | + --slot staging \ |
| 67 | + --src app.zip |
| 68 | +``` |
| 69 | + |
| 70 | +**2. Validar en staging:** |
| 71 | + |
| 72 | +URL del slot: `https://{app-name}-staging.azurewebsites.net` |
| 73 | + |
| 74 | +**3. Hacer swap a producción:** |
| 75 | + |
| 76 | +```bash |
| 77 | +# Swap directo staging -> production |
| 78 | +az webapp deployment slot swap \ |
| 79 | + --resource-group $RG \ |
| 80 | + --name $APP_NAME \ |
| 81 | + --slot staging \ |
| 82 | + --target-slot production |
| 83 | + |
| 84 | +# Swap con preview (recomendado) |
| 85 | +az webapp deployment slot swap \ |
| 86 | + --resource-group $RG \ |
| 87 | + --name $APP_NAME \ |
| 88 | + --slot staging \ |
| 89 | + --target-slot production \ |
| 90 | + --action preview |
| 91 | + |
| 92 | +# Después de validar, completar swap |
| 93 | +az webapp deployment slot swap \ |
| 94 | + --resource-group $RG \ |
| 95 | + --name $APP_NAME \ |
| 96 | + --slot staging \ |
| 97 | + --target-slot production \ |
| 98 | + --action swap |
| 99 | +``` |
| 100 | + |
| 101 | +!!! warning "Proceso de swap" |
| 102 | + Durante el swap, Azure sigue este proceso: |
| 103 | + 1. Aplica settings del target slot al source slot |
| 104 | + 2. Espera a que todas las instancias se reinicien y calienten |
| 105 | + 3. Si todas las instancias están healthy, intercambia el routing |
| 106 | + 4. El target slot (production) queda con la nueva app sin downtime |
| 107 | + |
| 108 | + Tiempo total: 1-5 minutos dependiendo de warmup. La producción NO sufre downtime. |
| 109 | + |
| 110 | +## Configuración sticky vs swappable |
| 111 | + |
| 112 | +No toda la configuración se intercambia en el swap: |
| 113 | + |
| 114 | +**Sticky (no se mueve con el código):** |
| 115 | +- App settings marcadas como "Deployment slot setting" |
| 116 | +- Connection strings marcadas como "Deployment slot setting" |
| 117 | +- Custom domains |
| 118 | +- Nonpublic certificates y TLS/SSL settings |
| 119 | +- Scale settings |
| 120 | +- IP restrictions |
| 121 | +- Always On, Diagnostic settings, CORS |
| 122 | + |
| 123 | +**Swappable (se mueve con el código):** |
| 124 | +- General settings (framework version, 32/64-bit) |
| 125 | +- App settings no marcadas |
| 126 | +- Handler mappings |
| 127 | +- Public certificates |
| 128 | +- WebJobs content |
| 129 | +- Hybrid connections |
| 130 | +- Virtual network integration |
| 131 | + |
| 132 | +**Configurar sticky settings:** |
| 133 | + |
| 134 | +Desde el portal Azure: |
| 135 | +1. Ir a **Configuration** → **Application settings** del slot |
| 136 | +2. Añadir/editar app setting |
| 137 | +3. Marcar checkbox **Deployment slot setting** |
| 138 | +4. Apply |
| 139 | + |
| 140 | +```bash |
| 141 | +# Crear app setting en slot staging |
| 142 | +az webapp config appsettings set \ |
| 143 | + --resource-group $RG \ |
| 144 | + --name $APP_NAME \ |
| 145 | + --slot staging \ |
| 146 | + --settings DATABASE_URL="staging-connection-string" |
| 147 | + |
| 148 | +# Para hacerla sticky: usar portal o ARM template |
| 149 | +# No hay flag CLI directo para marcar como slot-specific |
| 150 | +``` |
| 151 | + |
| 152 | +## Auto-swap para CI/CD |
| 153 | + |
| 154 | +```bash |
| 155 | +# Configurar auto-swap desde staging a production |
| 156 | +az webapp deployment slot auto-swap \ |
| 157 | + --resource-group $RG \ |
| 158 | + --name $APP_NAME \ |
| 159 | + --slot staging \ |
| 160 | + --auto-swap-slot production |
| 161 | +``` |
| 162 | + |
| 163 | +Con auto-swap habilitado: |
| 164 | +1. Push a staging → despliegue automático |
| 165 | +2. Warmup automático del slot |
| 166 | +3. Swap a producción sin intervención manual |
| 167 | + |
| 168 | +**Customizar warmup path:** |
| 169 | + |
| 170 | +```bash |
| 171 | +# Configurar URL de warmup personalizada |
| 172 | +az webapp config appsettings set \ |
| 173 | + --resource-group $RG \ |
| 174 | + --name $APP_NAME \ |
| 175 | + --slot staging \ |
| 176 | + --settings WEBSITE_SWAP_WARMUP_PING_PATH="/health/ready" |
| 177 | + |
| 178 | +# Validar solo códigos HTTP específicos |
| 179 | +az webapp config appsettings set \ |
| 180 | + --resource-group $RG \ |
| 181 | + --name $APP_NAME \ |
| 182 | + --slot staging \ |
| 183 | + --settings WEBSITE_SWAP_WARMUP_PING_STATUSES="200,202" |
| 184 | +``` |
| 185 | + |
| 186 | +## Buenas prácticas |
| 187 | + |
| 188 | +- **Usar staging para testing**: Siempre valida en staging antes del swap |
| 189 | +- **Configurar health checks**: Azure verifica el slot antes de hacer swap |
| 190 | +- **Mantener paridad**: Staging debe replicar producción (misma configuración, DB de test similar) |
| 191 | +- **Rollback rápido**: Si falla, haz swap inverso inmediatamente |
| 192 | +- **Limitar slots**: Máximo 2-3 slots por app (staging, pre-production) |
| 193 | + |
| 194 | +!!! tip "Ahorro de costos" |
| 195 | + Los slots comparten recursos del App Service Plan. No pagas más por tener staging, pero requieres tier Standard o superior. |
| 196 | + |
| 197 | +## Monitoring del swap |
| 198 | + |
| 199 | +```bash |
| 200 | +# Ver historial de swaps |
| 201 | +az webapp deployment slot list \ |
| 202 | + --resource-group $RG \ |
| 203 | + --name $APP_NAME |
| 204 | + |
| 205 | +# Logs durante el swap |
| 206 | +az webapp log tail \ |
| 207 | + --resource-group $RG \ |
| 208 | + --name $APP_NAME \ |
| 209 | + --slot staging |
| 210 | +``` |
| 211 | + |
| 212 | +## Referencias |
| 213 | + |
| 214 | +- [Set up staging environments - Azure App Service](https://learn.microsoft.com/en-us/azure/app-service/deploy-staging-slots) |
| 215 | +- [Azure CLI - webapp deployment slot](https://learn.microsoft.com/en-us/cli/azure/webapp/deployment/slot) |
0 commit comments