Sunday, September 1, 2019

Running a simple nodejs container on k8s

Create a docker image sample

Before launching a container based on the image, we need to create a pod definition.
-A pod describes an application running on K8s.
-A pod can contain one or more tightly couples containers, that make up the app.
 - Those apps can easily communicate with each other using their local port numbers.

create a pod.
File: pod-helloworld.yml with the pod definition
-------



apiVersion: v1
kind: Pod
metadata:
 name: nodehelloworld.example.com
 labels:
   app: helloworld
 spec:
   containers:
   - name: k8s-demo
     image: wardviaene/k8s-demo
     ports:
     - containerPort: 3000

kubectl get node   --> to see the nodes up.

Use kubectl to create the pod on the k8s cluster:
 kubectl create -f k8s-demo/pod-helloworld.yml

 After deployment, check with below command.

kubectl get pod
kubectl describe pod <name_of_pod>

To use this pod, mulitple ways are there.:
1. Port forwarding(for quick test running or not):
kubectl port-forward nodehelloworld.example.com 8081:3000

curl localhost:8081

2. to create a service, for aws use this, for minikube just expose the pod.

kubectl expose pod nodehelloworld.example.com --type=NodePort --name nodehelloworld-service

3.Now we need url to connect, for aws, use the master node and the port exposed.For minikube, run below command which gives us the url.

minikube service nodehelloworld-serevice --url

This may give a different port but redirection will be done internally.

4. kubectl get service  -> will show running services.





5. kubectl describe sevice <service_name>


This will give more info about service like endpoints/nodeport through which we can connect from other pods.
From endpoint:
create a container and connect from it using telnet to this endpoint ip and port.


Some useful commands:

 

No comments:

Post a Comment