使用Kubernetes Python客户端创建DaemonSet

277 阅读2分钟

目录

阅读时间: 3 分钟

读者朋友们,大家好在这篇博客中,我们将看到如何使用 Kubernetes Python客户端创建一个守护集。我们都知道,通常我们使用kubectl命令来创建、列出、更新、删除kubernetes资源。但在这篇博客中,我们将看到如何使用Python来做这些事情的资源。

前提条件

在我们使用K8s python客户端创建守护集之前,有一些先决条件需要遵循。

Kubernetes库为我们提供了客户端和配置等模块,我们将在这里使用这些模块。所以,让我们安装Kubernetes Python客户端。

$ pip install kubernetes

现在,我们已经安装了python-kubernetes包。

所以,让我们开始吧

创建Daemonset。

我将创建一个目录,在这个目录中我将创建一个名为create.py的文件。

Kubernetes Python ClientKubernetes Python Client

我的create.py文件包含以下代码,用于使用Kubernetes Python客户端创建守护集。现在我们已经安装了python-kubernetes包,我们可以导入它。

from kubernetes import client, config

在这段代码中,我创建了两个函数,一个是create_daemon_set_object( ) ,另一个是create_daemon_set( ) 。我在我的默认命名空间中创建了这个守护神,如果你想,你也可以使用其他的命名空间。

from kubernetes import client, config

def create_daemon_set_object():
    container = client.V1Container(
        name="ds-redis",
        image="redis",
        image_pull_policy="IfNotPresent",
        ports=[client.V1ContainerPort(container_port=6379)],
    )
    # Template
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={"app": "redis"}),
        spec=client.V1PodSpec(containers=[container]))
    # Spec
    spec = client.V1DaemonSetSpec(
        selector=client.V1LabelSelector(
            match_labels={"app": "redis"}
        ),
        template=template)
    # DaemonSet
    daemonset = client.V1DaemonSet(
        api_version="apps/v1",
        kind="DaemonSet",
        metadata=client.V1ObjectMeta(name="daemonset-redis"),
        spec=spec)

    return daemonset

def create_daemon_set(apps_v1_api, daemon_set_object):
    # Create the Daemonset in default namespace
    # You can replace the namespace with you have created
    apps_v1_api.create_namespaced_daemon_set(
        namespace="default", body=daemon_set_object
    )
def main():
    # Loading the local kubeconfig
    config. load_kube_config()
    apps_v1_api = client.AppsV1Api()
    core_v1_api = client.CoreV1Api()
    daemon_set_obj = create_daemon_set_object()

    create_daemon_set(apps_v1_api, daemon_set_obj)

if __name__ == "__main__":
    main()

现在,是时候创建守护集了。所以,我现在将运行python代码。

$ python3 create.py

正如你在这里看到的,我的守护集已经成功创建。

Kubernetes Python ClientKubernetes Python Client

更新守护集。

为了更新这个守护集,我将创建一个名为update.py的文件。在这个文件中,我将更新这里的容器镜像。我的文件包含以下代码用于更新守护集。

from kubernetes import client, config


def create_daemon_set_object():
    container = client.V1Container(
        name="ds-redis",
        image="redis",
        image_pull_policy="IfNotPresent",
        ports=[client.V1ContainerPort(container_port=6379)],
    )
    # Template
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={"app": "redis"}),
        spec=client.V1PodSpec(containers=[container]))
    # Spec
    spec = client.V1DaemonSetSpec(
        selector=client.V1LabelSelector(
            match_labels={"app": "redis"}
        ),
        template=template)
    # DaemonSet
    daemonset = client.V1DaemonSet(
        api_version="apps/v1",
        kind="DaemonSet",
        metadata=client.V1ObjectMeta(name="daemonset-redis"),
        spec=spec)

    return daemonset


def create_daemon_set(apps_v1_api, daemon_set_object):
    # Create the Daemonset in default namespace
    # You can replace the namespace with you have created
    apps_v1_api.create_namespaced_daemon_set(
        namespace="default", body=daemon_set_object
    )


def update_daemon_set(apps_v1_api, daemonset):
    # Update container image
    daemonset.spec.template.spec.containers[0].image = "redis:6.2"
    daemonset_name = daemonset.metadata.name
    # Patch the daemonset
    apps_v1_api.patch_namespaced_daemon_set(
        name=daemonset_name, namespace="default", body=daemonset
    )



def main():
    # Loading the local kubeconfig
    config.load_kube_config()
    apps_v1_api = client.AppsV1Api()
    core_v1_api = client.CoreV1Api()
    daemon_set_obj = create_daemon_set_object()

    update_daemon_set(apps_v1_api, daemon_set_obj)


if __name__ == "__main__":
    main()

在这段代码中,我使用了update_daemon_set( ) 函数来更新这个守护集。在这个函数中,我正在更新容器镜像。现在,我将运行该代码进行更新。

$ python3 update.py

正如你在这里看到的,我的容器镜像被更新了。

Kubernetes Python ClientKubernetes Python Client

所以,我们现在已经成功完成了。这就是我们如何使用Kubernetes python客户端的方法。

结语。

谢谢你坚持到最后。在这篇博客中,我们看到了如何轻松地使用kubernetes python客户端创建和更新守护集。如果你喜欢这篇博客,请通过竖起大拇指来表示感谢,并分享这篇博客,给我建议如何改进我未来的文章以满足你的需求。

学习愉快!

分享Knol。

相关信息