安装Docker、kubernetes、node.js。
1. 创建Node.js应用程序
var http = require('http');
var handleRequest = function(request, response) {
console.log('Received request for URL: ' + request.url);
response.setHeader('content-type', 'text/html;charset=utf-8')
response.writeHead(200);
response.end("我爱唱歌...!");
};
var www = http.createServer(handleRequest);
www.listen(8080);
2. 编写镜像文件Dockerfile
FROM node:16.9.1-slim
EXPOSE 8080
COPY server.js .
CMD node server.js
3. 在dockerfile目录下打包镜像
docker build -t hello-node:v1 .
4. 编写k8s中Deployment的配置文件deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-deployment
spec:
selector:
matchLabels:
app: node
replicas: 1 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
template:
metadata:
labels:
app: node
spec:
containers:
- name: node
image: hello-node:v2
ports:
- containerPort: 8080
5. 创建k8s的Deployment
kubectl apply -f deployment.yaml
6. 创建k8s的Service服务
kubectl expose deployment node-deployment --type=LoadBalancer
7. 浏览器输入localhost:8080就可以啦。
8. 更新应用
kubectl set image deployment/node-deployment node=hello-node:v2
# node=hello-node:v2, 将容器node更换镜像为hello-node:v2