本文介绍了如何使用k8s客户端和cronjob运行定期卷快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要为其运行快照的卷PersistentVolumeClaim.我知道有VolumeSnapshot docs .我认为运行定期快照的最佳方法是为此创建一个CronJob.

I have a volume PersistentVolumeClaim that I want to run snapshots for. I know there is VolumeSnapshot docs. I think the best way to run periodic snapshots is to create a CronJob for that.

因此,我使用 python k8s客户端和我的自定义脚本创建了一个docker镜像.这样,我可以随时运行它,并且可以直接从pod中访问kube config和所有资源.

So I've created a docker image with python k8s client and my custom script. This way I'm able to run it whenever I want and I can access kube config and all resources directly from the pod.

FROM python:3.8-slim-buster
RUN apt-get -qq update && apt-get -qq install -y git
COPY . .
RUN pip install --upgrade pip
RUN pip install git+https://github.com/kubernetes-client/python.git

我遇到的第一个问题是我正在尝试使用apiVersion: snapshot.storage.k8s.io/v1beta1

The first problem I encountered, was that I was trying to use this VolumeSnapshot template using apiVersion: snapshot.storage.k8s.io/v1beta1

client = kubernetes.client.ApiClient()
utils.create_from_yaml(
        k8s_client=client,
        yaml_file='snapshot.yaml',
        verbose=True
    )

但失败

AttributeError: module 'kubernetes.client' has no attribute 'SnapshotStorageV1beta1Api'

确实,我在 python客户端中找不到它 js客户端官方文档v1.18.也许是因为它在测试版?

And indeed I can't find it in python client, js client nor in the official docs v1.18. Maybe it's because it's in beta?

然后我尝试编写一些自定义代码.所以我有

Then I tried writing some custom code. So I have

def main():
    _configuration = kubernetes.client.Configuration()
    _client = kubernetes.client.ApiClient(_configuration)
    _storage_api = kubernetes.client.StorageV1beta1Api(_client)
    storages = _storage_api.get_api_resources()

但是它返回 V1存储类,我找不到从响应中创建快照的任何方法.

But it returns a list of V1 storage class and I can't find any way to create a snapshot from the response.

用我的术语来说,快照是将当前状态复制粘贴到其他任何内容上.你知道如何实现吗?

In my terminology, a snapshot is a copy-paste of a current state to anything else. Do you know how to achieve that?

我觉得我对此进行了过度设计,但我不想使用诸如 stash 之类的第三方.

I feel like I'm overengineering this but I don't want to use 3rd parties such as stash.

我正在GKE上运行.

推荐答案

如果您登录gcloud控制台并在侧边栏中,则将该persistentVolumeClaim映射到PersistentVolume

That persistentVolumeClaim is mapped to a PersistentVolume, if you log into your gcloud console and on the Sidebar

Compute Engine->磁盘

Compute Engine -> Disks

您将获得项目中正在使用的所有GCE磁盘的列表.

You will get a list of all the GCE Disks that are being used on your project.

您将需要运行

kubectl get pvc --namespace YOUR_NAMESPACE

这将为您提供所有pvc的列表,您需要弄清楚所需的PVC是哪个,以获取磁盘名称,以便可以在控制台中查看需要做的事情

this will get you a list of all pvc, you need to figure out which one is the PVC you want, in order to get the disk name so you can view in the console you need to do something like this

╰─ kubectl describe pvc NAME_OF_PVC  --namespace YOUR_NAMESPACE
Name:          NAME_OF_PVC
Namespace:     YOUR_NAMESPACE
StorageClass:  standard
Status:        Bound
Volume:        pvc-61e864b6-6fbf-4a36-80af-8a65e1588b58
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      10Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Mounted By:    <none>
Events:        <none>

您的卷名称为pvc-61e864b6-6fbf-4a36-80af-8a65e1588b58

返回控制台并按此名称过滤,然后单击它,您应该能够从此处创建快照.

Go back to the console and filter by this name and then click on it, you should be able to create a snapshot from there.

现在正确的方法是创建快照计划并将其绑定到磁盘,如下所示( https://cloud.google.com/blog/products/compute/introducing-scheduled-snapshots-for-compute-engine-persistent-disk ).

Now the correct approach would be to create a snapshot schedule and bind it to your disk as shown here (https://cloud.google.com/blog/products/compute/introducing-scheduled-snapshots-for-compute-engine-persistent-disk).

完成快照计划的创建后,您可以在控制台中编辑磁盘,并将您创建的任何快照快照计划分配给磁盘.

When you are done creating the snapshot schedule you can edit the disk in the console and assign it whatever shanpshot schedule you created to your disk.

这篇关于如何使用k8s客户端和cronjob运行定期卷快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 11:18