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
kubectlinstalled (or it's not in thePATH). You can use image:google/cloud-sdk:latest. This image already havecloud-sdkinstalled which includes:gcloudkubectlTo run a
CronJobthat will get the information aboutPV's and change the configuration ofGCPstorage you will need following accesses:Kubernetes/GKEAPI(kubectl) -ServiceAccountwith aRoleandRoleBinding.GCPAPI (gcloud) -Google Service accountwithIAMpermissions for storage operations.I found this links helpful when assigning permissions to list
PV's:The recommended way to assign specific permissions for
GCPaccess: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 forpdNameinstead ofNameas thepdNameis representation of thegce-pddisk 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
gcloudcommand.One of the options:
Above command will get the
PDNameattribute 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: Forbidinstead ofReplace. ReplacedCronJobwill start from the beginning iterating over all of those disks. Command could not complete in the desired time andCronJobwill be replaced.kubectlversion as the official support allows +1/-1 version difference between client and a server (cloud-sdk:latestusesv1.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
CSIplugin for yourStorageClasswill allow you to useVolume Snapshotsinside ofGKE:Additional resources: