1. 程式人生 > >OMserver自動化運維平臺搭建及測試

OMserver自動化運維平臺搭建及測試

Python ansible uwsgi Django 自動化運維

本文基於《Python自動化運維 技術與最佳實踐》第十三章內容“從零開始打造B/S自動化運維平臺”。

參考鏈接為作者劉天斯個人博客:

https://blog.liuts.com/post/245/

https://blog.liuts.com/post/246/

https://github.com/yorkoliu/pyauto


平臺簡介:OMServer是本平臺的名稱。OMServer實現了一個集中式的Linux集群管理基礎平臺,提供了模塊擴展的支持,可以隨意添加集群操作任務模塊,服務器端模塊支持前端HTML表單參數動態定制,可靈活實現日常運維遠程操作、文件分發等任務;在安全方面,采用加密(RC4算法)指令傳輸、操作日誌記錄、分離Web Server與主控設備等。在用戶體驗方面,采用前端異步請求,模擬Linux終端效果接收返回串。任何人都可以根據自身的業務特點對OMServer平臺進行擴展,比如與現有資產平臺進行對接,或整合到現有的運維平臺中。


系統架構設計:OMServer平臺采用三層設計模式,第一層為Web交互層,采用了Django+prototype.js+MySQL實現,服務器端采用了Nginx+uwsgi構建高效的Web服務;第二層為分布式計算層,采用rpyc分布式計算框架實現,作為第一層與第三層的數據交互及實現主控端物理分離,提高整體安全性,同時具備第三層的多機服務的能力;第三層為集群主控端服務層,支持Saltstack、Ansible、Func等平臺。架構圖:

技術分享圖片


主機
ip
操作系統
軟件
備註
web
172.27.9.17
Centos7.3.1611Python2.7.5、Django1.4.9、uwsgi2.0.4、mysql5.7.22、rpyc3.2.3、nginx1.12.2、setuptools0.6c11關閉防火墻和selinux
server172.27.9.23
Centos7.3.1611Python2.7.5、rpyc3.2.3、setuptools39.1.0、ansible2.3.1.0-1關閉防火墻和selinux
agent01
172.27.9.19
Centos7.3.1611/
被控主機
agent02
172.27.9.22
Centos7.3.1611/
被控主機


Web端搭建

安裝包準備:

鏈接:https://pan.baidu.com/s/1Y6nYv3L9udEGsIsOma2Vzg 密碼:048j

[root@web ~]# mkdir /home/apps

將apps.zip上傳至/home/apps並解壓


1.nginx部署

參考文檔:http://blog.51cto.com/3241766/2094315

[root@web ~]# yum -y install gcc-c++
[root@web ~]# yum  -y install  pcre pcre-devel
[root@web ~]# yum -y install zlib zlib-devel
[root@web ~]# yum -y install wget
[root@web ~]# wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
[root@web ~]# tar -zxvf nginx-1.12.2.tar.gz
[root@web ~]# groupadd nginx
[root@web ~]# useradd -g nginx -d /home/nginx nginx
[root@web ~]# passwd nginx
[root@web ~]# cd nginx-1.12.2
[root@web nginx-1.12.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx
[root@web nginx-1.12.2]# make && make install
[root@web nginx-1.12.2]# cd /usr/local/nginx/sbin/
[root@web sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx


2.mysql數據庫安裝

[root@web ~]# rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@web ~]# yum -y install mysql-community-server
[root@web ~]# systemctl start mysqld.service
[root@web ~]# systemctl enable mysqld.service

修改密碼:

[root@web ~]# cat /var/log/mysqld.log | grep password
2018-05-08T09:07:09.591079Z 1 [Note] A temporary password is generated for root@localhost: Ipuq?4#lWyo+
[root@web ~]# mysql -uroot -p
Enter password:

輸入密碼"Ipuq?4#lWyo+",重置密碼:

mysql> set password = password('Mysql123!');

創建數據庫OMServer和omserver_user用戶並授權:

mysql> create database OMServer character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.01 sec)
mysql> grant all privileges on OMServer.* to omserver_user@'%' identified by 'Omserver_user123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)

導入表數據:

[root@web apps]# cd /home/apps/
[root@web apps]# cat OMServer.sql |mysql -uomserver_user -pOmserver_user123! -D OMServer
mysql: [Warning] Using a password on the command line interface can be insecure.

查看導入:

使用SQLyog Community數據庫連接工具查看:

技術分享圖片

技術分享圖片

3.MySQL-python庫安裝

MySQL-python是Python訪問MySQL數據庫的第三方模塊庫

setuptools模塊安裝:

[root@web apps]# tar -xvf setuptools-0.6c11.tar.gz
[root@web apps]# cd setuptools-0.6c11
[root@web setuptools-0.6c11]# python setup.py build
[root@web setuptools-0.6c11]# python setup.py install

MySQL-python模塊安裝:

[root@web apps]# cd MySQL-python-1.2.5
[root@web MySQL-python-1.2.5]# python setup.py install


4.rpyc模塊安裝

rpyc用於平臺與主控端做數據通訊交互

[root@web apps]# cd rpyc-3.2.3
[root@web rpyc-3.2.3]# python setup.py install


5.uwsgi模塊安裝

uwsgi是一個快速的、純C語言開發的、自維護、對開發者友好的WSGI服務器,旨在提供專業的Python web應用發布和開發。

[root@web apps]# cd uwsgi-2.0.4
[root@web uwsgi-2.0.4]# make

安裝成功:

技術分享圖片

[root@web uwsgi-2.0.4]# view /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib    #新增行
[root@web uwsgi-2.0.4]# ldconfig    #讓動態鏈接庫為系統所共享
[root@web uwsgi-2.0.4]# cp uwsgi /usr/local/bin/

6.安裝Django

[root@web apps]# cd Django-1.4.9
[root@web Django-1.4.9]# python setup.py install

推薦兩個比較好的Django入門博客:

http://www.cnblogs.com/qianyuliang/p/6814376.html

https://zhuanlan.zhihu.com/p/24831528


7.django-debug-toolbar安裝

[root@web apps]# cd /home/apps/django-debug-toolbar-master/
[root@web django-debug-toolbar-master]# python setup.py install 
[root@web ~]# view /data/www/OMserverweb/settings.py

根據實際情況修改如下:

技術分享圖片技術分享圖片


8.導入項目文件

導入項目文件至/data/www目錄

鏈接:https://pan.baidu.com/s/1Z69_DOwR4R3y06Jcgn7jxA 密碼:uhyl

[root@web ~]# mkdir -p /data/www
[root@web ~]# cd /data/www

將OMserverweb.zip上傳至/data/www並解壓


9.修改數據庫連接信息

[root@web OMserverweb]# view /data/www/OMserverweb/settings.py

技術分享圖片


10.修改主控端rpyc主機IP

[root@web OMserverweb]# view /data/www/OMserverweb/autoadmin/views.py

connect裏的ip修改為172.27.9.23

技術分享圖片


11.修改Nginx配置

server {
        listen       80;
        server_name  localhost;
        
        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9001;
            uwsgi_connect_timeout 30;
            uwsgi_param UWSGI_CHDIR /data/www/OMserverweb;
            uwsgi_param UWSGI_SCRIPT django_wsgi;
        }
        location ^~ /static {  
            root /data/www/OMserverweb;  
        }  
  
        location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {  
            root /data/www/OMserverweb/static;  
        }
      }


12.uwsgi配置

uwsgi.ini為新建文件,已經在OMserverweb安裝包中:

[root@web OMserverweb]# more /data/www/OMserverweb/uwsgi.ini 
[uwsgi]
chdir=/data/www/OMserverweb
pythonpath=/data/www
socket=127.0.0.1:9001
master=true
workers=4
pidfile=/data/logs/uwsgi.pid
vacuum=true
thunder-lock=true
enable-threads=true
harakiri=30
post-buffering=4096
daemonize=/data/logs/django_uwsgi.log


13.啟動web服務

[root@web OMserverweb]# uwsgi --ini /data/www/OMserverweb/uwsgi.ini
[uWSGI] getting INI configuration from /data/www/OMserverweb/uwsgi.ini
[root@web OMserverweb]# nginx

啟動日誌查看:

[root@web logs]# more /data/logs/django_uwsgi.log

nginx日誌查看:

[root@web ~]# tail /usr/local/nginx/logs/access.log

瀏覽器直接輸入IP地址:http://172.27.9.17/

技術分享圖片


Server端搭建

下載安裝包:

鏈接:https://pan.baidu.com/s/11FGbZ1Q9oedrqdKnnHhCwQ 密碼:yszq

[root@server ~]# mkdir /home/app-server/

將安裝包app-server上傳至/home/app-server


1.ansible部署

setuptools安裝:

[root@server ~]# cd /home/app-server/
[root@server app-server]# yum -y install unzip
[root@server app-server]# unzip setuptools-39.1.0.zip
[root@server app-server]# cd setuptools-39.1.0
[root@server app-server]# python setup.py install

ansible安裝(ansible必須使用2.3.1.0-1版本,否則會報錯):

[root@server ~]# cd /home/app-server/
[root@server app-server]# tar -zxvf ansible-2.3.1.0-1.tar.gz 
[root@server app-server]# cd ansible-2.3.1.0-1
[root@server ansible-2.3.1.0-1]# python setup.py install

sshpass安裝:

[root@server ansible-2.3.1.0-1]# cd ..
[root@server app-server]# tar -zxvf sshpass.tar.gz
[root@server app-server]# cd sshpass-1.06/
[root@server sshpass-1.06]# ./configure
[root@server sshpass-1.06]# make && make install


2.ansible配置

添加主機IP

[root@server ~]# mkdir -p /etc/ansible
[root@server ~]# cd /home/app-server/ansible-2.3.1.0-1/examples
[root@server examples]# cp ansible.cfg hosts /etc/ansible
[root@server examples]# view /etc/ansible/hosts    
[webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
172.27.9.17
172.27.9.19
172.27.9.22
172.27.9.23


3.連接測試

首先分別ssh連接websrvers組主機,此過程會生成秘鑰信息,不然後面的ansible測試會報錯:"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."

[root@server ~]# ssh 172.27.9.17
The authenticity of host '172.27.9.17 (172.27.9.17)' can't be established.
ECDSA key fingerprint is b2:dc:51:b1:a1:b6:e2:4a:c1:ab:f4:e3:15:48:4c:f6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.27.9.17' (ECDSA) to the list of known hosts.
[email protected]'s password:

ansible進行ping測試

[root@server ~]# ansible 172.27.9.17 -m ping -k
SSH password: 
172.27.9.17 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}


4.配置Linux主機SSH無密碼訪問

為避免Ansible下發指令時輸入目標主機密碼,通過證書簽名達到SSH無密碼是一個好的方案。這裏使用ssh-keygen生成一對秘鑰,使用ssh-copy-id來下發生成的公鑰。

創建秘鑰:

技術分享圖片

分別同步公鑰至目標主機:

[root@server ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

校驗SSH無密碼配置是否成功:

[root@server ~]# ssh 172.27.9.17
Last login: Wed May  9 17:50:17 2018 from 172.27.9.19

登陸成功,無需輸入密碼。


5.rpyc模塊安裝

[root@server app-server]# tar -zxvf rpyc-3.2.3.tar.gz
[root@server rpyc-3.2.3]# python setup.py install


6.Server端配置

[root@server app-server]# mv OMServer /home
[root@server app-server]# cd /home/OMServer/
[root@server OMServer]# view config.py

技術分享圖片

本文以ansible為例


7.修改數據庫配置

使用SQLyog Community工具修改server_list表數據,新增被控主機17/19/22/23

技術分享圖片

8.啟動server

[root@server OMServer]# python OMserver.py &
[root@server OMServer]# netstat -an|grep 11511 
tcp        0      0 0.0.0.0:11511           0.0.0.0:*               LISTEN

11511端口處於監聽狀態


9.訪問web頁面

技術分享圖片


測試

新增模塊(使用火狐瀏覽器)

技術分享圖片


技術分享圖片

‘提交’確定

技術分享圖片

編寫後臺任務模塊,編號為1008

[root@server ~]# cd /home/OMServer/modules/ansible
[root@server ansible]# view Mid_1008.py
# -*- coding: utf-8 -*-
from Public_lib import *
#查看系統版本模塊#
class Modulehandle():
    def __init__(self,moduleid,hosts,sys_param_row):
        self.hosts = ""
        self.Runresult = ""
        self.moduleid = moduleid
        self.sys_param_array= sys_param_row
        self.hosts=target_host(hosts,"IP")
    def run(self):
        try:
            self.Runresult = Order_Run(host=self.hosts, module_name='shell', module_args="df -h")
            if len(self.Runresult["failed"]) == 0 and len(self.Runresult["success"]) == 0 and len(self.Runresult["unreachable"]) == 0:
                return "No hosts found,請確認主機已經添加ansible環境!"
        except Exception,e:
            return str(e)
        return self.Runresult


技術分享圖片


技術分享圖片

常見錯誤:

技術分享圖片

server進程異常,重啟server端主進程:

[root@server OMServer]# python OMserver.py &

技術分享圖片

原因:server端開啟了防火墻

[root@server OMServer]# systemctl stop firewalld.service
[root@server OMServer]# systemctl disable firewalld.service

技術分享圖片

原因:web端uwsgi進程異常,重拉進程

[root@web OMserverweb]# ps -ef|grep uwsgi |awk '{print $2}'|xargs kill -9 
[root@web OMserverweb]# uwsgi --ini uwsgi.ini

技術分享圖片

原因:web端settings.py中的SECRET_KEY與server端config.py中的SECRET_KEY不一致,改成一致即可。


總結:

1.該平臺為我們進行自動化運維提供了一種思路。

OMServer平臺的三層架構模式簡明清晰,第一層是Django+uwsgi+Nginx+Mysql,第二層是rpyc分布式計算框架,第三層是Ansible、Saltstack、Func等自動化運維工具,三層都具有很強的代表性。

2.Django是用Python寫的Web框架,功能強大、內容全面、安全性高、高效,對ORM支持完善,社區活躍,同時其靈活性低、可修改性差。

3.Ansible是一個部署一群遠程主機的工具,具有部署簡單、功能強大、配置簡單、擴展性強、支持API及自定義模塊,可通過Python輕松擴展的特點,被控主機無需部署客戶端代理。ansible通過常用模塊在命令行就可以針對主機清單來管理配置遠程主機。

4.Web端搭建難點一是在對Django框架的理解,各模塊做什麽的,起什麽作用,二是uwsgi和nginx的參數配置;Server端搭建相對容易,主要是後期平臺擴展對Python和Ansible的掌握有一定要求。


OMserver自動化運維平臺搭建及測試