简述
本文中,将使用nginx+docker来搭建iarc-app的集群。
源代码
iarc-app
环境
IDE: SublimeText3
Env: Nodejs, NPM
WebServer: Nginx
Container: Docker
开发
以下步骤在CentOS7中完成
创建Dockerfile
在iarc-app根目录下新建文件’Dockerfile’,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| FROM node:9.11.1-alpine
# install simple http server for serving static content RUN npm install -g http-server
# make the 'app' folder the current working directory WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available) COPY package*.json ./
# install project dependencies # RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder) COPY . .
# build app for production with minification RUN npm run build
EXPOSE 8080 CMD [ "http-server", "dist" ]
|
构建docker镜像
docker build -t iarc/iarc-app .
运行2个docker容器
1 2
| $ docker run -it -p 8001:8080 --rm --name iarc-app-1 iarc/iarc-app $ docker run -it -p 8002:8080 --rm --name iarc-app-2 iarc/iarc-app
|
安装nginx
1 2 3
| $ npm install nginx -y # 安装 $ systemctl enable nginx # 开机自启 $ systemctl restart nginx # 配置后重启nginx
|
配置负载均衡
配置文件路径是 /etc/nginx/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| http { upstream iarc_app { server 127.0.0.1:8001; server 127.0.0.1:8002; }
server { listen 8080; location / { proxy_pass http://iarc_app; } } ... }
|
开放8080端口
1 2
| $ firewall-cmd --zone=public --add-port=8080/tcp --permanent $ firewall-cmd --reload
|
调试
访问
本地使用curl localhost:8080来验证,外部使用http://192.168.56.102:8080来访问
问题
由于SELinux的影响,导致访问时报错502 Bad Gateway,检查日志/var/log/nginx/error.log后发现提示如下内容:
1
| (13: Permission denied) while connecting to upstream:[nginx]
|
解决方案是:setsebool -P httpd_can_network_connect 1
部署
无
参考