Interacting with Kubernetes Deployments and Services using Python SDK
Interacting with Kubernetes Deployments and Services using Python SDK
Kubernetes has become the de facto choice for a container orchestration platform that automates containerized applications’ deployment, scaling, and management. This article will demonstrate how to interact with Kubernetes Deployments and Services using Python and the official Kubernetes Python client.
Prerequisites
To follow this guide, you will need:
- A Kubernetes cluster is up and running.
kubectl
is installed and configured to access your cluster.- Python
3.x
installed. - Kubernetes Python client.
You can install the Kubernetes Python client library using pip
:
pip install kubernetes
We will cover the following topics:
Table of Contents
- Listing Kubernetes Deployments
- Listing Kubernetes Services
- Updating a Kubernetes Deployment
- Updating a Kubernetes Service
- Deleting a Kubernetes Deployment
- Deleting a Kubernetes Service
- Assembling the Pieces
- Conclusion
Listing Kubernetes Deployments
Let’s create a script to list all existing Deployments in the default
namespace:
from kubernetes import client, config
def list_deployments(api_instance, namespace):
# List Deployments
deployments = api_instance.list_namespaced_deployment(namespace)
for deployment in deployments.items:
print("Name: %s, Replicas: %s" % (deployment.metadata.name, deployment.spec.replicas))
def main():
config.load_kube_config()
apps_v1 = client.AppsV1Api()
namespace = "default"
list_deployments(apps_v1, namespace)
if __name__ == '__main__':
main()
Run the script using the following:
python list_deployments.py
Listing Kubernetes Services
Now, let’s create a script to list all existing Services in the default
namespace:
from kubernetes import client, config
def list_services(api_instance, namespace):
# List Services
services = api_instance.list_namespaced_service(namespace)
for service in services.items:
print("Name: %s, Cluster IP: %s" % (service.metadata.name, service.spec.cluster_ip))
def main():
config.load_kube_config()
namespace = "default"
core_v1 = client.CoreV1Api()
list_services(core_v1, namespace)
if __name__ == '__main__':
main()
Run the script using the following:
python list_services.py
Updating a Kubernetes Deployment
To update a Deployment, you can patch it with the desired changes. In this example, we will update the number of replicas for an existing Deployment:
from kubernetes import client, config
def update_deployment_replicas(api_instance, deployment_name, new_replicas, namespace):
# Update Deployment replicas
body = {
"spec": {
"replicas": new_replicas
}
}
api_response = api_instance.patch_namespaced_deployment(
name=deployment_name,
namespace=namespace,
body=body
)
print("Deployment updated")
def main():
config.load_kube_config()
apps_v1 = client.AppsV1Api()
namespace = "default"
deployment_name = "nginx-deployment"
new_replicas = 5
update_deployment_replicas(apps_v1, deployment_name, new_replicas, namespace)
if __name__ == '__main__':
main()
Run the script using the following:
python update_deployment.py
Verify updates to the Deployment using the following command:
kubectl get deployments
Updating a Kubernetes Service
To update a Service, you can patch it with the desired changes. In this example, we will update the type of an existing Service:
from kubernetes import client, config
def update_service_type(api_instance, service_name, new_type, namespace):
# Update Service type
body = {
"spec": {
"type": new_type
}
}
api_response = api_instance.patch_namespaced_service(
name=service_name,
namespace=namespace,
body=body
)
print("Service updated")
def main():
config.load_kube_config()
core_v1 = client.CoreV1Api()
namespace = "default"
service_name = "nginx-service"
new_type = "NodePort"
update_service_type(core_v1, service_name, new_type, namespace)
if __name__ == '__main__':
main()
Run the script using the following:
python update_service.py
Verify updates to the Service using the following command:
kubectl get services
Deleting a Kubernetes Deployment
To delete a Deployment, use the following script:
from kubernetes import client, config
def delete_deployment(api_instance, deployment_name, namespace):
# Delete Deployment
api_response = api_instance.delete_namespaced_deployment(
name=deployment_name,
namespace=namespace,
body=client.V1DeleteOptions(propagation_policy="Foreground", grace_period_seconds=5)
)
print("Deployment deleted")
def main():
config.load_kube_config()
apps_v1 = client.AppsV1Api()
namespace = "default"
deployment_name = "nginx-deployment"
delete_deployment(apps_v1, deployment_name, namespace)
if __name__ == '__main__':
main()
Run the script using the following:
python delete_deployment.py
Deleting a Kubernetes Service
To delete a Service, use the following script:
from kubernetes import client, config
def delete_service(api_instance, service_name, namespace):
# Delete Service
api_response = api_instance.delete_namespaced_service(
name=service_name,
namespace=namespace,
body=client.V1DeleteOptions()
)
print("Service deleted")
def main():
config.load_kube_config()
core_v1 = client.CoreV1Api()
namespace = "default"
service_name = "nginx-service"
delete_service(core_v1, service_name, namespace)
if __name__ == '__main__':
main()
Run the script using the following:
python delete_service.py
Assembling the Pieces
After putting all the pieces together, we have a list_deployment.py
, list_service.py
, update_deployment.py
, update_service.py
, delete_deployment.py
and delete_service.py
file containing all the code snippets we discussed previously.
Conclusion
To summarise, we demonstrated how to interact with Kubernetes Deployments and Services using Python and the official Kubernetes Python client. You can now use these examples as a starting point to build more complex interactions and automation for your Kubernetes workloads.
Subscribe to Faizan Bashir
Get the latest posts delivered right to your inbox