Deploying Your First Application
1. Introduction to Application Deployment in Kubernetes
Kubernetes simplifies deploying, scaling, and managing containerized applications. This guide will walk you through deploying your first application.
2. Containerizing Your Application
Basics of Containerization: Containers package an application and its dependencies together. They ensure that the application works seamlessly in any environment.
Creating a Docker Image:
- Install Docker.
- Create a
Dockerfilethat specifies the base image and the steps to build your application. Example for a simple Node.js app:FROM node:14
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "app.js"] - Build the Docker image:
docker build -t my-node-app . - Push the image to a container registry (like Docker Hub).
3. Writing a Deployment YAML File
Understanding the Deployment Object: A Deployment manages a set of identical Pods. It ensures they are updated and scaled.
Creating the YAML File:
deployment.yamlexample for the Node.js app:apiVersion: apps/v1
kind: Deployment
metadata:
name: my-node-app
spec:
replicas: 2
selector:
matchLabels:
app: my-node-app
template:
metadata:
labels:
app: my-node-app
spec:
containers:
- name: my-node-app
image: [Your Docker Hub username]/my-node-app
ports:
- containerPort: 80
4. Deploying the Application
Using kubectl:
- Apply the Deployment:
kubectl apply -f deployment.yaml. - Verify the Deployment:
kubectl get deployments.
5. Accessing the Application
Exposing the Application:
- Create a Service to expose the app.
service.yamlexample:apiVersion: v1
kind: Service
metadata:
name: my-node-app-service
spec:
type: NodePort
selector:
app: my-node-app
ports:
- protocol: TCP
port: 80
nodePort: 30007- Apply the Service:
kubectl apply -f service.yaml.
Types of Services:
- ClusterIP: Default, internal communication.
- NodePort: Exposes the app outside the cluster by adding a port on each node.
- LoadBalancer: Integrates with cloud-based load balancers.
6. Scaling and Updating the Application
Scaling the Application:
- Update the
replicasin thedeployment.yamland apply the changes.
Updating the Application:
- Change the application code, rebuild the Docker image, push it, and update the image in the deployment file. Kubernetes will perform a rolling update.
7. Cleanup
Deleting Resources:
- Delete the Deployment:
kubectl delete deployment my-node-app. - Delete the Service:
kubectl delete service my-node-app-service.