DEV Community

Cover image for Part-90: ๐Ÿš€ Kubernetes Deployment with Declarative Way in Google Kubernetes Engine (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-90: ๐Ÿš€ Kubernetes Deployment with Declarative Way in Google Kubernetes Engine (GCP)

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
Enter fullscreen mode Exit fullscreen mode

d1


๐Ÿ”น 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น 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
Enter fullscreen mode Exit fullscreen mode

d2


๐Ÿ”น 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น 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://<Load-Balancer-Service-IP>
Enter fullscreen mode Exit fullscreen mode

d3

โœ… Now you should see Application Version: 3.0.0 served through your GCP LoadBalancer.

d4


๐Ÿ“Œ 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
Enter fullscreen mode Exit fullscreen mode

d5


โœ… 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

Top comments (0)