Overview
The Deployment class represents a Kubernetes Deployment, which enables declarative updates for Pods and ReplicaSets. Deployments provide features like rolling updates, rollbacks, and scaling.
Class Definition
from ocp_resources.deployment import Deployment
API Group: apps
Constructor
Deployment accepts all parameters from NamespacedResource, plus:
Label selector for pods. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all objects.
Pod template describing the pods that will be created. Must include metadata and spec.
Minimum number of seconds for which a newly created pod should be ready without any container crashing for it to be considered available. Defaults to 0.
Indicates that the deployment is paused
progress_deadline_seconds
Maximum time in seconds for a deployment to make progress before it is considered failed. Defaults to 600s.
Number of desired pods. Defaults to 1.
Number of old ReplicaSets to retain for rollback. Defaults to 10.
Deployment strategy describing how to replace existing pods with new ones. Types include RollingUpdate and Recreate.
Methods
scale_replicas
Update the number of replicas in the deployment.
def scale_replicas(self, replica_count: int)
Number of replicas to scale to
Returns: Result of the update operation
wait_for_replicas
Wait until all replicas are updated and available.
def wait_for_replicas(
self,
deployed: bool = True,
timeout: int = TIMEOUT_4MINUTES
) -> None
True to wait for replicas to be deployed, False to wait for no replicas
timeout
int
default:"TIMEOUT_4MINUTES"
Time to wait for the deployment in seconds
Raises: TimeoutExpiredError if replicas are not ready within timeout
Inherited Methods
Deployment inherits all methods from NamespacedResource and Resource.
Examples
Creating a Basic Deployment
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="nginx-deployment",
namespace="default",
replicas=3,
selector={"matchLabels": {"app": "nginx"}},
template={
"metadata": {"labels": {"app": "nginx"}},
"spec": {
"containers": [{
"name": "nginx",
"image": "nginx:1.14.2",
"ports": [{"containerPort": 80}]
}]
}
}
)
deployment.create(wait=True)
print(f"Deployment {deployment.name} created")
Creating a Deployment with Rolling Update Strategy
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="app-deployment",
namespace="default",
replicas=5,
selector={"matchLabels": {"app": "myapp"}},
template={
"metadata": {"labels": {"app": "myapp"}},
"spec": {
"containers": [{
"name": "app",
"image": "myapp:v1"
}]
}
},
strategy={
"type": "RollingUpdate",
"rollingUpdate": {
"maxSurge": 1,
"maxUnavailable": 0
}
}
)
deployment.create()
Scaling a Deployment
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="nginx-deployment",
namespace="default"
)
# Scale to 5 replicas
deployment.scale_replicas(replica_count=5)
# Wait for all replicas to be ready
deployment.wait_for_replicas(deployed=True, timeout=300)
print(f"Deployment scaled to 5 replicas")
Scaling Down to Zero
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="nginx-deployment",
namespace="default"
)
# Scale down to 0 replicas
deployment.scale_replicas(replica_count=0)
# Wait for all pods to be terminated
deployment.wait_for_replicas(deployed=False, timeout=300)
print("Deployment scaled down to 0")
Deployment with Resource Limits
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="resource-limited-app",
namespace="default",
replicas=3,
selector={"matchLabels": {"app": "myapp"}},
template={
"metadata": {"labels": {"app": "myapp"}},
"spec": {
"containers": [{
"name": "app",
"image": "myapp:latest",
"resources": {
"requests": {
"memory": "128Mi",
"cpu": "100m"
},
"limits": {
"memory": "256Mi",
"cpu": "200m"
}
}
}]
}
}
)
deployment.create()
Waiting for Deployment to be Available
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="nginx-deployment",
namespace="default",
replicas=3,
selector={"matchLabels": {"app": "nginx"}},
template={
"metadata": {"labels": {"app": "nginx"}},
"spec": {
"containers": [{"name": "nginx", "image": "nginx:latest"}]
}
}
)
deployment.create()
# Wait for deployment to be available
deployment.wait_for_condition(
condition="Available",
status="True",
timeout=300
)
print("Deployment is available")
Using Context Manager
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
with Deployment(
client=client,
name="temp-deployment",
namespace="default",
replicas=2,
selector={"matchLabels": {"app": "temp"}},
template={
"metadata": {"labels": {"app": "temp"}},
"spec": {
"containers": [{"name": "nginx", "image": "nginx:latest"}]
}
},
wait_for_resource=True
) as deploy:
deploy.wait_for_replicas(deployed=True)
print(f"Deployment {deploy.name} is running")
# Deployment is automatically deleted when exiting this block
Creating from YAML
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
yaml_file="/path/to/deployment.yaml"
)
deployment.create()
deployment.wait_for_replicas(deployed=True)
Listing All Deployments
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
# List all deployments in a namespace
for deploy in Deployment.get(client=client, namespace="default"):
status = deploy.instance.status
print(f"Deployment: {deploy.name}")
print(f" Replicas: {status.replicas}")
print(f" Available: {status.availableReplicas or 0}")
print(f" Ready: {status.readyReplicas or 0}")
Updating Deployment Image
from ocp_resources.deployment import Deployment
from ocp_resources.resource import get_client
client = get_client()
deployment = Deployment(
client=client,
name="nginx-deployment",
namespace="default"
)
# Update the container image
deployment.update({
"spec": {
"template": {
"spec": {
"containers": [{
"name": "nginx",
"image": "nginx:1.21.0"
}]
}
}
}
})
# Wait for rollout to complete
deployment.wait_for_replicas(deployed=True, timeout=300)
print("Deployment image updated")
See Also