部署 node 应用
步骤
- 创建一个 Linux 服务器
编写 Ansible Playbook
- 在服务器上安装 node & npm
- 复制节点工件并解压缩
- 启动应用
- 检查应用是否成功运行
- 使用的模块
npm node command copy unarchive async shell register debug user
- 其他
become=true ,默认是 false
become_user=为具有所需权限的用户设置 , 默认是 root
启动文件
- 获取 node 项目
# 将node项目从windos上复制到你的管理主机上,在windos上运行命令:
git clone https://gitlab.com/devops-bootcamp3/simple-nodejs
scp -r .\simple-nodejs\ user@ip:~
- 目录结构
~/simple-node.js
~/ansible/
|--- hosts
|--- my-playbook.yaml
|--- ansible.cfg
- 文件代码
# hosts
[droplet]
192.168.136.136 # 1. 节点
[droplet:vars]
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible_user=root
# my-playbook.yaml
---
- name: Configure nginx web server
hosts: droplet
tasks:
- name: install nginx server
yum:
name: nginx
state: latest
- name: start nginx server
service:
name: nginx
state: stopped
# ansible.cfg
[default]
host_key_checking = False
编写 playbook
第一种方法: 使用 copy 和 unarchive 两个模块
- 复制到受控主机
- 解压
# ansible/deploy-node.yaml
---
- name: Install node and npm
hosts: 192.168.136.136
tasks:
- name: Update yum repo and cache
yum: update_cache=yes
- name: Install nodejs npm
yum:
pkg:
- nodejs
- npm
- name: Deplay nodejs app
hosts: 192.168.136.136
tasks:
# -------与第二种方法不同之处,建议使用第二种代码更少-----------
- name: Copy nodejs folder to a server
copy:
src: /home/slx/simple-nodejs/nodejs-app-1.0.0.tgz
dest: /root/app-1.0.0.tgz
- name: Unpack the node file
unarchive:
src: /root/app-1.0.0.tgz
dest: /root/package
remote_src: yes
# ---------------------
- name: Install dependencies
npm:
path: /root/package
- name: Start the application
command:
chdir: /root/package/app
cmd: node server
# 在受控主机查看 ps -aux | grep node
async: 1000 # 异步运行
poll: 0
- name: Ensure app is running # 在管理机上检查受控主机app是否运行成功
shell: ps aux | grep node
register: app_status # 返回结果存入变量中
- debug: msg={{app_status.stdout_lines}} # 打印信息
ansible-playbook -i hosts deplay-node.yaml
第二种方法: 仅仅使用 unarchive 模块
- 直接将本地文件解压到受控主机
# ansible/deploy-node.yaml
# 不指定 remote_src会在本地寻找路径
---
- name: Install node and npm
hosts: 192.168.136.136
tasks:
- name: Update yum repo and cache
yum: update_cache=yes
- name: Install nodejs npm
yum:
pkg:
- nodejs
- npm
- name: Deplay nodejs app
hosts: 192.168.136.136
tasks:
- name: Unpack the node file
unarchive:
src: /home/slx/simple-nodejs/nodejs-app-1.0.0.tgz
dest: /root/
- name: Install dependencies
npm:
path: /root/package
- name: Start the application
command: # 执行命令
chdir: /root/package/app
cmd: node server
# 在受控主机查看 ps -aux | grep node
async: 1000 # 异步运行
poll: 0
- name: Ensure app is running # 在管理机上检查受控主机app是否运行成功
shell: ps aux | grep node
register: app_status # 返回结果存入变量中
- debug: msg={{app_status.stdout_lines}} # 打印信息
ansible-playbook -i hosts deplay-node.yaml
优化: 这两种都是 root 角色来执行命令,会有安全隐患
- 创建一个新用户
- 用这个用户运行应用
# ...同上
- name: Create new linux user for node app
hosts: 192.168.136.136
tasks:
- name: cretate linux user
user:
name: nana
comment: nana Admin
group: admin
- name: Deplay nodejs app
become: true # 开启
become_user: nana #你想设置的用户名称
# ...同上
# 将 [root]替换为 [home/nana] , 即将root用户替换为普通用户
评论 (0)