Deploying a Simple Node.js Application on Kubernetes
We will deploy a basic Node.js application on a Kubernetes cluster using the KodeKloud engineer platform.
Create a Nodeapp Deployment
We will use the imperative approach and save it to a file.
kubectl create deployment nodeapp --replica=2 \
--image=gcr.io/kodekloud/centos-ssh-enabled:node \
--port 8080 --dry-run=client -o yaml > nodeapp-deployment.yaml
Final deployment object
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nodeapp
name: nodeapp
spec:
replicas: 2
selector:
matchLabels:
app: nodeapp
strategy: {}
template:
metadata:
labels:
app: nodeapp
spec:
containers:
- image: gcr.io/kodekloud/centos-ssh-enabled:node
name: centos-ssh-enabled
ports:
- containerPort: 8080
selector: Labels to identify pods managed by our deployment. labels: Labels for the pod template (app: nodeapp).
Create a Nodeapp nodeport Service
We will use the imperative approach, and then save it to a manifest file for some changes.
kubectl create service nodeport nodeapp --tcp 8080:8080 \
--dry-run=client -o yaml > nodeapp-service.yaml
Final service object
apiVersion: v1
kind: Service
metadata:
labels:
app: nodeapp
name: nodeservice
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
nodePort: 30012
selector:
app: nodeapp
type: NodePort
selector: Labels to select pods for exposing the service. Here, pods with the label app: nodeapp will be included