DEV Community

Cover image for ๐Ÿš€ Deploying Your First App on Kubernetes (With YAML Examples)
zaheetdev
zaheetdev

Posted on

1 1 1 1 1

๐Ÿš€ Deploying Your First App on Kubernetes (With YAML Examples)

Kubernetes can feel overwhelming at first , all the YAML, pods, deployments, services... But once you deploy your first app, things start to click.

In this guide, weโ€™ll walk through deploying a simple Nginx web server to a Kubernetes cluster, using just a few YAML files.


๐Ÿงฑ What Weโ€™re Deploying

Weโ€™ll use:

  • A Deployment โ€“ to manage our pods
  • A Service โ€“ to expose our app
  • Optionally, a NodePort โ€“ to access it from a browser

๐Ÿ“ฆ Step 1: Create a Deployment

Letโ€™s create a deployment to run two replicas of the nginx container.

File: nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f nginx-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Check status:

kubectl get deployments
kubectl get pods
Enter fullscreen mode Exit fullscreen mode

๐ŸŒ Step 2: Expose the App with a Service

To make the app accessible, define a Service.

File: nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode

Check the port:

kubectl get svc nginx-service
Enter fullscreen mode Exit fullscreen mode

Look for the NodePort, e.g., 30007.


๐ŸŒ Step 3: Access the App

  • If you're using Minikube:
minikube service nginx-service
Enter fullscreen mode Exit fullscreen mode
  • If you're using Docker Desktop or a cloud cluster:

Open http://<NODE-IP>:<NODE-PORT> in your browser.


๐Ÿ“œ Bonus: Clean Up

To delete the resources:

kubectl delete -f nginx-deployment.yaml
kubectl delete -f nginx-service.yaml
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Summary

You just:

  1. Created a Kubernetes deployment for Nginx
  2. Exposed it using a service
  3. Accessed it via browser or CLI
  4. Learned the basics of kubectl and YAML syntax

๐Ÿงญ Next Steps

Try:

  • Replacing Nginx with your own app image
  • Using ConfigMaps or Secrets for configuration
  • Exploring Ingress controllers
  • Scaling your deployment with kubectl scale

#kubernetes #devops #k8s #containers #yaml #cloudnative

Warp.dev image

Warp is the highest-rated coding agentโ€”proven by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarklyโ€™s MCP server ๐Ÿ

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

๐Ÿ‘‹ Kindness is contagious

Dive into this insightful article, celebrated by the caring DEV Community. Programmers from all walks of life are invited to share and expand our collective wisdom.

A simple thank-you can make someoneโ€™s dayโ€”drop your kudos in the comments!

On DEV, spreading knowledge paves the way and strengthens our community ties. If this piece helped you, a brief note of appreciation to the author truly counts.

Letโ€™s Go!