-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AKS Backup scenario in Terraform #158
Open
HoussemDellai
wants to merge
54
commits into
Azure:main
Choose a base branch
from
HoussemDellai:tf-aks-backup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
73a57a7
impl all in one TF
HoussemDellai abfef1e
impl all in one TF
HoussemDellai 466635a
impl all in one TF
HoussemDellai 6581834
impl all in one TF
HoussemDellai 706752d
impl all in one TF
HoussemDellai 2f140f4
impl all in one TF
HoussemDellai f9c66d4
impl all in one TF
HoussemDellai 54f5a4a
impl all in one TF
HoussemDellai cfb12c5
impl all in one TF
HoussemDellai bce3ba9
impl all in one TF
HoussemDellai 7dc8c36
impl all in one TF
HoussemDellai 6c104e2
impl all in one TF
HoussemDellai f3b358e
impl all in one TF
HoussemDellai cb3e498
work in progress for branch tf-aks-backup
HoussemDellai dc23437
work in progress for branch tf-aks-backup
HoussemDellai 29c0bd7
work in progress for branch tf-aks-backup
HoussemDellai e1020a7
work in progress for branch tf-aks-backup
HoussemDellai 3b34b8c
work in progress for branch tf-aks-backup
HoussemDellai 49d5bd3
work in progress for branch tf-aks-backup
HoussemDellai 6ef08ed
work in progress for branch tf-aks-backup
HoussemDellai f149a8b
work in progress for branch tf-aks-backup
HoussemDellai 2cfe885
work in progress for branch tf-aks-backup
HoussemDellai 686c4b9
work in progress for branch tf-aks-backup
HoussemDellai b8d000e
work in progress for branch tf-aks-backup
HoussemDellai a7101fe
work in progress for branch tf-aks-backup
HoussemDellai f853d9c
work in progress for branch tf-aks-backup
HoussemDellai 4efe955
work in progress for branch tf-aks-backup
HoussemDellai 8729611
work in progress for branch tf-aks-backup
HoussemDellai 526cc02
work in progress for branch tf-aks-backup
HoussemDellai bc05f74
work in progress for branch tf-aks-backup
HoussemDellai 0469627
work in progress for branch tf-aks-backup
HoussemDellai aa259c4
work in progress for branch tf-aks-backup
HoussemDellai 3802cd6
work in progress for branch tf-aks-backup
HoussemDellai add5397
work in progress for branch tf-aks-backup
HoussemDellai 2529ad1
work in progress for branch tf-aks-backup
HoussemDellai 31ac089
work in progress for branch tf-aks-backup
HoussemDellai c55731b
work in progress for branch tf-aks-backup
HoussemDellai 01ea657
work in progress for branch tf-aks-backup
HoussemDellai 86db095
work in progress for branch tf-aks-backup
HoussemDellai 0371bb9
work in progress for branch tf-aks-backup
HoussemDellai 281a6ef
work in progress for branch tf-aks-backup
HoussemDellai a375def
work in progress for branch tf-aks-backup
HoussemDellai ceb1671
work in progress
197ae5f
work in progress
89aa020
work in progress for branch tf-aks-backup
HoussemDellai 3a86c77
work in progress for branch tf-aks-backup
HoussemDellai a1332e5
work in progress for branch tf-aks-backup
HoussemDellai 5ed1086
work in progress
3ba60e3
Merge branch 'tf-aks-backup' of https://github.com/HoussemDellai/AKS-…
eb90391
work in progress for branch tf-aks-backup
HoussemDellai 397f5a7
work in progress for branch tf-aks-backup
HoussemDellai 6ab116a
work in progress
3428323
work in progress
6596c4d
work in progress
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Scenario: creating a backup of an AKS cluster | ||
|
||
## Introduction | ||
|
||
This scenario shows how to create a backup of an existing AKS cluster and then use it to restore to a new cluster. | ||
|
||
## Steps to follow | ||
|
||
1. Deploy the AKS Landing Zone by following the steps in folder `Scenarios/AKS-Landing-Zone`. | ||
2. Provide the required input parameters in the `Scenarios/AKS-Backup/terraform.tfvars` file. | ||
3. Run the current Terraform templates to create a backup of the AKS cluster and a new cluster for restore: | ||
|
||
```sh | ||
cd Scenarios/AKS-Backup | ||
terraform init | ||
terraform apply | ||
``` | ||
|
||
4. Deploy a kubernetes deployment and persistent volumes to the original AKS cluster. | ||
|
||
```sh | ||
kubectl apply -f ./kubernetes/deploy_disk_lrs.yaml | ||
kubectl apply -f ./kubernetes/deploy_disk_zrs_sc.yaml | ||
``` | ||
|
||
5. Trigger the backup of the AKS cluster on the azure portal through Vault backup. | ||
> You might need to click on the 'fix errors' button to resolve the backup misconfigurations. | ||
6. Watch for the resources created in the backup resource group (disk snapshots and blobs in the backup storage account). | ||
7. Trigger the restore operation to the new AKS cluster using the backup created in the previous step. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
data "azurerm_kubernetes_cluster" "aks-1" { | ||
name = var.aksName | ||
resource_group_name = var.rgLzName | ||
} | ||
|
||
data "azurerm_user_assigned_identity" "aks1_identity" { | ||
name = split("/", data.azurerm_kubernetes_cluster.aks-1.identity.0.identity_ids[0])[8] | ||
resource_group_name = var.rgLzName | ||
} | ||
|
||
resource "azurerm_role_assignment" "cluster_msi_contributor_on_snap_rg" { | ||
scope = azurerm_resource_group.rg-backup.id | ||
role_definition_name = "Contributor" | ||
principal_id = data.azurerm_user_assigned_identity.aks1_identity.principal_id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
resource "azurerm_kubernetes_cluster" "aks-2" { | ||
name = "aks-cluster" | ||
location = azurerm_resource_group.rg-2.location | ||
resource_group_name = azurerm_resource_group.rg-2.name | ||
dns_prefix = "aks" | ||
kubernetes_version = "1.30.5" | ||
|
||
network_profile { | ||
network_plugin = "azure" | ||
network_plugin_mode = "overlay" | ||
# subnet_id = azurerm_subnet.snet-aks-2.id | ||
} | ||
|
||
default_node_pool { | ||
name = "systempool" | ||
temporary_name_for_rotation = "syspool" | ||
node_count = 3 | ||
vm_size = "standard_b2als_v2" | ||
zones = [1, 2, 3] | ||
} | ||
|
||
identity { | ||
type = "SystemAssigned" | ||
} | ||
|
||
lifecycle { | ||
ignore_changes = [ | ||
default_node_pool.0.upgrade_settings | ||
] | ||
} | ||
} | ||
|
||
|
||
|
||
resource "azurerm_role_assignment" "cluster_2_msi_contributor_on_snap_rg" { | ||
scope = azurerm_resource_group.rg-backup.id | ||
role_definition_name = "Contributor" | ||
principal_id = azurerm_kubernetes_cluster.aks-2.identity.0.principal_id | ||
} | ||
|
||
resource "azurerm_kubernetes_cluster_extension" "extension-2" { | ||
name = "backup-extension" | ||
cluster_id = azurerm_kubernetes_cluster.aks-2.id | ||
extension_type = "Microsoft.DataProtection.Kubernetes" | ||
release_train = "stable" | ||
release_namespace = "dataprotection-microsoft" | ||
configuration_settings = { | ||
"configuration.backupStorageLocation.bucket" = azurerm_storage_container.container.name | ||
"configuration.backupStorageLocation.config.storageAccount" = azurerm_storage_account.storage.name | ||
"configuration.backupStorageLocation.config.resourceGroup" = azurerm_storage_account.storage.resource_group_name | ||
"configuration.backupStorageLocation.config.subscriptionId" = data.azurerm_client_config.current.subscription_id | ||
"credentials.tenantId" = data.azurerm_client_config.current.tenant_id | ||
} | ||
} | ||
|
||
resource "azurerm_role_assignment" "extension_2_storage_account_contributor" { | ||
scope = azurerm_storage_account.storage.id | ||
role_definition_name = "Storage Account Contributor" | ||
principal_id = azurerm_kubernetes_cluster_extension.extension-2.aks_assigned_identity[0].principal_id | ||
} | ||
|
||
resource "azurerm_kubernetes_cluster_trusted_access_role_binding" "aks_2_trusted_access" { | ||
kubernetes_cluster_id = azurerm_kubernetes_cluster.aks-2.id | ||
name = "trusted-access" | ||
roles = ["Microsoft.DataProtection/backupVaults/backup-operator"] | ||
source_resource_id = azurerm_data_protection_backup_vault.backup-vault.id | ||
} | ||
|
||
resource "azurerm_role_assignment" "vault_msi_read_on_cluster_2" { | ||
scope = azurerm_kubernetes_cluster.aks-2.id | ||
role_definition_name = "Reader" | ||
principal_id = azurerm_data_protection_backup_vault.backup-vault.identity[0].principal_id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
resource "azurerm_kubernetes_cluster_extension" "extension-1" { | ||
name = "backup-extension" | ||
cluster_id = data.azurerm_kubernetes_cluster.aks-1.id | ||
extension_type = "Microsoft.DataProtection.Kubernetes" | ||
release_train = "stable" | ||
release_namespace = "dataprotection-microsoft" | ||
configuration_settings = { | ||
"configuration.backupStorageLocation.bucket" = azurerm_storage_container.container.name | ||
"configuration.backupStorageLocation.config.storageAccount" = azurerm_storage_account.storage.name | ||
"configuration.backupStorageLocation.config.resourceGroup" = azurerm_storage_account.storage.resource_group_name | ||
"configuration.backupStorageLocation.config.subscriptionId" = data.azurerm_client_config.current.subscription_id | ||
"credentials.tenantId" = data.azurerm_client_config.current.tenant_id | ||
} | ||
} | ||
|
||
resource "azurerm_role_assignment" "extension_1_storage_account_contributor" { | ||
scope = azurerm_storage_account.storage.id | ||
role_definition_name = "Storage Account Contributor" | ||
principal_id = azurerm_kubernetes_cluster_extension.extension-1.aks_assigned_identity[0].principal_id | ||
} | ||
|
||
data "azurerm_client_config" "current" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
resource "azurerm_data_protection_backup_instance_kubernetes_cluster" "backup_instance_aks" { | ||
name = "backup-instance-aks" | ||
location = azurerm_resource_group.rg-backup.location | ||
vault_id = azurerm_data_protection_backup_vault.backup-vault.id | ||
kubernetes_cluster_id = data.azurerm_kubernetes_cluster.aks-1.id | ||
snapshot_resource_group_name = azurerm_resource_group.rg-backup.name | ||
backup_policy_id = azurerm_data_protection_backup_policy_kubernetes_cluster.backup_policy_aks.id | ||
|
||
backup_datasource_parameters { | ||
# excluded_namespaces = ["ns1"] | ||
# excluded_resource_types = ["exvolumesnapshotcontents.snapshot.storage.k8s.io"] | ||
cluster_scoped_resources_enabled = true | ||
# included_namespaces = ["*"] # ["test-included-namespaces"] | ||
# included_resource_types = ["*"] # ["involumesnapshotcontents.snapshot.storage.k8s.io"] | ||
# label_selectors = ["*"] # ["kubernetes.io/metadata.name:test"] | ||
volume_snapshot_enabled = true | ||
} | ||
|
||
depends_on = [ | ||
azurerm_role_assignment.extension_1_storage_account_contributor, | ||
azurerm_role_assignment.vault_msi_read_on_cluster, | ||
azurerm_role_assignment.vault_msi_read_on_snap_rg, | ||
azurerm_role_assignment.cluster_msi_contributor_on_snap_rg, | ||
azurerm_role_assignment.vault_msi_snapshot_contributor_on_snap_rg, | ||
azurerm_role_assignment.vault_msi_data_operator_on_snap_rg, | ||
azurerm_role_assignment.vault_msi_data_contributor_on_storage, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
resource "azurerm_data_protection_backup_policy_kubernetes_cluster" "backup_policy_aks" { | ||
name = "backup-policy-aks" | ||
resource_group_name = azurerm_data_protection_backup_vault.backup-vault.resource_group_name | ||
vault_name = azurerm_data_protection_backup_vault.backup-vault.name | ||
|
||
backup_repeating_time_intervals = ["R/2023-05-23T02:30:00+00:00/P1W"] | ||
|
||
retention_rule { | ||
name = "Daily" | ||
priority = 25 | ||
|
||
life_cycle { | ||
duration = "P84D" | ||
data_store_type = "OperationalStore" | ||
} | ||
|
||
criteria { | ||
days_of_week = ["Thursday"] | ||
months_of_year = ["November"] | ||
weeks_of_month = ["First"] | ||
scheduled_backup_times = ["2023-05-23T02:30:00Z"] | ||
} | ||
} | ||
|
||
default_retention_rule { | ||
life_cycle { | ||
duration = "P14D" | ||
data_store_type = "OperationalStore" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
resource "azurerm_data_protection_backup_vault" "backup-vault" { | ||
name = "backup-vault" | ||
resource_group_name = azurerm_resource_group.rg-backup.name | ||
location = azurerm_resource_group.rg-backup.location | ||
datastore_type = "VaultStore" | ||
redundancy = "LocallyRedundant" # `GeoRedundant` | ||
# cross_region_restore_enabled = "false" # can only be specified when `redundancy` is specified for `GeoRedundant` | ||
soft_delete = "Off" | ||
retention_duration_in_days = 14 | ||
|
||
identity { | ||
type = "SystemAssigned" | ||
} | ||
} | ||
|
||
resource "azurerm_role_assignment" "vault_msi_read_on_cluster" { | ||
scope = data.azurerm_kubernetes_cluster.aks-1.id # var.aksClusterId | ||
role_definition_name = "Reader" | ||
principal_id = azurerm_data_protection_backup_vault.backup-vault.identity[0].principal_id | ||
} | ||
|
||
resource "azurerm_role_assignment" "vault_msi_read_on_snap_rg" { | ||
scope = azurerm_resource_group.rg-backup.id | ||
role_definition_name = "Reader" | ||
principal_id = azurerm_data_protection_backup_vault.backup-vault.identity[0].principal_id | ||
} | ||
|
||
resource "azurerm_role_assignment" "vault_msi_snapshot_contributor_on_snap_rg" { | ||
scope = azurerm_resource_group.rg-backup.id | ||
role_definition_name = "Disk Snapshot Contributor" | ||
principal_id = azurerm_data_protection_backup_vault.backup-vault.identity[0].principal_id | ||
} | ||
|
||
resource "azurerm_role_assignment" "vault_msi_data_operator_on_snap_rg" { | ||
scope = azurerm_resource_group.rg-backup.id | ||
role_definition_name = "Data Operator for Managed Disks" | ||
principal_id = azurerm_data_protection_backup_vault.backup-vault.identity[0].principal_id | ||
} | ||
|
||
resource "azurerm_role_assignment" "vault_msi_data_contributor_on_storage" { | ||
scope = azurerm_storage_account.storage.id | ||
role_definition_name = "Storage Blob Data Contributor" | ||
principal_id = azurerm_data_protection_backup_vault.backup-vault.identity[0].principal_id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
variable "location1" { | ||
default = "swedencentral" | ||
} | ||
|
||
variable "location2" { | ||
default = "swedencentral" | ||
} | ||
|
||
variable "aksName" { | ||
type = string | ||
default = "" | ||
} | ||
|
||
variable "rgLzName" { | ||
type = string | ||
default = "AksTerra-AVM-LZ-RG" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-lrs | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx-lrs | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx-lrs | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
command: | ||
- "/bin/sh" | ||
- "-c" | ||
- while true; do echo $(date) >> /mnt/azuredisk/outfile; sleep 60; done | ||
volumeMounts: | ||
- name: azuredisk-lrs | ||
mountPath: "/mnt/azuredisk" | ||
volumes: | ||
- name: azuredisk-lrs | ||
persistentVolumeClaim: | ||
claimName: azure-managed-disk-lrs | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: azure-managed-disk-lrs | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
storageClassName: managed-csi | ||
resources: | ||
requests: | ||
storage: 5Gi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx-zrs | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx-zrs | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx-zrs | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx | ||
command: | ||
- "/bin/sh" | ||
- "-c" | ||
- while true; do echo $(date) >> /mnt/azuredisk/outfile; sleep 60; done | ||
volumeMounts: | ||
- name: azuredisk-zrs | ||
mountPath: "/mnt/azuredisk" | ||
volumes: | ||
- name: azuredisk-zrs | ||
persistentVolumeClaim: | ||
claimName: azure-managed-disk-zrs | ||
--- | ||
apiVersion: storage.k8s.io/v1 | ||
kind: StorageClass | ||
metadata: | ||
name: managed-csi-zrs | ||
parameters: | ||
skuname: StandardSSD_ZRS | ||
provisioner: disk.csi.azure.com | ||
reclaimPolicy: Delete | ||
volumeBindingMode: WaitForFirstConsumer | ||
allowVolumeExpansion: true | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: azure-managed-disk-zrs | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
storageClassName: managed-csi-zrs | ||
resources: | ||
requests: | ||
storage: 5Gi |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need these commented out?