LoginSignup
0
0

More than 1 year has passed since last update.

Docker create image and run with Kubernetes

Posted at

Docker

  1. 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);
  1. create Dockerfile
FROM node:16
ADD app.js /app.js
ENTRYPOINT ["node", "app.js"]
  1. Directory structure
--
 -- app.js
 -- Dockerfile
  1. Create Docker Image
    docker build -t tellhost . Run this command in the same directory with Dockerfile
  2. Show all images
    docker images
  3. 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

  1. show all running containers
    docker ps
    docker ps -a show all running and stopeed
  2. Show the detail of one container
    docker inspect tellhost-container
  3. 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).

  1. Stop container
    docker stop tellhost-container
  2. Remove container
    docker remove tellhost-container

Kubernetes

  1. show server status
    kubectl cluster-info
  2. show all nodes
    kubectl get nodes
  3. show detail of one node
    kubectl describe node your-node-name
  4. 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.
  5. expose the pod
    kubectl expose pod tellhost --type=LoadBalancer --name tellhost-http
  6. deploy application
    kubectl create deployment tellhost2 --image=toka9988/tellhost
  7. expose deployment
    kubectl expose deployment tellhost2 --type=LoadBalancer --port 8080
  8. show all deployment
    kubectl get deployments
  9. show all pods
    kubectl get pods
  10. scale out
    kubectl scale deployment tellhost2 --replicas=3
  11. show pod's detail
    kubectl describe pod your-pod-name
  12. show services
    kubectl get services
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0