Production deployment patterns
Graceful shutdown, prod config, rollouts, readiness probes, and ship-with-confidence checks.
How to Mock Kubernetes Secret Mounts in Python
Create and inspect a mock Kubernetes secret volume mount using the official client library and unittest.mock.
import json
from kubernetes import client, config, watch
from unittest.mock import Mock, patch
def create_mock_mount_spec():
"""Create a mock Kubernetes secret volume mount."""
mock_client = Mock()
mock_client.api_version = "v1"
mock_client.kind = "Secret"
mock_client.metadata = {"name": "my-secre…
How to Mock a Dependency for Readiness Probe in Python
Use unittest.mock.Mock to simulate a dependency's readiness check response for testing a service's is_ready method without hitting a real connection.
import time
import unittest
from unittest.mock import Mock
class Service:
def __init__(self, dependency):
self.dependency = dependency
def is_ready(self):
try:
result = self.dependency.check()
return result == "ready"
except Exception:
return False
…
How to Mock a Slow Startup Probe for Fast Testing in Python
This code shows how to replace a slow startup probe's initialization with a mock to make tests run fast and reliably.
import time
from unittest.mock import Mock, patch
class StartupProbe:
def __init__(self, init_time):
self.init_time = init_time
self.ready = False
def initialize(self):
time.sleep(self.init_time)
self.ready = True
return self.ready
def run_startup_probe(probe):
…
How to Mock time.sleep in a Python PreStop Hook
This code simulates a Kubernetes PreStop hook that delays shutdown, then mocks time.sleep to verify the hook logic without real delay.
import subprocess
import sys
import time
from unittest.mock import patch
def pre_stop_hook():
"""Simulate a Kubernetes PreStop hook that sleeps before shutdown."""
print("PreStop hook started: delaying shutdown")
time.sleep(3)
print("PreStop hook completed: ready to shutdown")
if __name__ == "__main_…
How to Smoke Test a Deployment in Python with unittest.mock
Run a post-deploy smoke test by mocking the deployment status check to verify your health-check logic returns PASS/FAIL.
import unittest
from unittest.mock import Mock, patch
class DeploymentService:
def check_status(self):
return "unknown"
def smoke_test_deploy():
service = DeploymentService()
with patch.object(service, "check_status", return_value="healthy") as mock_check:
status = service.check_status()
…
Browse by section
Each section groups closely related Python snippets.
Production deployment patterns — Python code examples
What you will find here
This page collects production deployment patterns snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.