An Introduction to Kubernetes: Deploying Your First Application

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. If you're looking to deploy your first application using Kubernetes, this guide will walk you through the process step by step.
Key Concepts in Kubernetes
Before diving into deployment, it's essential to understand some key Kubernetes concepts:
Pods
A pod is the smallest deployable unit in Kubernetes. It encapsulates one or more containers, storage, and options that define how the containers run. Think of a pod as a wrapper around your application container.
Services
A service in Kubernetes is an abstract way to define a set of pods and how they should be accessed. Services provide stable networking for pods, which are otherwise ephemeral and can change IP addresses.
Deployments
A deployment is a resource that manages the lifecycle of a pod. It ensures that the correct number of pods are running and handles updates, rollbacks, and scaling.
Step 1: Setting Up Your Environment
To get started, you need a Kubernetes cluster. If you're working locally, you can use Minikube, a single-node cluster for testing purposes.
Install Minikube:
# On macOS using Homebrew brew install minikube
Start Minikube:
minikube start
Install kubectl:
# On macOS using Homebrew brew install kubectl
Step 2: Creating a Simple Web Application
For this tutorial, we'll create a basic Node.js web server.
Create a directory for your project:
mkdir my-node-app cd my-node-app
Create an
index.js
file:const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello from Kubernetes!'); }); app.listen(8080, () => { console.log('Server running on port 8080'); });
Create a
Dockerfile
:FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 8080 CMD ["node", "index.js"]
Build the Docker image:
docker build -t my-node-app:v1 .
Push the image to Docker Hub (or your preferred registry):
docker push my-node-app:v1
Step 3: Creating a Kubernetes Deployment
Create a deployment.yaml
file to define how your application should be deployed.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-container
image: my-node-app:v1
ports:
- containerPort: 8080
Explanation:
apiVersion: apps/v1
: Specifies the version of the Kubernetes API to use.kind: Deployment
: Indicates that we are creating a deployment.metadata.name
: The name of the deployment.spec.replicas: 3
: Ensures three copies of the pod are running.template.spec.containers
: Defines the container configuration.
Step 4: Creating a Service
Create a service.yaml
file to expose your deployment to the internet.
apiVersion: v1
kind: Service
metadata:
name: my-node-app-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: my-node-app
Explanation:
spec.type: LoadBalancer
: Exposes the service externally.spec.ports.port: 80
: The port on which the service will be exposed.spec.selector.app: my-node-app
: Routes traffic to the pods with the labelapp: my-node-app
.
Step 5: Deploying Your Application
Apply the deployment:
kubectl apply -f deployment.yaml
Apply the service:
kubectl apply -f service.yaml
Check the status:
kubectl get pods kubectl get services
Access your application:
minikube tunnel
Visit
http://localhost
in your browser to see your application in action.
Step 6: Cleaning Up
When you're done, delete the deployment and service:
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml
Key Takeaways
- Pods: The smallest unit of deployment.
- Services: Provide stable networking for pods.
- Deployments: Manage the lifecycle of pods.
By following this guide, you've successfully deployed a simple web application on Kubernetes. This is just the beginning; Kubernetes offers many more features for scaling, self-healing, and continuous updates.
Corrected Code Snippet
Here's a corrected version of the deployment file, ensuring all necessary fields are included:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-container
image: my-node-app:v1
ports:
- containerPort: 8080
This deployment ensures one instance of your application is running, ready to scale as needed.
Subscribe to my newsletter
Read articles from Sabin Chapagain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
