项目部署与ansible自动化部署
一、项目部署
| |
| |
| |
| |
| |
| |
| |
| mysql> create database bbs charset utf8mb4; |
| |
| |
| mysql> use bbs |
| mysql> source /opt/bbs.sql |
| |
| |
| unzip BBS.zip |
| |
| |
| [root@web01 ~] |
| [root@web01 BBS] |
| [root@web01 BBS] |
| |
| |
| ALLOWED_HOST = ['*'] |
| |
| DATABASES = { |
| 'default': { |
| 'ENGINE': 'django.db.backends.mysql', |
| 'HOST': '10.0.0.100', |
| 'PORT': 3306, |
| 'USER': 'root', |
| 'PASSWORD': '123', |
| 'NAME': 'time8', |
| } |
| } |
| |
| |
| mysql> grant select,insert,delete,update on bbs.* to bbs@'10.0.0.%' identified by '123'; |
| |
| |
| |
| [root@web01 BBS] |
| |
| server { |
| listen 80; |
| server_name 10.0.0.100; |
| client_max_body_size 100M; |
| |
| location /static { |
| alias /opt/BBS/static/; |
| } |
| |
| location /media { |
| alias /opt/BBS/media; |
| } |
| |
| location / { |
| index index.html; |
| include uwsgi_params; |
| uwsgi_pass 127.0.0.1:9090; |
| uwsgi_param UWSGI_SCRIPT BBS.wsgi; |
| uwsgi_param UWSGI_CHDIR /opt/BBS; |
| } |
| } |
| |
| |
| kill -9 `ps -ef |grep uwsgi|awk {'print $2'}` |
| |
| |
| [root@web01 BBS] |
| |
| [uwsgi] |
| socket = 127.0.0.1:9090 |
| master = true |
| workers = 2 |
| reload-mercy = 10 |
| vacuum = true |
| max-requests = 1000 |
| limit-as = 512 |
| buffer-size = 30000 |
| |
| |
| uwsgi --ini uwsgi.ini & |
| |
| |
| systemctl restart nginx |
| 数据库中删除数据存在安全隐患,因此可以加一个字段state; |
| 如果state为1,表示没用的数据;如果state为0,表示可以访问; |
| 只要在查询数据的时候,查询条件加一条state==1,即可访问安全的没有被“删除”的数据 |
二、ansible自动化部署(python自动化运维)
1、安装ansible
| wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo |
| |
| curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo |
| |
| yum install ansible -y |
2、ansible例子
(1)克隆虚拟机
(2)在虚拟机上修改克隆机配置
| |
| hostnamectl set-hostname standby |
| |
| |
| vim /etc/sysconfig/network-scripts/ifcfg-eth0 |
| |
| IPADDR=10.0.0.200 |
| |
| |
| |
| vim /etc/hosts |
| |
| 10.0.0.200 standby |
| |
| |
| systemctl restart network |
(3)利用SSH连接管理机与被管理机——管理机生成秘钥并推送公钥
| 验证方式: |
| (1)用户+密码(PAM) |
| (2)秘钥验证(公钥--钥匙,私钥--锁) |
| 通过秘钥对实现,需要将公钥分发到各节点 |
| # 1.生成秘钥对 |
| [root@web01 ~]# ssh-keygen |
| |
| # 2.推送公钥给被管理机 |
| [root@web01 ~]# for i in {1..12};do ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.$i;done # 被管理机数量多,利用循环分发公钥 |
| [root@web01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.200 # 被管理机少,直接推送公钥 |
| |
| # 3.测试推送 |
| [root@web01 ~]# ssh 10.0.0.200 date # date表示连接,成功并退出连接 |
(4)配置被管理的主机清单
| [root@web01 ~] |
| |
| [web] |
| 10.0.0.100 |
| 10.0.0.200 |
(5)测试ansible
| |
| [root@web01 ~] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [root@web01 ~] |
3、ansible自动化部署nginx
(1)配置YAML文件
| [root@web01 ~] |
| |
| - hosts: web |
| remote_user: root |
| vars: |
| http_port: 80 |
| tasks: |
| - name: Add Nginx Yum Repository |
| yum_repository: |
| name: nginx |
| description: Nginx Repository |
| baseurl: http://nginx.org/packages/centos/7/$basearch/ |
| gpgcheck: no |
| |
| - name: Install Nginx Server |
| yum: |
| name=nginx state=present |
| |
| - name: Configure Nginx Server |
| template: src=./default.conf.template dest=/etc/nginx/conf.d/default.conf |
| notify: Restart Nginx Server |
| |
| - name: Start Nginx Server |
| service: name=nginx state=started enabled=yes |
| |
| handlers: |
| - name: Restart Nginx Server |
| service: name=nginx state=restarted |
(2)配置 default.conf.template 文件
| |
| [root@web01 ~] |
| |
| server { |
| listen {{ http_port }}; |
| server_name localhost; |
| |
| location / { |
| root /usr/share/nginx/html; |
| index index.html index.htm; |
| } |
| } |
(3)执行 ansible-playbook
| |
| [root@web01 ~] |
| |
| |
| [root@web01 ~] |
| |
| |
| [root@web01 ~] |
(4)测试部署
博客内容仅供参考,部分参考他人优秀博文,仅供学习使用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)