- .github
- devel
- doc
-
examples
-
dynamic-client
-
notebooks
-
watch
-
yaml_dir
- README.md
- __init__.py
- annotate_deployment.py
- api_discovery.py
- apply_from_dict.py
- apply_from_directory.py
- apply_from_single_file.py
- cluster_scoped_custom_object.py
- cronjob_crud.py
- deployment_create.py
- deployment_crud.py
- duration-gep2257.py
- in_cluster_config.py
- ingress_create.py
- job_crud.py
- multiple_clusters.py
- namespaced_custom_object.py
- node_labels.py
- out_of_cluster_config.py
- pick_kube_config_context.py
- pod_config_list.py
- pod_exec.py
- pod_portforward.py
- remote_cluster.py
- rollout-daemonset.py
- rollout-statefulset.py
-
dynamic-client
- kubernetes
- scripts
- .gitignore
- CHANGELOG.md
- CONTRIBUTING.md
- LICENSE
- MANIFEST.in
- OWNERS
- README.md
- SECURITY_CONTACTS
- code-of-conduct.md
- codecov.yml
- requirements.txt
- setup.cfg
- setup.py
- test-requirements.txt
- tox.ini
Explanation
This Python code demonstrates how to create a Kubernetes deployment using the Python Kubernetes client.
1. Setup and Configuration:
- Imports necessary libraries:
yaml
,os
, andkubernetes.client
. - Loads Kubernetes configuration:
config.load_kube_config()
loads configuration from the default location (usually~/.kube/config
).- Opens the
nginx-deployment.yaml
file, which presumably contains the deployment definition in YAML format.
2. Creating the Deployment:
yaml.safe_load(f)
parses the YAML content from the file into a Python dictionary (dep
).k8s_apps_v1 = client.AppsV1Api()
creates an instance of theAppsV1Api
class, responsible for interacting with the Kubernetes API for applications.k8s_apps_v1.create_namespaced_deployment(body=dep, namespace="default")
sends a request to the Kubernetes API to create the deployment.body=dep
passes the deployment definition from the YAML file.namespace="default"
specifies the namespace where the deployment should be created.- The response (
resp
) is aV1Deployment
object representing the newly created deployment. - The code prints the status message with the name of the created deployment.
Key Points:
- This code assumes you have a
nginx-deployment.yaml
file within ayaml_dir
subdirectory of the current directory. - The
kubernetes.client
library provides a convenient way to interact with the Kubernetes API. - The
AppsV1Api
class offers methods for managing deployments and other application resources.
How it works:
- The script loads configuration from your
kube-config
file, which contains information about your Kubernetes cluster and authentication details. - It reads a YAML file (in this case,
nginx-deployment.yaml
) containing the definition for your desired deployment. - It creates a
V1Deployment
object from the YAML content. - It sends a request to the Kubernetes API to create the deployment using the provided YAML definition.
- It retrieves the newly created deployment object and prints its name.
This code provides a basic example of how to create a Kubernetes deployment using the Python Kubernetes client library. You can adapt this code to create different types of deployments or use it as a starting point for automating other Kubernetes operations.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph