As someone who regularly works with Kubernetes, I’ve come to appreciate the efficiency of review apps. Review apps help my team quickly validate features and fixes in isolated environments, without the overhead or risk of deploying directly to production or staging. Over time, I’ve found a few techniques to help streamline this process, making the setup of these environments almost effortless. Here are five of my favorite approaches:
1. Centralize your Kubernetes YAML templates.
Using a central set of YAML templates ensures consistency and significantly reduces manual maintenance. Here’s a basic namespace template that I use as the foundation for creating namespaces dynamically:
apiVersion: v1
kind: Namespace
metadata:
name: app-name
labels:
name: app-name
When you create your deployments, simply reference the namespace directly:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-name-api
namespace: app-name
spec:
replicas: 1
template:
metadata:
labels:
app: app-name-api
spec:
containers:
- name: api
image: app-name-api:branch
2. Use dynamic Namespace and URL management.
Leveraging basic scripting tools like sed
, you can effortlessly replace namespaces and URLs within your YAML files, making each environment unique:
sed "s/app-name/$REVIEW_NAMESPACE/g" namespace.yaml | kubectl apply -f -
Similarly, adjusting ingress URLs dynamically becomes straightforward:
sed "s/app-name.local/$REVIEW_URL/g" ingress.yaml | kubectl apply -f -
3. Automate setup with scripts.
Automating the entire setup through scripts saves a huge amount of time and ensures consistency. I typically use a script like create-namespaced-app.sh
, which handles everything from namespace creation to SSL and app deployments:
# Example script usage
./create-namespaced-app.sh review-branch app-name-api:branch app-name-ui:branch
Inside this script, common Kubernetes commands like namespace creation, SSL management, and deployments are handled seamlessly:
# Create namespace
echo "Creating namespace $NAMESPACE..."
cat namespace.yaml | \
sed "s|name: app-name|name: $NAMESPACE|" | \
kubectl apply -f -
# Deploy API
echo "Deploying API..."
cat deployment.yaml | \
sed "s|namespace: app-name|namespace: $NAMESPACE|" | \
sed "s|registry.wherever:.*|$API_IMAGE|" | \
kubectl apply -f -
4. Try ephemeral ConfigMaps for flexible configuration.
For review apps, using ephemeral ConfigMaps is a smart way to handle temporary configurations, like feature flags or environment-specific settings. These ConfigMaps can default to settings from environments like staging, but allow overrides for more specific scenarios:
if [ -f "config-review.yaml" ]; then
kubectl apply -f config-review.yaml --namespace=$REVIEW_NAMESPACE
fi
if [ -f "$REVIEW_NAMESPACE-config-review.yaml" ]; then
kubectl apply -f $REVIEW_NAMESPACE-config-review.yaml --namespace=$REVIEW_NAMESPACE
fi
Note: For stable environments such as development, staging, or production, you’ll typically want persistent ConfigMaps to maintain version control and stability.
5. Ensure isolation and rapid iteration.
Dedicated namespaces and ephemeral configurations mean that your review apps remain isolated. This reduces risks associated with testing and helps your team iterate quickly and safely.
Streamlining Kubernetes Review Apps
By adopting these five techniques, I’ve significantly streamlined how my team creates and manages Kubernetes review apps. Centralized YAML templates, dynamic scripting, automation, ephemeral configuration management, and maintaining isolation have become invaluable parts of our development workflow. I hope these strategies can help enhance your Kubernetes practices too, making review processes quicker, safer, and more reliable.