一. 部署BBS
-
项目文件导入:
项目放到opt下, 下面的day1130为项目名
将数据转为sql文件放到opt下 -
创建bbs数据库, 并运行sql文件写入数据
mysql> create database bbs charset utf8mb4; mysql> use bbs mysql> ource /opt/bbs.sql
-
修改settings.py的数据库配置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'bbs', 'HOST': "10.0.0.100", 'USER': 'bbs', 'PASSWORD': '123', 'PORT': 3306, } }
-
创建mysql的bbs用户
mysql> grant select,update,delete ,insert on bbs.* to bbs@'10.0.0.%' identified by '123'; 测试: mysql -ubbs -p123 -h10.0.0.100
-
配置nginx
vim /etc/nginx/conf.d/py.conf server { listen 80; server_name 10.0.0.100; client_max_body_size 100M; location /static { alias /opt/day1130/static/; } location /media { alias /opt/day1130/media; } location / { index index.html; include uwsgi_params; uwsgi_pass 127.0.0.1:9091; # 这里配置的day1130是settings.py所在的文件夹.wsgi uwsgi_param UWSGI_SCRIPT day1130.wsgi; # 这里配置的项目根目录的路径 uwsgi_param UWSGI_CHDIR /opt/day1130; } }
-
配置uwsgi
关闭所有已有的uwsgi进程: kill -9 `ps -ef |grep uwsgi|awk {'print $2'}` 写配置文件: vim /opt/day1130/uwsgi.ini [uwsgi] socket = 127.0.0.1:9091 master = true workers = 2 reload-mercy = 10 vacuum = true max-requests = 1000 limit-as = 512 buffer-size = 30000
-
启动uwsgi
uwsgi --ini /opt/day1130/uwsgi.ini
二. ansible的安装与使用
-
安装ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install ansible -y curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
-
创建多台服务器的环境(克隆一台虚拟机代替)
主机关机,克隆机开机,进入克隆机 修改克隆机名为 standby hostnamectl set-hostname standby 修改IP地址 vim /etc/sysconfig/network-scripts/ifcfg-eth0 删除UUID, 改下ip地址 : IPADDR=10.0.0.200 修改host文件 echo "改后的ip 改后的主机名">>/etc/hosts 重启网卡 systemctl restart network
-
秘钥链接的使用
生成秘钥 ssh-keygen 公钥推给另一台机器 ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.200 给自己一个秘钥 ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.200 测试 ssh 10.0.0.200 date
-
将这几个ip放到一个组,方便以后批量调用
vim /etc/ansible/hosts [web] 10.0.0.100 10.0.0.200
-
测试
[root@wzj ~]# ansible web -m ping 显示如下则成功 10.0.0.200 | SUCCESS => { "changed": false, "ping": "pong" } 10.0.0.100 | SUCCESS => { "changed": false, "ping": "pong" } [root@wzj ~]# ansible all -m shell -a "df -h" 显示如下则成功 10.0.0.200 | CHANGED | rc=0 >> 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 47G 9.3G 38G 20% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 10.0.0.100 | CHANGED | rc=0 >> 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 47G 9.3G 38G 20% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 124K 489M 1% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0
ansible playbook自动化批量安装nginx
1. 写剧本
vim playbook_nginx.yml
- 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文件如下
cat default.conf.template
server {
listen {{ http_port }};
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
3. 执行ansible-playbook
检查语法
ansible-playbook --syntax playbook_nginx.yml
模拟执行
ansible-playbook -C playbook_nginx.yml
执行
ansible-playbook playbook_nginx.yml
三. Docker的安装使用
-
安装docker:
curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo 安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 显示Docker的所有版本: yum list docker-ce.x86_64 --showduplicates | sort -r 选择一个版本安装 yum install -y --setopt=obsoletes=0 \ docker-ce-17.03.2.ce-1.el7.centos.x86_64 \ docker-ce-selinux-17.03.2.ce- 1.el7.centos.noarch
-
启动Docker
systemctl daemon-reload systemctl restart docker
-
镜像加速
mkdir -p /etc/docker vim /etc/docker/daemon.json { "registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"] }
-
使用docker装一些其他的环境,用于项目在其他系统上测试
docker pull centos:6.9 docker pull centos:7.5.1804 docker pull nginx
-
查看下了哪些镜像
docker images
-
启用centos6.9镜像
docker run -it centos:6.9
-
查看当前所在镜像
[root@wzj ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core)
-
查询镜像
docker images 所有的镜像 docker images -q 只看镜像ID docker inspect ID/name:tag 镜像的详细信息 docker images --no-trunc 所有的镜像的详细信息删除镜像 docker rmi -f ID/name:tag docker rmi -f `docker images -q` 批量删除所有镜像
-
导入导出镜像
导出: docker save ID/name:tag >/tmp/xxx.tar 导入: docker load -i /tmp/xxx.tar