I created a schedule configuration inside my Gcloud project to create snapshots of a bunch of virtual disks.
Now I want to add my schedule configuration to my disks, but I dont know how to do it in a automated way, because I have more than 1200 disks.
I tryed to use a POD with a cron inside, but I cannot execute the kubectl command to list all my persistent volumes:
kubectl describe pv | grep "Name" | awk 'NR % 2 == 1' | awk '{print $2}'
I want to use this list with the next command in a Loop to add automatically my programmed schedule to my disks:
gcloud compute disks add-resource-policies [DISK_NAME] --resource-policies [SCHEDULE_NAME] --zone [ZONE]
Thanks in advance for your help.
Edit 1: After some comments I changed my code to add a Kubernetes CronJob, but the result is the same, the code doesn't work (the pod is created, but it gives me an error: ImagePullBackOff):
resource "kubernetes_cron_job" "schedulerdemo" {
metadata {
name = "schedulerdemo"
}
spec {
concurrency_policy = "Replace"
failed_jobs_history_limit = 5
schedule = "*/5 * * * *"
starting_deadline_seconds = 10
successful_jobs_history_limit = 10
job_template {
metadata {}
spec {
backoff_limit = 2
ttl_seconds_after_finished = 10
template {
metadata {}
spec {
container {
name = "scheduler"
image = "imgscheduler"
command = ["/bin/sh", "-c", "date; kubectl describe pv | grep 'Name' | awk 'NR % 2 == 1' | awk '{print $2}'"]
}
}
}
}
}
}
}
Answering the comment:
It means that the image that you are using doesn't have
kubectl
installed (or it's not in thePATH
). You can use image:google/cloud-sdk:latest
. This image already havecloud-sdk
installed which includes:gcloud
kubectl
To run a
CronJob
that will get the information aboutPV
's and change the configuration ofGCP
storage you will need following accesses:Kubernetes/GKE
API(kubectl
) -ServiceAccount
with aRole
andRoleBinding
.GCP
API (gcloud
) -Google Service account
withIAM
permissions for storage operations.I found this links helpful when assigning permissions to list
PV
's:The recommended way to assign specific permissions for
GCP
access:I encourage you to read documentation I linked above and check other alternatives.
As for the script used inside of a
CronJob
. You should look forpdName
instead ofName
as thepdName
is representation of thegce-pd
disk inGCP
(assuming that we are talking about in-tree plugin).You will have multiple options to retrieve the disk name from the API to use it in the
gcloud
command.One of the options:
Above command will get the
PDName
attribute from thePV
's and iterate with each of them in the command afterxargs
.Some of the things to take into consideration when creating a script/program:
.spec.concurrencyPolicy: Forbid
instead ofReplace
. ReplacedCronJob
will start from the beginning iterating over all of those disks. Command could not complete in the desired time andCronJob
will be replaced.kubectl
version as the official support allows +1/-1 version difference between client and a server (cloud-sdk:latest
usesv1.19.3
).I highly encourage you to look on other methods to backup your
PVC
's (like for exampleVolumeSnapshots
).Take a look on below links for more reference/ideas:
It's worth to mention that:
Switching to
CSI
plugin for yourStorageClass
will allow you to useVolume Snapshots
inside ofGKE
:Additional resources: