大中型公司运维基础运维

 

我们先谈谈运维整体概念:

  核心:确保线上稳定

1、产品生产流程:



 

    1、首先公司管理层根据公司发展方向定位,相关人员定位市场需求进行调研、分析、最终给出详细设计。

    2、架构师根据产品设计的需求,如pv大小预估、服务器规模、应用架构等因素完成网络规划,架构设计等

    3、开发工程师将设计code实现出来、测试工程师对应用进行测试。

    4、运维人员将程序上线和保证线上稳定运行。

      

2、产品从无到有 运维工程师做的事(确保线上稳定):




    1、应用的前期架构设计、软/硬件资源评估申请采购、应用设计性能隐患及评估、IDC、服务性能、安全调优、服务器系统级优化(与特定应用有关)等

    2、负责产品服务器上架准备工作,服务器系统安装、网络、IP、通用工具集安装。

    3、运维工程师还需要对上线的应用系统架构是否合理、是否具备可扩展性、及安全隐患等因素负责,并负责最后将产品(程序)、网络、系统三者进行拼接并最优化的组合在一起,最终完成产品上线提供用户使用,并周而复使:需求->开发(升级)->测试->上线(性能、安全问题等之前预估外的问题随之慢慢就全出来了)

       a、具体工作可能包括:升级版本上线工作、服务监控、应用状态统计、日常服务状态巡检、突发故障处理、服务日常变更调整、集群管理、服务性能评估优化、数据库管理优化、随着应用PV增减进行应用架构的伸缩、安全、运维开发工作:

         (1、尽量将日常机械性手工工作通过工具实现(如服务监控、应用状态统计、服务上线等等),提高效率。

         (2、解决现实中服务存在的问题,如高可靠性、可扩展性问题等。

         (3、大规模集群管理工具的开发,如1万台机器如何在1分钟内完成密码修改、或运行指定任务?2000台服务器如何快速安装操作系统?各分布式IDC、存储集群中数PT级的数据如何快速的存储、共享、分析?等一系列挑战都需运维工程师的努力。

 

3、我这里详细谈谈公司从采购服务器到服务上线前的准备过程(基础运维):




   1、找IDC托管服务器。(购买所需带宽,和 找相应的7*24小时的看管维护服务等)

   2、根据架构师分析经boss同意,有采购部门采购服务器。(服务器类型:DELLR510、DELLR610、PR2510N、HP-DL180G6、IBMx3550m3、LenovoR510等) 

   3、服务器发到IDC上架。

      (1、服务器上架。(布线、布置电源系统、配置网络信息等)

      (2、服务器安装系统。(1.利用PXE+kickstart批量安装系统 2.设置机器远程控制口ip信息和访问密码 3.设置好机器内网ip 保证可以正常登录。)

      (3、服务器做基础设置。(1.脚本批量执行 预先写好的脚本完成服务器初始化。初始化脚本(配置好外网ip,hostname,dns等、关闭不必要的服务、初始iptables设置、hosts.allow文件设置、rc.local文件加载自启动服务和网络安全参数设置、ntp服务设置、升级驱动、优化fstab表、配置crontab计划任务等。) 2.基础监控部署(服务器存活、磁盘好坏、raid是否正常、网卡连接和速度、cpu负载、内存使用、swap分区使用、))

   4、提交给应用运维上线服务。

   5、机器的售后和维修。(根据监控报警,检测到硬件故障(硬盘、电源、主板、)联系服务商 去维修。)  

 

 

 

1.Kickstart+Dhcp+Nfs+Tftp+Pxe 最实用的批量linux服务器安装




 

 

一、实用环境,随着公司业务不断增加,经常需要采购新服务器,并要求安装Linux系统,并且要求Linux版本要一致,方便以后的维护和管理,每次人工安装linux系统会浪费掉更多时间,如果我们有办法能节省一次一次的时间岂不更好呢?下面就让我们开始进入自动安装linux系统学习!

 

前言* 虽然网上有成千上万的kickstart安装配置文档,但是在这些文章中,有好多配置,我们可能看不懂,好多地方模糊,所以根据亲身实践并在生产环境使用自动安装linux过程写给大家,希望对刚接触Linux自动安装系统的朋友一些点滴的帮助!

 

 

 

二、正式安装:【实践环境 64位Centos 5.3 】

 

(1)、看标题我们需要配置这些步骤:Kickstart+Dhcp+Nfs+Tftp+Pxe,我们从最简单的dhcp、tftp、nfs开始配置:

 

yum install   -y  dhcp*  tftp*    即可!nfs系统默认已经安装,稍后会配置;

 

vi /etc/xinetd.d/tftp
service tftp
{
 
disable = no
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -u nobody -s /tftpboot
per_source = 11
cps = 100 2
flags = IPv4
}

需要修改1个地方

disable = yes 修改为no 即可!

 

(2)、配置tftp+pxe:

 

安装linux系统,首先需要挂载linux安装镜像,可以是iso,可以是linux光盘:

 

mount     /dev/cdrom    /mnt  即可!然后执行如下步骤:

 

mkdir  -p /tftpboot  &&cd  /tftpboot  &&cp /usr/lib/syslinux/pxelinux.0 ./  &&cp /mnt/p_w_picpaths/pxeboot/{vmlinuz,initrd.img}   ./  && mkdir  -p  pxelinux.cfg  &&cp  /mnt/isolinux/isolinux.cfg   pxelinux.cfg/default 完毕!

 

然后编辑defalut配置文件:【还是在 /tftpboot 目录下】

vi   pxelinux.cfg/default (修改配置如下:)
default linux
prompt 1
timeout 10
display boot.msg
F1 boot.msg
F2 options.msg
F3 general.msg
F4 param.msg
F5 rescue.msg
label linux
kernel vmlinuz
append ks=nfs:192.168.2.79:/centosinstall/ks.cfg  ksdevice=eth0 initrd=initrd.img
 
label text
kernel vmlinuz
append initrd=initrd.img text
label ks
kernel vmlinuz
append ks initrd=initrd.img
label local
localboot 1
label memtest86
kernel memtest
append -

 

修改如上两个红色地方即可:【设置timeout 10 /*超时时间为10S */;ksdevice=etho代表当我们有多块网卡的时候,要实现自动化需要设置从eth0安装,不指定的话,安装的时候系统会让你选择,那就不叫全自动化了,192.168.2.79是kickstart服务器,/centosinstall 是nfs共享linux镜像的目录,ks.cfg是kickstart主配置文件,稍后会详细配置】

自此tftp配置完毕! 我们可以启动它,它是非独立服务,需要借助xinetd启动如下:

 

chkconfig    tftp  --level 35 on  && service  xinetd  restart 即可!

 

 

(3)、接下来安装nfs+kickstart:

 

创建nfs共享目录,也是存放linux镜像的目录:

mkdir   -p   /centosinstall  && cp  -rf   /mnt/*  /centosinstall  拷贝完后,设置nfs共享
vi  /etc/exports
/centosinstall    *(rw,sync) 保存退出即可!【可根据实际控制权限访问*代表所有】
 
 
nfs配置完毕,接着配置kickstart主配文件:
 
在/centosinstall/下创建ks.cfg文件,输入以下内容:
 
【这是我的实际使用的配置文件,当然你可以根据需要修改】有下载地址:http://blog.mgcrazy.com/download/ks.cfg
 
 
# Kickstart file automatically generated by anaconda.
install
text
nfs --server=192.168.2.79 --dir=/centosinstall
key --skip 
lang zh_CN.UTF-8
keyboard us
network --device eth0 --bootproto=dhcp --noipv6 
rootpw linux@passwd 
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all --initlabel
part /boot --fstype ext3 --size=200
part swap --size=4000
part /www/logs --fstype ext3 --size=20000
part / --fstype ext3 --size=20000
part /home --fstype ext3 --size=10000
part /www --fstype ext3 --size=1 --grow
%packages
@admin-tools
@base
@core
@development-libs
@development-tools
@editors
@system-tools
@base-x
@chinese-support
keyutils
kexec-tools
trousers
fipscheck
device-mapper-multipath
imake
audit
xorg-x11-server-Xnest
xorg-x11-server-Xvfb

 

如上ks.cfg配置完毕,并设置其权限:这里设置为:chmod   777   /centosinstall/ks.cfg

 

具体配置参数可以参考该文章:http://wenku.baidu.com/view/ab012dfb770bf78a65295420.html  kickstart参数详解!

 

(4)、最后我们来配置dhcp服务:

 

dhcp配置下载地址:http://blog.mgcrazy.com/download/dhcpd.conf   :【精简版配置,如果需要大批量安装linux服务器,可以设置dhcp段】

ddns-update-style interim;
ignore client-updates;
next-server 192.168.2.79;
filename "pxelinux.0";
allow booting;
allow bootp;
subnet 192.168.0.0 netmask 255.255.252.0 {
# --- default gateway
option routers   192.168.0.1;
option subnet-mask  255.255.252.0;
# option nis-domain  "domain.org";
# option domain-name  "192.168.0.10";
# option domain-name-servers 192.168.0.10;
option time-offset  -18000; # Eastern Standard Time
# option ntp-servers  192.168.1.1;
# option netbios-name-servers 192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless
# -- you understand Netbios very well
# option netbios-node-type 2;
host ns {
  hardware ethernet  00:1a:a0:2b:38:81;
  fixed-address 192.168.2.101;}
 
host ns1 {
  hardware ethernet  00:0c:29:2b:90:67;
  fixed-address 192.168.2.102;}
}

【*如上注释掉dns,是为了安装的时候,快速,不必在查找本地dns的时候等待时间,最注意的地方,你的本机ip一定要跟你的dhcpd里面配置一致,否则的话,无法启动dhcp,我这里只允许两个MAC地址如上的机器安装,也许你会说,为什么要限制呢?如果不限制的话,权限就太大了,所有已网卡启动机器都会安装成linux系统,想象一下,如果你公司的电脑,第二天同事们来上班的话,系统都变成linux啦,闯的祸就大了,或者你也可以把需要安装的机器和kickstart服务器放在单独的环境更安全!(*^__^*) 嘻嘻…… 】

三、整个环境部署完毕!接下来我们重启所有服务,并测试,注意这里安装完后重启登陆的密码为ks.cfg里面配置的rootpwd 后面的 linux@passwd

 

service    xinetd  restart  &&  service nfs restart  && service  dhcpd restart  即可!

找一台需要安装linux系统机器,进入bios把第一启动项设置为LAN 也即是网卡启动!然后启动,系统会自动查找本地pxe 服务器然后,自动安装!

 

2.服务器初始化



 
  
Rhel 6.1版本举例:
#!/bin/sh
sz=`uname -a|grep "i386"|wc -l`
if [ $sz -ne 1 ]
then 
      echo "system hardware-platform error"
      exit 1
fi
 
echo "----------------------------------------------------------------------"
echo "add DNS"
echo "nameserver 8.8.8.8
">/etc/resolv.conf
 
echo "----------------------------------------------------------------------"
echo "close some services"
 
{
chkconfig --level 3 abrtd off
chkconfig --level 3 acpid off
chkconfig --level 3 atd off
chkconfig --level 3 auditd off
chkconfig --level 3 autofs off
chkconfig --level 3 avahi-daemon off
chkconfig --level 3 certmonger off
chkconfig --level 3 cgconfig off
chkconfig --level 3 cgred off
chkconfig --level 3 cpuspeed off
chkconfig --level 3 cups off
chkconfig --level 3 haldaemon off
chkconfig --level 3 ip6tables off
chkconfig --level 3 ipsec off
chkconfig --level 3 kdump off
chkconfig --level 3 lvm2-monitor off
chkconfig --level 3 mdmonitor off
chkconfig --level 3 messagebus off
chkconfig --level 3 netconsole off
chkconfig --level 3 netfs off
chkconfig --level 3 nfs off
chkconfig --level 3 nfslock off
chkconfig --level 3 ntpd off
chkconfig --level 3 ntpdate off
chkconfig --level 3 oddjobd off
chkconfig --level 3 portreserve off
chkconfig --level 3 postfix off
chkconfig --level 3 psacct off
chkconfig --level 3 quota_nld off
chkconfig --level 3 rdisc off
chkconfig --level 3 restorecond off
chkconfig --level 3 rhnsd off
chkconfig --level 3 rhsmcertd off
chkconfig --level 3 rpcbind off
chkconfig --level 3 rpcgssd off
chkconfig --level 3 rpcidmapd off
chkconfig --level 3 rpcsvcgssd off
chkconfig --level 3 saslauthd off
chkconfig --level 3 smartd off
chkconfig --level 3 sssd off
chkconfig --level 3 sysstat off
chkconfig --level 3 udev-post off
chkconfig --level 3 ypbind off
} > /dev/null 2>&1
 
 
 
echo "----------------------------------------------------------------------"
echo "config the iptables"
sz=`grep "161" /etc/sysconfig/iptables|wc -l`
if [ $sz -eq 0 ];then
{
cat <<'EOF'
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -s 10.1.1.0/24  -j ACCEPT
-A RH-Firewall-1-INPUT -s 172.16.1.0/24 -m udp -p udp --dport 161 -j ACCEPT
#运行内网通过snmp获取信息
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 30976 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
EOF
} > /etc/sysconfig/iptables
sed -i '188a echo "2621400" >/proc/sys/net/ipv4/ip_conntrack_max' /etc/init.d/iptables
fi
 
 
 
echo "----------------------------------------------------------------------"
echo "config the hosts.allow"
cat <<'EOF'
##lan
sshd:10.1.0.0/255.255.240.0
sshd:172.16.1.0/255.255.255.0
##管理机
#sshd:ip地址
EOF
> /etc/hosts.allow
 
echo "all:all" >> /etc/hosts.deny
chattr +i /etc/hosts.deny
fi
 
/usr/sbin/ntpdate clock.isc.org -w
 
echo "----------------------------------------------------------------------"
echo "config rc.local"
sz=`grep "proc" /etc/rc.local|wc -l`
if [ $sz -eq 0 ];then
{
cat <<'EOF'
echo "1" >/proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/tcp_synack_retries
echo "1" > /proc/sys/net/ipv4/tcp_syn_retries
echo "4096000">  /proc/sys/net/ipv4/route/max_size
echo "2621400" >/proc/sys/net/ipv4/ip_conntrack_max
echo "8192" > /proc/sys/net/core/somaxconn
/usr/sbin/ntpdate clock.isc.org
EOF
} >> /etc/rc.local
fi
 
 
 
echo "----------------------------------------------------------------------"
echo "config crontab"
 
cat <<'EOF'
1 0 * * * /usr/sbin/ntpdate clock.isc.org &
*/5 * * * * /sbin/iptables -Z
*/5 * * * * /usr/local/monitor-base/o.sh &
2 0 * * * /usr/sbin/ntpdate ntp.cc.sandai.net &
EOF
> /var/spool/cron/root
fi
 
 
echo "----------------------------------------------------------------------"
echo "keep ipv6"
sz=`grep "ipv6 off" /etc/modprobe.conf|wc -l`
if [ $sz -eq 0 ];then
{
cat <<'EOF'
alias net-pf-10 off
alias ipv6 off
EOF
} >> /etc/modprobe.conf
fi
 
echo "----------------------------------------------------------------------"
echo "modify the fstab"
sed -i "s/LABEL=\/usr\/local        \/usr\/local              ext3    defaults        1 2/LABEL=\/usr\/local        \/usr\/local              ext3    defaults,noatime        1 2/g" /etc/fstab
sed -i "s/LABEL=\/data             \/data                   ext3    defaults        1 2/LABEL=\/data             \/data                   ext3    defaults,noatime        1 2/g" /etc/fstab
 
for i in {1..15}
do
sed -i "s/LABEL=\/data$i            \/data$i                  ext3    defaults        1 2/LABEL=\/data$i            \/data$i                  ext3    defaults,noatime        1 2/g" /etc/fstab
done
 
 
 
echo "----------------------------------------------------------------------"
echo "other opt"
mkdir /var/lib/vnstat
mv /usr/local/vnstat* /usr/local/bin/vnstat
mv /usr/sbin/sendmail /usr/sbin/sendmail.old >/dev/null 2>&1
mkdir -p /usr/local/xl_app_backup
mkdir -p /usr/local/mysql
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/media/media \/usr\/local \/data \/data1 \/data2 \/data3 \/data4 \/data5 \/data6 \/data7 \/data8 \/data9 \/data10 \/data11 \/data12 \/data13 \/data14 \/data15/g' /etc/updatedb.conf
 
 
 
 
 
echo "----------------------------------------------------------------------"
 
echo "yum"
wget "http://apt.sandai.net/as6u1_x86.repo" -O /etc/yum.repos.d/rhel-source.repo > /dev/null
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-*
yum clean
yum upgrade -y
 
echo "----------------------------------------------------------------------"
 
echo "update netcard driver"
#升级驱动程序。。省略了
 
 
 
##add_ntpdate
#echo "/usr/sbin/ntpdate clock.isc.org" >> /etc/rc.local
 
##update_date
/usr/sbin/ntpdate clock.isc.org -w
 
###clear files
rm -rf $snmp_file
rm -rf $monitor_file
rm -rf $config_file
rm -rf $vnstat_file
echo "----------------------------------------------------------------------"
echo "sleep for 5 seconds before reboot"
sleep 5
reboot

 

 

 

 

 

 

 

 

 

 

 

 

 

3. 公司的密码管理机:




   构思:有两台密码管理机。写一个程序将所有机器密码保存到数据库中。 数据库用mysql实现主从同步。(做到数据冗余的效果)

   预期结果:

      使用getpasswd 即可获取到你要登陆的机器密码。

      主机linux001 是密码获取机。linux002 是备用的。

   

Usage: getpasswd <user> <host|_host|-host> [setpwd|del]
             举例:
              1.获取密码:  
                [huangjiang@linux001 ~]$ getpasswd huangjiang linux001
                 huangjiang's password:                 #输入huangjiang 用户的密码获取
                 host linux001 root password: 123321
              2.修改机器密码:  
                 [huangjiang@linux001 ~]$ getpasswd huangjiang linux001 setpwd
                 huangjiang's password:                 #输入huangjiang 用户的密码获取
                 New password:                          #输入linux001的新密码
                 Retype new password:                   #重复输入linux001的新密码

 

4. 批量密码修改



[root@linuxtest bat_exec]# cat bat_exec_cmd.exp 

 
   
  
#!/usr/bin/expect -f
set file [ open "host_lists" r ]
set log [open sshlog a ]
proc do_command {new_pwd} {    
#expect "*#"        
#send "df -hT\r"
expect "*#"
send "echo $new_pwd|passwd --stdin root\r"
expect "*#"
send "exit\n"
}
 
while { [gets $file line] != -1 } {
    set host [lindex $line 0]
    set new_pwd [lindex $line 2]
    set old_pwd [lindex $line 1]
#    spawn ssh root@$host.sandai.net
     spawn ssh root@$host
    expect {
    "*(yes/no)*"  {send "yes\r";exp_continue}
    "*password:*" {send "$old_pwd\r"}
    "*Connection refused" { puts $log "$host is can not connect";continue;}
    "closed by remote host" { puts $log "$host is can not connect";continue;}
  }
 do_command $new_pwd
 interact
}
 
host_lists  文件格式(  机器名  旧密码 新密码
linux001 11111111 BOQOHVB5
linux002 22222222 BOQOHVB5
linux003 33333333 BOQOHVB5
linux004 12323233 BOQOHVB5


转载于:https://blog.51cto.com/netspace/1123169