CentOS 6.x 升级内核(在线)

近段时间,由于linux内核安全漏洞频发,不得不升级陈旧多年的内核版本。都有点担心,升级后起不来,就先做个快照。
下面链接是开源社区的相关技术支持。

http://elrepo.org/tiki/tiki-index.php

先看版本:

[[email protected] ~]# uname -r
2.6.32-573.el6.x86_64

版本比较低,下面先导入公钥:

[[email protected] tmp]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

要为RHEL-安装ELRepo 6,SL- 6或CentOS- 6://CentOS7的有另外的链接,参考上面的技术支持:

[[email protected] tmp]# yum install http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm

这里注意下:

  • kernel-ml:主线最新版本,最新
  • kernel-lt:长期支持版本,稳定

升级安装:

[[email protected] tmp]# yum --enablerepo=elrepo-kernel install kernel-ml-devel kernel-ml -y
...

Installed:
  kernel-ml.x86_64 0:4.17.2-1.el6.elrepo                           kernel-ml-devel.x86_64 0:4.17.2-
1.el6.elrepo                          
Complete!

查看是否成功:

[[email protected] tmp]# cat /boot/grub/grub.conf

里面就有最新的内核数据和陈旧的内核数据。

#boot=/dev/sda
default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (4.17.2-1.el6.elrepo.x86_64)
        root (hd0,0)
        kernel /vmlinuz-4.17.2-1.el6.elrepo.x86_64 ro root=UUID=67a0bdef-4edf-4a76-beb6-e82385ec2b5b 
rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc 
KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-4.17.2-1.el6.elrepo.x86_64.img
title CentOS 6 (2.6.32-573.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=67a0bdef-4edf-4a76-beb6-e82385ec2b5b rd_NO_LUKS 
rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us 
rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-573.el6.x86_64.img

这时候,只需要修改里面的启动顺序就可以更换最新的内核了:

default=1    //改成:default=0
[email protected] tmp]# vi /boot/grub/grub.conf
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (4.17.2-1.el6.elrepo.x86_64)

重启即可:

[[email protected] ~]# uname -r
4.17.2-1.el6.elrepo.x86_64

Centos 7 安装配置Zabbix 3.4和报错解决方法

Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案 访问官网https://www.zabbix.com/

1. 运行环境搭建

Zabbix组成:Web展示端(Zabbix-Web)、服务端(Zabbix-Server)、数据存储端和客户端(Zabbix-Agents)。可以安装在同一台服务器上,但是如果你拥有一个更大更复杂的环境,将它们安装在不同的主机上也是一个选项。
Zabbix服务器能够直接监控到同一网络中的设备,如果其他网络的设备也需要被监控,那还需要一台Zabbix代理服务器(Zabbix-Proxy)。

环境准备:lnmp(linux+nginx+mysql+php)或者lamp(linux+apache+mysql+php),大同小异,本文采用宝塔面板(linux管理面板)编译安装nginx+mysql+php,首先新建Zabbix站点和创建Zabbix数据库。

2. 安装 Repository with MySQL database

rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm

3. 安装 Zabbix server

yum install zabbix-server-mysql

说明:

  • 如果出现类似以下报错
Loaded plugins: fastestmirror
...
One of the configured repositories failed (Unknown),and yum doesn't have enough cached data to continue.
...
Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again
  • 解决办法:

1、将/etc/yum.repos.d/epel.repo中的mirrorlist改为baseurl
2、/etc/resolv.conf文件中增加nameserver 144.144.144.144

4. 导入数据库

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

由于我使用的是宝塔面板,直接通过面板导入create.sql文件。

5. 配置数据库用户及密码

vi /etc/zabbix/zabbix_server.conf

找到添加下面三项,按自己数据库实际信息填写(数据库如果在本地,DBHost默认即可)。

DBHost=localhost    
DBName=zabbix
DBUser=zabbix
DBPassword=zabbixpw

启动zabbix-server并允许开机自启

systemctl enable zabbix-server
systemctl start zabbix-server

6. 配置Web前端

前端路径是/zabbix-3.4.9/frontends/php,上传到网站根目录,运行。

  • 根据报错,修改PHP参数:
max_input_time = 300
date.timezone = Asia/Shanghai
  • 安装完成后,出现下面报错:
zabbix server is not running: the information displayed may not be current.

查看log:

cat /var/log/zabbix/zabbix_server.log

看到类似如下信息:

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

查看/var/lib/mysql文件夹的确没有mysql.sock文件,通过find查找mysql:

find / -name mysql.sock

文件在/tmp/mysql.sock这个位置,做个软连接报错消失。

ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

PostgreSQL-获取日期时间、截取年、月、日

未分类

一、获取系统时间函数

1.1 获取当前完整时间

select now();

未分类

select current_timestamp;      --也是一样的效果

1.2 获取当前日期

select current_date;

未分类

1.3 获取当前时间

select current_time;

未分类

时间字段的截取

取年份

select extract(year from now());

未分类

取月份

select extract(month from now());

未分类

取给定的时间的日期

select extract(day from timestamp '2013-04-13');

未分类

firewalld对指定IP开放指定端口的配置

1.打开firewalld防火墙

systemctl start firewalld.service

2.添加防火墙规则(对指定ip开放指定端口)
(以下红色字体需要根据实际情况修改)

#(1) Postgresql端口设置。允许192.168.142.166访问5432端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="5432" accept"
#(2)redis端口设置。允许192.168.142.166访问6379端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept"
#(3)beanstalkd端口设置。允许192.168.142.166访问11300端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept"

3.重启防火墙,使配置生效

systemctl restart firewalld.service

4.查看配置结果,验证配置

firewall-cmd --list-all

5.删除规则

示例:

firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="11300" accept"
systemctl restart firewalld.service

在 Ubuntu 16.04 LTS 系统上安装 Python 3.6

Ubuntu 16.04 LTS 系统默认自带的是 Python 2.7 和 Python 3.5,有时候我们会需要用到 Python 3.6,但是官方的源里是没有 Python 3.6 的 ,今天就介绍一下如何安装 Python 3.6。

未分类

安装方法

1.配置软件仓库,因为 Python 3.6 新版没有发布到 Ubuntu 的正式仓库中,咱们通过第3方仓库来做。在命令行中输入:

sudo add-apt-repository ppa:jonathonf/python-3.6

如果有提示信息,回车即可。

2.检查系统软件包并安装 Python 3.6

sudo apt-get update 
sudo apt-get install python3.6

3.查看 Python 版本信息(现在在你的系统中已经有3个 Python 版本了)

python -V
python3 -V
python3.6 -V

4.通过上图我们看到,新安装的3.6版本需要输入 python3.6 才能使用,那能不能配置我只输入 python3 时就默认使用 3.6 版本呢,当然可以,执行以下命令:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1 
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 
sudo update-alternatives --config python3

5.最后再确认一下:

python3 -V

6.配置完成后,之前有些 3.5 版本时候装的包可能会提示不存在了,此时重新安装一下即可。

参考文章

https://blog.csdn.net/lzzyok/article/details/77413968

centos 7 安装HAproxy四层TCP负载均衡配置及测试

未分类

haproxy负载均衡166.110.110.100
后端服务器1 166.110.110.1
后端服务器2 166.110.110.2

--------------------------------------------------centos 7 处理----------------------------------------------------
关闭SELinux
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq!  #保存退出
setenforce 0 #使配置立即生效

关闭centos 7的防火墙(仅在测试的时候)
systemctl stop firewalld.service
systemctl disable firewalld.service
--------------------------------------------------haproxy安装----------------------------------------------------
yum install wget gcc -y && wget -c --no-check-certificate https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.12.tar.gz && tar -xvf haproxy-1.8.12.tar.gz && cd haproxy-1.8.12

groupadd haproxy #添加haproxy组
useradd -g haproxy haproxy -s /bin/false #创建nginx运行账户haproxy并加入到haproxy组,不允许haproxy
--------------------------------------------------haproxy配置----------------------------------------------------
vi /etc/haproxy/haproxy.cfg

修改为以下配置
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen admin_stats  
        bind 127.0.0.1:1080               #监听端口  
        mode http                       #http的7层模式  
        option httplog                  #采用http日志格式  
        #log 127.0.0.1 local0 err  
        maxconn 10  
        stats refresh 30s               #统计页面自动刷新时间  
        stats uri /stats                #统计页面url  
        stats realm Proxy Haproxy  #统计页面密码框上提示文本  
        stats auth admin:admin          #统计页面用户名和密码设置  
        stats hide-version              #隐藏统计页面上HAProxy的版本信息  


listen test1  
        bind 0.0.0.0:80  
        mode tcp
        balance leastconn
        #maxconn 40000  
        #log 127.0.0.1 local0 debug  
        server s1 166.110.110.1:80  
        server s2 166.110.110.2:80
------------------------------------------------------------------------------------------------------
centos7下haproxy启动停止重启重载状态等控制命令
systemctl (start, stop, restart, try-restart, reload, force-reload, status) haproxy.service

haproxy web监控信息166.110.110.100为haproxy安装所在服务器IP
http://166.110.110.100:1080/status
-----------------------------后端安装lighttpd 作为测试用----------------------------------------------
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭SELinux
vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled

安装
yum install -y epel-release gcc gcc-c++ autoconf automake  zlib zlib-devel pcre-devel zip unzip libtool bzip2-* && yum install -y lighttpd && systemctl enable lighttpd

编辑配置文件关闭ipv6
vi /etc/lighttpd/lighttpd.conf
注释掉ipv6

启动服务器
systemctl restart lighttpd.service

默认目录及首页
/var/www/lighttpd/index.html

对两个index.html进行修改进行区别
后端第一个服务器
echo "<h1>this is upstream server 1" >> /var/www/lighttpd/index.html

后端第二个服务器
echo "<h1>this is upstream server 2" >> /var/www/lighttpd/index.html

访问haproxy所在服务器IP或者解析的域名刷新试试

-----------------------------Haproxy 负载均衡的8种算法----------------------------------------
balance roundrobin # 轮询,软负载均衡基本都具备这种算法

balance static-rr # 根据权重,建议使用

balance leastconn # 最少连接者先处理,建议使用

balance source # 根据请求源IP,建议使用

balance uri # 根据请求的URI

balance url_param,# 根据请求的URl参数'balance url_param' requires an URL parameter name

balance hdr(name) # 根据HTTP请求头来锁定每一次HTTP请求

balance rdp-cookie(name) # 根据据cookie(name)来锁定并哈希每一次TCP请求

Centos安装Python2.7与Python3.5双版本

前言

最近博主一直忙于工作之中,无法自拔(别问我为什么,因为穷 👿 );最近有个小项目用到了Python,但是无奈服务端的有个Python2.7,但是也不能更新他,因为有程序在用,无奈只能弄双版本的Python,下面就来说说具体的步骤。

未分类

正文

首先,先下载Python的源码包:https://www.python.org/ftp/python/3.5.5/Python-3.5.5.tgz,执行

wget https://www.python.org/ftp/python/3.5.5/Python-3.5.5.tgz

然后解压:

tar zxvf Python-3.5.5.tgz

将解压目录移动至/usr/local:

mv Python-3.5.5 /usr/local

进入目录,然后配置编译:

cd /usr/local/Python-3.5.5
./configure 
make
make install

创建软链接

ln -s /usr/local/Python3.5/python /usr/bin/python3

到这一步就全部完成了,此时在终端输入python3 -V即可看见输出Python 3.5.5

但是有时候安装上Python3,却没有pip3,那么继续执行如下脚本:

wget https://bootstrap.pypa.io/3.2/get-pip.py
python3 get-pip.py 

为了大家方便,我将这些操作整合成了一个shell脚本,运行下方的脚本即可一键安装:

yum install -y wget && wget -O install.sh https://www.licoy.cn/go/api/shell/python3.sh && sh install.sh

后记

有人说,这些东西网上都有,为什么自己还要写一份呢?

我也知道网上有,不过博客主要就是用来记录自己的所见所闻、所知所想,爹有娘有不如自己有,反正就相当于好记性不如烂笔头,自己写一遍更有利于加深对它的理解以及更好的去使用它。

CentOS 6.X 上安装配置 NFS 目录共享

NFS 简介

NFS(Network File System)即网络文件系统,是 FreeBSD 支持的文件系统中的一种,它允许网络中的计算机之间通过 TCP/IP 网络共享资源。在 NFS 的应用中,本地 NFS 的客户端应用可以透明地读写位于远端 NFS 服务器上的文件,就像访问本地文件一样。
目前 NFS 主要有三个版本(NFSv2,NFSv3,NFSv4)。 NFSv2 和 NFSv3 除了 3 版本支持更多的新特性以外,最主要的区别应该就是 NFSv2 是使用 UDP 协议进行传输的,所以 NFSv2 的连接在复杂网络环境下可能没有那么可靠,而 NFSv3 同时支持 UDP 和 TCP 协议。而 NFSv4 则是在 NFSv3 的基础上提升了性能,强制了安全策略,引入有状态的协议等…
当 Client 端要挂载 NFS 共享卷的时候,会发送 RPC 请求给服务端,而 NFS 服务端会在用户验证后将一个随机的 Cookie 传送至客户端,以便客户端使用这个 Cookie 来认证那些要访问的共享卷。
NFS 的验证支持内置的 IP/主机权限分配,同时也被 tcp wrappers 所限制。

未分类

上面这段话以及架构图都是我从网上找的,主要的作用大概就是撑个场子这样,你懂的。如果你只想快速地搭建一个 CentOS 上的 NFS 环境,跳过这段 …

一、环境

1、软件环境

  • Windows 10 x64

  • VMWare 12 x64

  • CentOS 6.7 x64 * 3

  • nfs-utils

  • nfs-utils-lib

  • rpcblind

2、服务器规划

未分类

注:

  • NFS Client 的数量可以根据实际情况自行调增更多。

二、NFS Server 环境准备

1、创建共享目录

登录连接到规划的 NFS Server 节点,使用 root 用户执行如下命令创建共享目录

[root@hadoop1 ~]# mkdir -p /data/shared

注:

  • 创建的共享目录的位置根据实际情况而定。比如,如果你的服务器上专门挂载了一块用于存放数据的大容量磁盘,该磁盘挂载到了 /data1 目录,那么你可以将共享目录创建在 /data1 下面(/data1/shared)

  • 这个共享目录是实际存放数据文件的位置,其他的 NFS Client 访问该位置获取共享的文件信息。

2、给共享目录读写权限

root 用户执行如下命令

[root@hadoop1 data]# cd /data

[root@hadoop1 data]# pwd

/data

[root@hadoop1 data]# chmod -R 777 shared

[root@hadoop1 data]# ll -d shared

drwxrwxrwx. 2 root root 4096 Aug  6 06:18 shared

三、安装与配置 NFS Server

可以根据服务器的配置以及具体的业务需求选取一台服务器作为 NFS Server,NFS Server 是用作文件物理存储的节点。因此,要考虑的是 NFS Server 服务器的磁盘资源是否满足需求。此处我选取 192.168.174.200 作为 NFS Server。SSH 连接到 192.168.174.200 服务器过程省略…

1、检查服务器是否已经安装有 nfs-utils

[root@hadoop1 ~]# rpm -qa | grep nfs-utils

[root@hadoop1 ~]#

如果已经安装了 nfs-utils 及 nfs-utils-lib 包,可以直接跳过第 2 步

2、安装 nfs-utils

root 用户执行如下命令安装 nfs-utils

[root@hadoop1 ~]# yum install -y nfs-utils

注:

  • 执行如上命令会安装 nfs-utils.x86_64 1:1.2.3-78.el6 包以及其依赖的包(版本可能不同)
nfs-utils-lib.x86_64 0:1.1.5-13.el6

keyutils.x86_64 0:1.4-5.el6

libgssglue.x86_64 0:0.1-11.el6

libtirpc.x86_64 0:0.2.1-15.el6

rpcbind.x86_64 0:0.2.0-16.el6
...
  • rpcbind 是用于端口映射的包,在 CentOS 5 上对应的是 portmap。

3、配置 NFS Server

NFS 的配置文件位于 /etc/exports,在 CentOS 6.7 中,/etc/exports 文件已经存在,内容为空。因此,我这儿使用 root 用户直接编辑此文件配置即可(如果没有此文件,可以直接创建此文件即可 touch /etc/exports 或者 vim /etc/exports)。

[root@hadoop1 ~]# vim /etc/exports

在文件中添加如下内容:

/data/shared 192.168.174.201(rw,sync,all_squash)
/data/shared192.168.174.202(rw,sync,all_squash)

注:

  • /data/shared192.168.174.201(rw,sync,all_squash) 表示 NFS Server 上的共享目录 /data/shared 允许来自 192.168.174.201 服务器上的所有用户(all_squash)的读写(rw)操作权限。且数据文件采用同步(sync)方式进行共享

  • 上面的文件也可以配置成一行 /data/shared192.168.174.*(rw,sync,all_squash) ,表示允许 192.168.174 这个网段的所有机器的访问

  • 配置文件中的内容格式如下,'()’ 中的选项列表之间没有空格

<输出目录> [客户端1 选项(访问权限,同步方式,用户映射)] [客户端2 选项(访问权限,同步方式,用户映射)]

  • 具体的配置详解不在此列出了,可以参考 CentOS 官网的解释 https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-nfs-server-config-exports.html

4、NFS Server 防火墙配置

root 用户执行如下命令编辑 iptables 文件

[root@hadoop1 shared]# vim /etc/sysconfig/iptables

添加如下内容,并保存退出

### rpcbind
-A INPUT -p udp -m multiport --dports 111,875,892,2049,10053,32769 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m multiport --dports 111,875,892,2049,10053,32803 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT  -p udp -m multiport --sports 111,875,892,2049,10053,32769 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT  -p tcp -m multiport --sports 111,875,892,2049,10053,32803 -m state --state ESTABLISHED -j ACCEPT

如下示例:

未分类

注:

  • 如果 NFS Server 没有开启防火墙服务,则可以跳过此步骤。
  • 由于 rpcbind 即映射了 tcp 端口,也映射了 udp 端口,因此需要配置 tcp/udp
  • 上面配置的这些端口都是 nfs server 服务默认监听的端口,默认的配置在 /etc/sysconfig/nfs 文件中可以查看到

5、启动 nfs

在启动 nfs 之前必须先启动 rpcbind,root 用户执行如下命令启动 rpcbind

[root@hadoop1 data]# service rpcbind start

Starting rpcbind:                                          [  OK  ]

root 用户执行如下命令启动 nfs

[root@hadoop1 data]# service nfs start

Starting NFS services:                                     [  OK  ]

Starting NFS quotas:                                       [  OK  ]

Starting NFS mountd:                                       [  OK  ]

Starting NFS daemon:                                       [  OK  ]

Starting RPC idmapd:                                       [  OK  ]

四、配置 NFS Client

使用 SSH 分别连接到 NFS Client 对应的两个节点 192.168.174.201/202

1、安装 nfs 包

使用 root 用户分别在两台客户端服务器上执行如下命令安装 nfs

[root@hadoop2 ~]# yum install -y nfs-utils

注:

  • NFS 客户端也要安装 nfs-utils 包,不然挂载不了

2、创建本地挂载目录

使用 root 用户分别在两台客户端服务器上执行如下命令创建两个本地目录,用于将 NFS Server 上的共享目录映射到本地目录。

[root@hadoop3 ~]# mkdir -p /data/shared

[root@hadoop3 ~]# cd /data

[root@hadoop3 data]# pwd

/data

[root@hadoop3 data]# chmod -R 777 shared

[root@hadoop3 data]# ll -d shared/

drwxrwxrwx. 2 root root 4096 Aug  6 06:43 shared/

注:

  • 上面命令我将 NFS 客户端的本地目录与 NFS Server 端的共享目录创建在相同的路径和相同的名称,但其实此处可以与 NFS Server 端不两只,此处我只是为了便于我好记忆。

3、挂载 NFS Server 共享目录到本地

[root@hadoop2 data] mount -t nfs 192.168.174.200:/data/shared /data/shared

查看挂载后的服务器文件系统

[root@hadoop2 ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda2              97G  4.3G   87G   5% /

tmpfs                 1.9G   72K  1.9G   1% /dev/shm

/dev/sda1             283M   41M  227M  16% /boot

192.168.174.200:/data/shared

                       97G   16G   76G  18% /data/shared

可以看到多出了一个将 192.168.174.200:/data/shared 挂载到本地的 /data/shared 目录的记录

五、验证

1、在 NFS Client 节点创建文件

在 192.168.174.202 服务器上执行如下命令

[root@hadoop3 data]# cd shared

[root@hadoop3 shared]# vim test.text

输入如下内容,保存退出:

This is a test text!

查看 192.168.174.202 服务器上的 /data/shared 目录,有 test.text 文件

[root@hadoop3 shared]# pwd

/data/shared

[root@hadoop3 shared]# ll

total 4

-rw-r--r--. 1 nfsnobody nfsnobody 21 Aug  6 09:38 test.text

[root@hadoop3 shared]# cat test.text

This is a test text!

2、查看另外两个节点的目录

登录 192.168.174.200/201 服务器,查看 /data/shared 目录,可以看到两个节点上都能访问刚刚在 202 上创建的 test.text 文件,如下所示:

200:

[root@hadoop1 shared]# pwd

/data/shared

[root@hadoop1 shared]# ll

total 4

-rw-r--r--. 1 nfsnobody nfsnobody 21 Aug  6 09:38 test.text

201:

[root@hadoop2 shared]# pwd

/data/shared

[root@hadoop2 shared]# ll

total 4

-rw-r--r--. 1 nfsnobody nfsnobody 21 Aug  6 09:38 test.text

3、在 NFS Client 上修改文件内容

在 192.168.174.201 上修改文件,并在文件下方添加一行 Hello World 并保存退出

[root@hadoop2 shared]# vim test.text

This is a test text!

Hello World

4、查看另外两个节点的文件内容

查看 192.168.174.200/202 上的 /data/shared/test.text 文件内容:

200:

[root@hadoop1 shared]# pwd

/data/shared

[root@hadoop1 shared]# cat test.text

This is a test text!

Hello World

202:

[root@hadoop3 shared]# pwd

/data/shared

[root@hadoop3 shared]# cat test.text

This is a test text!

Hello World

可以看到更改后的内容也同时在另外两个节点上生效了。

至此,我们在 CentOS 6.X 上安装配置 NFS 共享文件目录就完成了。

centos升级glibc至2.18

官方的glibc源只更新到2.12版,很多业务需要升级到更高级版,这里介绍编译glibc升级的方式。

安装gcc与wget

sudo yum install gcc
sudo yum install wget

下载glibc-2.18.tar.gz

wget http://ftp.gnu.org/gnu/glibc/glibc-2.18.tar.gz

解压并进入到glibc-2.18

tar glibc-2.18.tar.gz
cd glibc-2.18

创建build文件夹

mkdir  ../glibc-build

cd  ../glibc-build

编译

../glibc-2.18/configure  --disable-sanity-checks
make&&make install

重启生效

在docker中使用ansible来源码编译nginx服务

说明:
1)在VM上装了一个4核8G的centos7.5系统
2)docker版本为 18.06.0-ce

docker的安装不再讲述

1、创建一个带有含有ssh的镜像,通过编写Dockerfile

# Set the base image to centos
FROM centos:latest
MAINTAINER fei
#mount volume
VOLUME ["/root/docker/ansible-demo/volume2"]
################## BEGIN INSTALLATION ######################
#install EPEL
RUN rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 
&& rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 
&& yum install -y yum-priorities
RUN yum install -y sudo
RUN yum install -y 
net-tools 
openssh-clients 
openssh-server 
ansible 
vim
################## END INSTALLATION ######################
# 将sshd的UsePAM参数设置成no,优化ssh连接
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
# 修改root用户密码,这里密码为:devilf
RUN echo "root:devilf"|chpasswd
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# 启动sshd服务并且暴露22端口
RUN mkdir /var/run/sshd
EXPOSE 22
ENTRYPOINT ["/usr/sbin/sshd","-D"]

2、开始构建镜像

docker build --no-cache -t fei/centos:ssh_ansible .

3、启动容器(需要开启特权模式,否则会报错:Failed to get D-Bus connection: Operation not permitted)

docker run -itd -p 20021:22 --privileged=true --name node1 fei/centos:ssh_ansible
docker run -itd -p 20022:22 --privileged=true --name node2 fei/centos:ssh_ansible
...
...
docker run -itd -p 20020:22 --privileged=true --name ansible_server fei/centos:ssh_ansible

4、配置ansible主机清单,并建立互信关系

修改ansible.cfg文件,将默认的hosts文件改为一个目录,修改为:
inventory     = /etc/ansible/conf.d

设置清单

# cat conf.d/docker
[nodes]
172.17.0.2
172.17.0.3
172.17.0.4
172.17.0.5

生成密钥

ssh-keygen

下发密钥

ssh-copy-id [email protected]

5、测试

ansible nodes -m ping

注意:
查看容器IP的方法:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_id

停止容器:

docker stop container_id

删除容器:

docker container rm container_id

下面就要开始通过playbook来源码编译安装nginx

可以针对所有的服务安装创建一个专门的目录,例如这里安装nginx,可以创建一个目录,目录结构为:

tree roles/
roles/
├── conf
│   ├── default
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   └── temp_server.conf
│   └── vars
│       └── main.yml
├── install
│   ├── default
│   ├── files
│   │   └── nginx-1.12.0.tar.gz
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   ├── nginx.conf
│   │   ├── web1.conf
│   │   └── web2.conf
│   └── vars
│       └── main.yml
├── nginx.retry
├── nginx.yaml
└── site.yml

分为两部分,conf目录主要是方便增加站点,存放配置文件;install目录主要是为了安装nginx,该目录下会存放安装所用的源码包,配置文件等
install目录下定义一个任务:

# cat tasks/main.yml
- name: cp nginx package to remote host
  copy: src=nginx-1.12.0.tar.gz dest=/tmp/nginx-1.12.0.tar.gz   #去files目录中拉取源码包
  tags: cp-nginx-pkg
- name: tar nginx package
  shell: cd /tmp; tar zxf nginx-1.12.0.tar.gz
- name: install nginx depend pkg
  yum: name={{ item }} state=latest       #item是一个变量,用来指定下面的一些依赖包名
  with_items:
    - openssl-devel
    - pcre-devel
    - gcc
    - gcc-c++
    - autoconf
    - automake
    - libtool
    - make
    - cmake
    - zlib
    - zlib-devel
    - openssl
    - pcre-devel
    - libxslt-devel
- name: install nginx
  shell: cd /tmp/nginx-1.12.0; ./configure --user=www --group=www --prefix=/usr/local/nginx 
         --with-http_stub_status_module 
         --with-http_ssl_module 
         --with-pcre && make && make install
- name: cp conf
  template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf     #这个是去templates目录中拉取配置文件
  tags: nginx-conf
- name: cp shell
  copy: src=/ansible/script/create_users.sh dest=/tmp/create_users.sh    #这个脚本的目的是检测目标机器是否已经存在所建的用户,如果存在机会创建用户会报错
- name: create nginx user
  shell: /bin/bash /tmp/create_users.sh
  tags: add-nginx
  notify: start nginx service

上面脚本内容:

# cat /ansible/script/create_users.sh
#!/bin/bash
name="www"
num=$(grep -c $name /etc/passwd)
if [ $num -eq 0 ];then
        groupadd $name
        useradd -g $name $name -s /sbin/nologin
fi

给nginx的主配置文件指定一个端口,通过设置一个变量,后面主配置里面会去引用

# cat vars/main.yml
ngxport: "8080"

主配置文件

# cat templates/nginx.conf
user  www;
worker_processes  {{ ansible_processor_vcpus }};

events {
    worker_connections  65535;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format main  '$remote_addr - $remote_user[$time_local] "$request" '
                      '$status $body_bytes_sent"$http_referer" '
                      '"$http_user_agent""$http_x_forwarded_for"';
    access_log logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       {{ ngxport }};  
        server_name  www.a.com;
        access_log logs/a.com;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
   include conf.d/*.conf;
}

定义触发器

# cat handlers/main.yml
- name: start nginx service
  shell: /usr/local/nginx/sbin/nginx

我们这里要新增一个站点做测试,需要修改的目录(需要切换到conf目录中)有:
定义变量,用于配置文件的引用:

# cat vars/main.yml
server_name: "www.a.com"    #每次新增站点时,可以修改此域名
root_dir: "/data/web"

因为新增站点时,是基于域名的虚拟主机,所以端口均为默认的80端口
编写新增站的配置文件:

# cat templates/temp_server.conf
server
{
listen 80;
server_name {{server_name}};
index index.php index.html;
root {{root_dir}};
}

在var目录中定义变量:

cat main.yml
server_name: "www.a.com"
root_dir: "/data/web"

编写配置nginx的tasks步骤哦:

cd tasks
cat main.yml
- name: create vhosts
  shell: mkdir -p /usr/local/nginx/conf/conf.d/
  tags: create_dir
- name: cp file nginx.conf
  template: src=temp_server.conf dest=/usr/local/nginx/conf/conf.d/{{server_name}}.conf
  tags: ngxconf
  notify: reload nginx service

定义角色路径

#回到roles的上级目录下
cat nginx.yaml
- hosts: web1
  remote_user: root
  roles:
    - install
    - conf

测试:

ansible-playbook -C nginx.yaml

测试通过后可以真正去执行

ansible-playbook nginx.yaml