In our previous guides, we created Deployments using the imperative way.
Now, letās switch gears and explore the declarative approach using YAML manifests.
This is the production-grade approach, as it ensures configurations are version-controlled, reusable, and consistent.
š Step 01: Copy Templates from ReplicaSet
Weāll start by deleting any existing deployments/services, then create a fresh Deployment and Service using YAML definitions.
š¹ Delete Existing Deployment and Service
kubectl get deployments
kubectl delete deployment my-first-deployment
kubectl get svc
kubectl delete svc my-first-deployment-service
š¹ Create a Deployment Manifest
Weāll create a Deployment definition by modifying our earlier ReplicaSet template.
Changes made:
- kind: Deployment instead of ReplicaSet
- Image version updated to 3.0.0
- All labels/selectors updated to myapp3
š 01-deployment-definition.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp3-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp3
template:
metadata:
name: myapp3-pod
labels:
app: myapp3
spec:
containers:
- name: myapp3-container
image: ghcr.io/stacksimplify/kubenginx:3.0.0
ports:
- containerPort: 80
š¹ Apply Deployment
# Change Directory
cd kube-manifests
# Apply Deployment
kubectl apply -f 01-deployment-definition.yml
# Verify Deployment
kubectl get deploy
kubectl get rs
kubectl get po
š¹ Create a LoadBalancer Service
Now letās expose our Deployment externally using a LoadBalancer Service.
š 02-deployment-LoadBalancer-service.yml
apiVersion: v1
kind: Service
metadata:
name: deployment-loadbalancer-service
spec:
type: LoadBalancer
selector:
app: myapp3
ports:
- name: http
port: 80 # Service Port
targetPort: 80 # Container Port
š¹ Apply Service
kubectl apply -f 02-deployment-LoadBalancer-service.yml
# List Services
kubectl get svc
# Get Public IP (assigned by GCP LoadBalancer)
kubectl get nodes -o wide
# Access Application
http://
ā Now you should see Application Version: 3.0.0 served through your GCP LoadBalancer.
š Step 02: Clean-Up Kubernetes Deployment and Service
Once done testing, letās clean up the resources.
# Delete Deployment
kubectl delete -f 01-deployment-definition.yml
# Delete LoadBalancer Service
kubectl delete -f 02-deployment-LoadBalancer-service.yml
ā
Recap
In this guide, we:
- Converted a ReplicaSet template into a Deployment manifest
- Exposed it using a LoadBalancer Service in GCP
- Verified our application running with 3 replicas
- Cleaned up resources after testing
š Declarative manifests are the best practice for Kubernetes because they enable repeatability, GitOps workflows, and team collaboration.
š Thanks for reading! If this post added value, a like ā¤ļø, follow, or share would encourage me to keep creating more content.
ā Latchu | Senior DevOps & Cloud Engineer
āļø AWS | GCP | āøļø Kubernetes | š Security | ā” Automation
š Sharing hands-on guides, best practices & real-world cloud solutions