This guide will help you deploy a Node.js API and Redis cache using Kubernetes (K8s).
- Docker installed on your local machine.
- Kubernetes (K8s) cluster set up.
- Build the Docker image for the API:
docker build -t <your-registry>/api:v1 .
- Push the API image to your local Docker registry:
docker push <your-registry>/api:v1
- Build the Docker image for Redis cache:
docker build -t <your-registry>/redis:v1 .
- Push the Redis image to your local Docker registry:
docker push <your-registry>/redis:v1
- Create a deployment YAML file named api-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 1
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: <your-registry>/api:v1
ports:
- containerPort: 1337
- Apply the API deployment to the Kubernetes cluster:
kubectl apply -f api-deployment.yaml
1.Create a deployment YAML file named redis-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-deployment
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: <your-registry>/redis:v1
ports:
- containerPort: 6379
- Apply the Redis deployment to the Kubernetes cluster:
kubectl apply -f redis-deployment.yaml
- Create a service YAML file named api-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: api
ports:
- protocol: TCP
port: 80
targetPort: 1337
- Create an Ingress YAML file named api-ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api-ingress
spec:
rules:
- host: <your-api-domain.com>
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- Apply the API Ingress resource to define the routing rules:
kubectl apply -f api-ingress.yaml
- Check the status of the API and Redis deployments:
kubectl get deployments
- Verify that the API and Redis pods are running:
kubectl get pods
- Check the status of the Ingress resource:
kubectl get ingress