Docker
- Create a test Nodes.js app.js
const http = require('http');
const os = require('os');
console.log("Tellhost server starting...");
const handler = function(request, response) {
console.log("Received request from " + request.connection.remoteAddress);
response.writeHead(200);
response.end("You've hit " + os.hostname() +" from " + request.connection.remoteAddress+ "\n");
};
const www = http.createServer(handler);
www.listen(8080);
- create Dockerfile
FROM node:16
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]
- Directory structure
--
-- app.js
-- Dockerfile
- Create Docker Image
docker build -t tellhost .Run this command in the same directory with Dockerfile - Show all images
docker images - Run the image
docker run --name tellhost-container -p 8080:8080 -d tellhost
-d: The container will be detached from the console (-d flag), which means it will
run in the background.
-p 8080:8080 map 8080 on local machine to 8080 inside the container
- show all running containers
docker ps
docker ps -ashow all running and stopeed - Show the detail of one container
docker inspect tellhost-container - Run bash shell inside the container
docker exec -it tellhost-container bash
-i, which makes sure STDIN is kept open. You need this for entering commands into the shell.
-t, which allocates a pseudo terminal (TTY).
- Stop container
docker stop tellhost-container - Remove container
docker remove tellhost-container
Kubernetes
- show server status
kubectl cluster-info - show all nodes
kubectl get nodes - show detail of one node
kubectl describe node your-node-name - run one image
kubectl run tellhost --image toka9988/tellhost --port=8080
It doesn't map the port on local machine.So you need to login the container. - expose the pod
kubectl expose pod tellhost --type=LoadBalancer --name tellhost-http - deploy application
kubectl create deployment tellhost2 --image=toka9988/tellhost - expose deployment
kubectl expose deployment tellhost2 --type=LoadBalancer --port 8080 - show all deployment
kubectl get deployments - show all pods
kubectl get pods - scale out
kubectl scale deployment tellhost2 --replicas=3 - show pod's detail
kubectl describe pod your-pod-name - show services
kubectl get services