首页 > 代码库 > Linux+Python高端运维班第十一次作业
Linux+Python高端运维班第十一次作业
1、源码编译安装LNMP架构环境;
(1)安装编辑工具和环境
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make cmake libtool* git tree
(2)安全nginx
[root@localhost opt]# wget http://nginx.org/download/nginx-1.8.1.tar.gz #下载nginx安装包 [root@localhost opt]# tar xzvf nginx-1.8.1.tar.gz #解压安装包 [root@localhost nginx-1.8.1]# ./configure \ > --prefix=/usr/local/nginx > --with-http_realip_module > --with-http_sub_module > --with-http_ssl_module > --with-http_gzip_static_module > --with-pcre [root@localhost nginx-1.8.1]# make && make install [root@localhost nginx-1.8.1]# vim /usr/local/nginx/conf/nginx.conf #修改php相关配置 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; include fastcgi_params; } [root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx #启动nginx
(3)安装mysql
[root@localhost opt]# groupadd mysql #添加mysql组 [root@localhost opt]# useradd -r -g mysql mysql #添加mysql用户 [root@localhost opt]# tar xzvf mysql-5.6.24.tar.gz [root@localhost opt]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel libaio #安装编译代码所需要的包 [root@localhost opt]# cd /opt/mysql-5.6.24 [root@localhost mysql-5.6.24]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ [MySQL安装的根目录] -DMYSQL_DATADIR=/mydata/mysql/data \ [MySQL数据库文件存放目录] -DSYSCONFDIR=/etc \ [MySQL配置文件所在目录] -DMYSQL_USER=mysql \ [MySQL用户名] -DWITH_MYISAM_STORAGE_ENGINE=1 \ [MySQL的数据库引擎] -DWITH_INNOBASE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎] -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ [MySQL的数据库引擎] -DWITH_MEMORY_STORAGE_ENGINE=1 \ [MySQL的数据库引擎] -DWITH_READLINE=1 \ [MySQL的readline library] -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock \ [MySQL的通讯目录] -DMYSQL_TCP_PORT=3306 \ [MySQL的监听端口] -DENABLED_LOCAL_INFILE=1 \ [启用加载本地数据] -DENABLE_DOWNLOADS=1 \ [编译时允许自主下载相关文件] -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all \ [使MySQL支持所有的扩展字符] -DDEFAULT_CHARSET=utf8 \ [设置默认字符集为utf8] -DDEFAULT_COLLATION=utf8_general_ci \ [设置默认字符校对] -DWITH_DEBUG=0 \ [禁用调试模式] -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled \ [通讯时支持ssl协议] -DWITH_ZLIB:STRING=bundled [允许使用zlib library] [root@localhost mysql-5.6.24]# make && make install [root@localhost local]# chown -R mysql:mysql /usr/local/mysql/ #修改mysql文件夹属组和属主 [root@localhost local]# chown -R mysql:mysql /mydata/mysql/data [root@localhost etc]# vim /etc/my.cnf #修改my.conf配置文件 # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir=/usr/local/mysql datadir=/mydata/mysql/data socket=/var/lib/mysql/mysql.sock character-set-server=utf8 user= mysql # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [root@localhost local]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql/data #初始化mysql数据库
(4)安装PHP
root@localhost opt]# tar xzvf php-5.6.5.tar.gz [root@localhost php-5.6.5]# ./configure --enable-opcache --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring=all --with-pdo-mysql --enable-sockets --enable-mbstring --enable-fpm --with-curl --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-gd --with-libxml-dir=/usr --enable-xml --with-openssl --with-iconv [root@localhost php-5.6.5]# make && make install [root@localhost etc]# cp /opt/php-5.6.5/php.ini-development /usr/local/php/etc/php.ini [root@localhost etc]# cd /usr/local/php/etc/ [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf [root@localhost etc]# /usr/local/php/sbin/php-fpm #启动php [root@localhost html]# vim /usr/local/nginx/html/index.php #创建一个php页面 <?php phpinfo(); ?>
2、编写一个脚本完成以下功能:
(1)、一键搭建LNMP源码编译环境;
(2)、可通过在脚本后面跟上一些参数来自定义安装目录等其他选项。
[root@localhost shell]# cat /shell/LNMP.conf #参数较多放在一个配置文件中 nginx_dir=/usr/local/nginx mysql_dir=/user/local/mysql mysqldata_dir=/mysqldata php_dir=/usr/local/php [root@localhost shell]# cat /shell/LNMP.sh #!/bin/bash #加载配置文件 source $1 #nginx安装函数 nginx() { groupadd -r nginx useradd -s /sbin/nologin -g nginx -r nginx cd /opt echo "------------开始安装nginx-1.8.1------------" wget http://nginx.org/download/nginx-1.8.1.tar.gz tar xzvf nginx-1.8.1.tar.gz cd nginx-1.8.1 ./configure --prefix=${nginx_dir} --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre make && make install } #mysql安装模块 mysql() { groupadd mysql useradd -r -g mysql mysql cd /opt echo "------------开始安装libmcrypt-2.5.7------------" wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.24.tar.gz tar mysql-5.6.24.tar.gz cd mysql-5.6.24 cmake . -DCMAKE_INSTALL_PREFIX=${mysql_dir} -DMYSQL_DATADIR=${mysqldata_dir} -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql -DMYSQL_GROUP=mysql make && make install cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf chown -R root:mysql ${mysql_dir} chown -R root:mysql ${mysqldata_dir} } #php安装模块 php() { cd /opt echo "------------开始安装libmcrypt-2.5.7------------" wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz tar xvf libmcrypt-2.5.7.tar.gz ./configure --prefix=/usr/local/libmcrypt && make && make install tar xzvf php-5.6.5.tar.gz echo "------------开始安装php-5.6.5------------" wget http://cn2.php.net/distributions/php-5.6.5.tar.gz cd php-5.6.5 ./configure --enable-opcache --prefix=${php_dir} --with-config-file-path=${php_dir}/etc --with-mysql=${mysql_dir} --with-mysqli=${mysql_dir}/bin/mysql_config --enable-mbstring=all --with-pdo-mysql --enable-sockets --enable-mbstring --enable-fpm --with-curl --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-xml --with-gd --with-libxml-dir=/usr --enable-xml --with-openssl --with-iconv && make && make install cp ${php_dir}/etc/php-fpm.conf.default ${php_dir}/etc/php-fpm.conf cp /opt/php-5.6.5/php.ini-development ${php_dir}/etc/php.ini } yum install gcc gcc-c++ make pcre-* zlib-* cmake ncurses-devel libjpeg* libpng* freetype* libcurl-devel libvpx-devel libxml2 libxml2-devel libXpm libXpm-devel libXpm.i686 libXpm.i686-devel openldap-devel -y #安装nginx软件nginx if [ $? -eq 0 ];then echo "nginx安装成功!!!" else echo "nginx安装异常!!!" exit fi #安装mysql数据库 mysql if [ $? -eq 0 ];then echo "mysql安装成功!!!" else echo "mysql安装异常!!!" exit fi #安装php php if [ $? -eq 0 ];then echo "php安装成功!!!" exit else echo "php安装异常!!!" exit fi
3、结合图形描述LVS的工作原理;
(1)vs-nat模型
主要是修改目标IP地址为挑选出新的RS的IP地址。即请求进入负载均衡器时做DNAT,响应出负载均衡器时做SNAT。
a.当用户请求到达Director Server,此时请求的数据报文会先到达内核的PREROUTING链,此时报文的源IP是CIP,目标IP是VIP。
b.PREROUTING链检查发现数据包的目标IP是本机,将数据包送至INPUT链。
c.IPVS内核模块比对数据包请求的服务是否为集群服务,如果是,则修改数据包的目标IP为后端服务器的IP,然后将数据包发至POSTROUTING链,做DNAT转换。此时报文的源IP是CIP,目标IP是RIP
d.POSTROUTING链通过选路,将数据包发送到Real Server。
e.Real Server比对发现目标IP是自己的IP,开始建立响应报文发回给Director Server,此时报文的源IP是RIP,目标IP是CIP.
f.Director Server在响应客户端之前,此时会将源IP地址修改为自己的IP地址,然后响应给客户端,做SNAT转换。此时报文的源IP是VIP。目标IP是CIP。
(2)lvs-dr模型
将请求报文的目标MAC地址设定为天选出来的RS的MAC地址。即做MAC地址转换。
a.当用户请求到达Director Server,此时请求的数据报文huixiandao内核空间的PREROUTING链,此时报文的IP是CIP,目标IP是VIP。
b.PREROUTING链检查发现数据包的目标IP是本机,将数据包送至INPUT链,
c.IPVS内核模块比对数据包请求的服务是否为集群服务,如果是,将请求报文中的源MAC地址修改为DIP的MAC地址,将目标MAC地址修改为RIP的MAC地址,然后将数据包发至POETROUTING链中,此时的源IP和目的IP均未修改,仅修改了源MAC地址为DIP的MAC地址,目标MAC地址为RIP的MAC地址。
d.由于DS和RS实在同一网络中,所以两者之间的通信时通过二层协议来传输。POSTROUTING链检查目标MAC地址为RIP的MAC地址,那么此时数据包将会发至Real Server。
e.RS发现请求报文的MAC地址是自己的MAC地址,就接受此报文,处理完成以后,将响应报文通过IO接口传送给eth0网卡,然后向外发出,不经过负载均衡器。此时源IP地址为VIP,目标IP是CIP。
f.响应报文最终送至客户端。
(3)lvs-tun模型
在原有的IP报文外再次封装多一层IP首部,内部IP首部(源地址为CIP,目标地址为VIP)外部IP地址首部(源地址为DIP,目标地址为RIP)
a.当用户请求报文到达DS,此时请求的数据报文会先到内核的PREROUTING链。此时源IP是CIP,目标IP是VIP。
b.PREROUTING链检查发现数据包的目标IP是本机,将数据包送至INPUT链。
c.IPVS比对数据包请求的服务是否为集群服务,如果是,在请求报文的首部再次封装一层IP报文,封装源IP为DIP,目标IP是RIP,然后发至POSTROUTING链。此时源IP是为DIP,目标IP是RIP。
d.POSTROUTING链根据最新封装的IP报文,将数据包发至RS(因为外层封装多了一层IP首部,所以可以理解为此时通过隧道传输)。此时源IP是DIP,目标IP是RIP。
e.RS收到报文后发现是自己的IP地址,就会将报文接受下来,拆除最外层的IP后,会发现里面还有一层IP首部,而且目标地址是自己的lo接口VIP,那么此时RS开始处理此请求,处理完成滞后,通过lo接口送给eth0网卡,然后向外传递。此时的源IP地址为VIP,目标IP为CIP。
f.响应报文送达至客户端。
4、阐述varnish的功能及其应用场景,并通过实际的应用案例来描述配置、测试、调试过程。
varnish实现php和html动静分离、负载均衡
php页面
http://10.10.10.200:8080http://10.10.10.201:8080html页面
http://10.10.10.200:8081
http://10.10.10.201:8081
[root@localhost opt]# wget -c https://repo.varnish-cache.org/source/varnish-4.1.3.tar.gz [root@localhost opt]# tar xzvf varnish-4.1.3.tar.gz [root@localhost opt]# cd varnish-4.1.3 [root@localhost varnish-4.1.3]# ./configure --prefix=/usr/local/varnish [root@localhost varnish-4.1.3]# make && make install [root@localhost varnish-4.1.3]# cd /usr/local/varnish/var/varnish/ [root@localhost varnish-4.1.3]# vim default.vcl #定义后端服务器 backend web1 { .host = "10.10.10.200"; .port = "8081"; .connect_timeout = 20s; } backend web2 { .host = "10.10.10.201"; .port = "8081"; .connect_timeout = 20s; } backend php1 { .host = "10.10.10.200"; .port = "8080"; .connect_timeout = 20s; } backend php2 { .host = "10.10.10.201"; .port = "8080"; .connect_timeout = 20s; } import directors; #定义html和php集群 sub vcl_init { new web_cluster = directors.round_robin(); web_cluster.add_backend(web1); web_cluster.add_backend(web2); new php_cluster = directors.round_robin(); php_cluster.add_backend(php1); php_cluster.add_backend(php2); } #定义acl,设置清理缓存的IP acl allow_purge_cache { "127.0.0.1"; "10.18.11.0"/24; } sub vcl_recv { #配置动静分离 if (req.url ~ "\.php$"){ set req.backend_hint = php_cluster.backend(); } else{ set req.backend_hint = web_cluster.backend(); } #使得后端服务能记录访问者的真实IP if (req.http.X-Forward-For) { set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip; } else { set req.http.X-Forward-For = client.ip; } #定义清楚缓存IP,调用上面的Acl if (req.method == "PURGE") { if (!client.ip ~ allow_purge_cache) { return(synth(405,"Not allowed")); } return(hash); } #支持压缩功能 if (req.http.Accept-Encoding) { if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { remove req.http.Accept-Encoding; } else if (req.http.Accept-Encoding ~ "gzip") { set req.http.Accept-Encoding = "gzip"; } else if (req.http.Accept-Encoding ~ "deflate") { set req.http.Accept-Encoding = "deflate"; } else { remove req.http.Accept-Encoding; } } } sub vcl_deliver { #定义Header标识,判断缓存是否命中 if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; #命中则返回HIT } else { set resp.http.X-Cache = "MISS"; #未命中则返回MISS } } [root@localhost varnish]# /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/var/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:80 #启动varnish [root@localhost varnish]# curl -I http://10.10.10.200/index.php #访问php页面 HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Tue, 22 Nov 2016 08:38:05 GMT Content-Type: text/html; charset=UTF-8 X-Powered-By: PHP/5.6.5 X-Varnish: 2 Age: 0 Via: 1.1 varnish-v4 X-Cache: MISS #第一次访问没有缓存 Accept-Ranges: bytes Connection: keep-alive [root@localhost varnish]# curl -I http://10.10.10.200/index.php HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Tue, 22 Nov 2016 08:38:05 GMT Content-Type: text/html; charset=UTF-8 X-Powered-By: PHP/5.6.5 X-Varnish: 5 3 Age: 3 Via: 1.1 varnish-v4 X-Cache: HIT #第二次访问命中缓存 Accept-Ranges: bytes Connection: keep-alive [root@localhost varnish]# curl -I http://10.10.10.200/index.html #访问html页面 HTTP/1.1 200 OK Date: Tue, 22 Nov 2016 08:46:02 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Tue, 22 Nov 2016 07:00:59 GMT ETag: "15-541de55a6c15a" Content-Length: 21 Content-Type: text/html; charset=UTF-8 X-Varnish: 32772 Age: 0 Via: 1.1 varnish-v4 X-Cache: MISS Accept-Ranges: bytes Connection: keep-alive [root@localhost varnish]# curl -I http://10.10.10.200/index.html HTTP/1.1 200 OK Date: Tue, 22 Nov 2016 08:46:02 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Tue, 22 Nov 2016 07:00:59 GMT ETag: "15-541de55a6c15a" Content-Length: 21 Content-Type: text/html; charset=UTF-8 X-Varnish: 9 32773 Age: 1 Via: 1.1 varnish-v4 X-Cache: HIT Accept-Ranges: bytes Connection: keep-alive
5、搭建一套LVS-DR模型的高性能集群,并用Keepalived实现nginx与lvs的高可用集群,同时实现以下功能:
(1)、wordpress程序通过nfs共享给各个realserver;
(2)、后端realserver中的nginx和php分离
名称
用途 | IP |
LVS-Master | 10.18.11.31 |
LVS-BACKUP | 10.18.11.32 |
LVS-VIP | 10.18.11.40 |
wordpress1 | 10.18.11.29 |
wordpress2 | 10.18.11.30 |
(1)配置NFS共享wordpress程序
#在10.18.11.29上配置共享目录 [root@localhost ~]# vim /etc/exports #在10.18.11.29上配置共享目录 /usr/local/nginx/html/ 10.18.11.29(rw,sync,fsid=0,no_root_squash) [root@localhost /]# systemctl start rpcbind.service #启动rpcbind服务 [root@localhost /]# systemctl start nfs-server.service #启动nfs服务 #在10.18.11.30上挂载共享目录 [root@localhost nginx]# mount -t nfs 10.18.11.29:/usr/local/nginx/html /usr/local/nginx/htmlmount -t nfs 10.18.11.29:/usr/local/nginx/html /usr/local/nginx/html [root@localhost nginx]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda2 75G 4.6G 66G 7% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 25M 1.9G 2% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda1 477M 94M 354M 21% /boot /dev/mapper/vgdata-mylv 6.8G 23M 6.4G 1% /users tmpfs 380M 0 380M 0% /run/user/0 10.18.11.29:/usr/local/nginx/html 75G 8.7G 62G 13% /usr/local/nginx/html [root@localhost nginx]# ll /usr/local/nginx/html/ #wordpress工程文件已共享 总用量 28 -rw-r--r-- 1 root root 537 11月 16 17:22 50x.html -rw-r--r-- 1 root root 145 11月 26 23:14 inc.php -rw-r--r-- 1 root root 612 11月 16 17:22 index.html -rw-r--r-- 1 root root 20 11月 25 15:23 index.php -rw-r--r-- 1 root root 6 11月 21 17:39 test.html -rw-r--r-- 1 root root 145 11月 26 23:15 test.php drwxrwxrwx 5 root root 4096 11月 27 16:37 wordpress
(2)安装workpress
[root@localhost opt]# wget [root@localhost opt]# tar xzvf wordpress-4.5.3-zh_CN.tar.gz [root@localhost opt]# cp -r wordpress /usr/local/nginx/html/
(3)在游览器中访问http://10.18.11.29/wordpress/wp-admin/install.php进行数据库配置
本文出自 “学海无涯” 博客,谢绝转载!
Linux+Python高端运维班第十一次作业