Creating Resources

The Kubernetes Python client supports creating new resources using the create() method of the corresponding API resource object.

Creating a new resource

To create a new resource, you need to:

  1. Instantiate the appropriate resource object.
  2. Set the desired attributes of the resource.
  3. Call the create() method on the resource object.

For example, to create a new Pod named my-pod in the default namespace:

from kubernetes import client, config
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create a new Pod object
          pod = client.V1Pod(
              metadata=client.V1ObjectMeta(name="my-pod"),
              spec=client.V1PodSpec(containers=[
                  client.V1Container(name="nginx", image="nginx:1.14.2")
              ]),
          )
          
          # Create the Pod
          api_instance = client.CoreV1Api()
          api_instance.create_namespaced_pod(namespace="default", body=pod)
          

Specifying the resource name

The name argument of the create() method is optional. If not provided, the client will attempt to extract the name from the metadata.name field of the resource object. If the metadata.name field is not set, the client will generate a unique name for the resource.

For example, to create a new Deployment named my-deployment in the default namespace:

from kubernetes import client, config
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create a new Deployment object
          deployment = client.V1Deployment(
              metadata=client.V1ObjectMeta(name="my-deployment"),
              spec=client.V1DeploymentSpec(
                  replicas=3,
                  selector=client.V1LabelSelector(match_labels={"app": "nginx"}),
                  template=client.V1PodTemplateSpec(
                      metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
                      spec=client.V1PodSpec(containers=[
                          client.V1Container(name="nginx", image="nginx:1.14.2")
                      ]),
                  )
              )
          )
          
          # Create the Deployment
          api_instance = client.AppsV1Api()
          api_instance.create_namespaced_deployment(namespace="default", body=deployment)
          

Specifying the namespace

The namespace argument of the create() method is required for namespaced resources. If not provided, the client will use the default namespace.

For example, to create a new Service named my-service in the default namespace:

from kubernetes import client, config
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create a new Service object
          service = client.V1Service(
              metadata=client.V1ObjectMeta(name="my-service"),
              spec=client.V1ServiceSpec(
                  selector={"app": "nginx"},
                  ports=[
                      client.V1ServicePort(port=80, target_port=80)
                  ],
              )
          )
          
          # Create the Service
          api_instance = client.CoreV1Api()
          api_instance.create_namespaced_service(namespace="default", body=service)
          

Specifying the resource name and namespace

You can also specify both the name and namespace arguments of the create() method. This allows you to create resources with specific names in specific namespaces.

For example, to create a new Pod named my-pod in the my-namespace namespace:

from kubernetes import client, config
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create a new Pod object
          pod = client.V1Pod(
              metadata=client.V1ObjectMeta(name="my-pod"),
              spec=client.V1PodSpec(containers=[
                  client.V1Container(name="nginx", image="nginx:1.14.2")
              ]),
          )
          
          # Create the Pod
          api_instance = client.CoreV1Api()
          api_instance.create_namespaced_pod(namespace="my-namespace", body=pod)
          

Creating resources from YAML files

The Kubernetes Python client also provides the create_from_yaml() function, which can be used to create resources from YAML files.

For example, to create a new Pod named my-pod from the my-pod.yaml file:

from kubernetes import client, config
          from kubernetes.utils import create_from_yaml
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create the Pod from the YAML file
          create_from_yaml(client.ApiClient(), "my-pod.yaml", namespace="default")
          

Creating resources from a directory

The create_from_directory() function allows you to create resources from a directory containing YAML files.

For example, to create all the resources from the my-resources directory:

from kubernetes import client, config
          from kubernetes.utils import create_from_directory
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create the resources from the directory
          create_from_directory(client.ApiClient(), "my-resources", namespace="default")
          

Example for creating a namespace

from kubernetes import client, config
          
          # Load kubeconfig
          config.load_kube_config()
          
          # Create a new Namespace object
          namespace = client.V1Namespace(
              metadata=client.V1ObjectMeta(name="my-namespace")
          )
          
          # Create the Namespace
          api_instance = client.CoreV1Api()
          api_instance.create_namespace(body=namespace)
          

Top-Level Directory Explanations

doc/ - This directory contains documentation files for the project.

doc/source/ - This directory contains the source files for the documentation.

examples/ - This directory contains example usage of the Kubernetes client library.

examples/dynamic-client/ - This directory contains examples of using the dynamic client to interact with Kubernetes.

examples/watch/ - This directory contains examples of using the watch function to monitor Kubernetes resources.

examples/yaml_dir/ - This directory contains YAML files used in the examples.

kubernetes/ - This directory contains the main Kubernetes client library.

kubernetes/base/ - This directory contains the base Kubernetes client library.

kubernetes/base/config/ - This directory contains configuration files for the base library.

kubernetes/base/dynamic/ - This directory contains the dynamic client implementation for the base library.

kubernetes/base/hack/ - This directory contains hack files for the base library.

kubernetes/base/leaderelection/ - This directory contains the leader election implementation for the base library.

kubernetes/base/watch/ - This directory contains the watch implementation for the base library.

kubernetes/client/ - This directory contains the top-level client for the Kubernetes client library.

kubernetes/client/api/ - This directory contains the API definitions for the client library.

kubernetes/client/models/ - This directory contains the data models used by the client library.

kubernetes/e2e_test/ - This directory contains end-to-end tests for the Kubernetes client library.

kubernetes/e2e_test/test_yaml/ - This directory contains YAML files used in the end-to-end tests.

kubernetes/utils/ - This directory contains utility functions for the Kubernetes client library.

scripts/ - This directory contains scripts used in the development and build process.