Kubernetes Fundamentals: Deploying Your First Application
Kubernetes, more commonly referred to as K8s, is a leading platform used for the management of containerized applications at scale. The increasing demand to deploy and scale applications fast and reliably makes Kubernetes the most powerful solution, by automating the deployment, scaling, and operations of application containers across clusters of hosts.
Setting Up the Kubernetes Environment
To get started with Kubernetes, we will need a local Kubernetes environment, so – Minikube is an ideal tool for this. Minikube is a tool that allows users to run Kubernetes (k8s) on their local machine. Once installed, we can start Minikube with :
minikube start
Kubernetes Components
These are certain main components of Kubernetes :
- Pod: The smallest unit in Kubernetes, which can contain one or more containers.
- Node: physical or virtual machine running a pod.
- Deployment: It declares the desired state of running replicas for pods.
- Kubelet: It is a command line utility that enables users to communicate with Kubernetes clusters.
- Service: It exposes our application making it accessible within or outside the cluster.
Create and Deploy a Simple Application
Create a simple application such as a basic Node.js API or Python Flask app. Add a Dockerfile to containerize the app
FROM node: latest
WORKDIR /app
COPY . .
RUN npm install
CMD [“node”, “server.js”]
Build the Docker image and push it to a container registry like Docker Hub where Kubernetes can pull it:
docker build -t your-username/your-app:latest .
docker push your-username/your-app
Writing the Kubernetes Manifest Files
To deploy the app, create two YAML files for defining the deployment and the service.
Deployment file
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
– name: my-app
image: your-username/your-app
ports:
– containerPort: 3000
Service.yaml:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: NodePort
selector:
app: my-app
ports:
– port: 80
targetPort: 3000
nodePort: 3001
Apply these manifests with the following commands :
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Deploying the Application to Kubernetes
Deploying an application on Kubernetes allows you to take advantage of Kubernetes’ scaling and self-healing features, ensuring that your application is both resilient and ready for growth. Once familiar with these basics, you can explore more advanced features like Ingress controllers, Helm charts, and monitoring to further optimize your Kubernetes deployments.
With Kubernetes, you can easily scale your application up or down to meet demand. To increase the replicas of your application, use:
kubectl scale deployment my-app-deployment –replicas=3
Accessing the Application
minikube service my-app-service
This will open your app in a browser, allowing you to verify it’s up and running.
Conclusion
Kubernetes automates the deployment, scaling, and management of applications, allowing developers to focus on building rather than managing infrastructure.