Linux基础

hosts

1
2
3
4
linux:
/etc/hosts
windows
C:\Windows\System32\drivers\etc

ssh

1
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

安装依赖组

1
2
3
[root@node0 ~]# yum grouplist
[root@node0 ~]# yum groupinfo
[root@node0 ~]# yum groupinstall 开发工具

端口占用

netstat -anp|grep 80

解压缩

tar 解压缩命令 :

c: 建立压缩档案
-x:解压
-t:查看内容
-r:向压缩归档文件末尾追加文件
-u:更新原压缩包中的文件
这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时可选的。
-z:有gzip属性的
-j:有bz2属性的
-Z:有compress属性的
-v:显示所有过程
-O:将文件解开到标准输出
下面的参数-f是必须的
-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。

更改主机名

查看

1
2
3
4
5
6
7
8
9
10
11
xyxj@u18_data1:~$ hostnamectl
Static hostname: Master #静态主机名
Transient hostname: u18_data1 #临时主机名
Icon name: computer-vm
Chassis: vm
Machine ID: 67f22187487a4bbd893048a9f7b4bb13
Boot ID: ce03c7b8874e4b75ab0bb71c886fd3fe
Virtualization: vmware
Operating System: Ubuntu 18.10
Kernel: Linux 4.18.0-13-generic
Architecture: x86-64

操作

1
2
xyxj@u18_data1:~$ sudo hostname Master-name		#更改临时主机名
xyxj@u18_data1:~$ sudo hostnamectl set-hostname Master #更改静态主机名

更改CentOS默认的yum源

1
2
3
4
5
6
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# centos-7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# cache
yum makecache

高并发和负载均衡

名词:

  • VIP: 虚拟服务器地址
  • DIP: 转发的网络地址
    • 1, 和RIP通信: ARP协议, 获取Real Server的RIP; MAC地址
    • 2.转发Client的数据包到RIP上-隐藏VIP
  • RIP: 后端真是主机
  • CIP: 客户IP地址

原则: 三次握手到四次分手不可分割

配置负载均衡

  • 准备三台centos的虚拟机

  • 配置虚拟机网络

    • eth0, 配置同一网段
    • DIP,RIP在同一网段
  • 配置lvs的VIP

    • ifconfig eth0:8 192.168.80.100/24
    • echo 1 > /proc/sys/net/ipv4/ip_forward
  • 调整RS的响应通告级别 (每台RS)

    • echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
    • echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
    • echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    • echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
  • 配置RS的VIP

    • ifconfig lo:8 192.168.80.100 netmask 255.255.255.255 (每台RS)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    [root@node03 ~]# ifconfig lo:8 192.168.80.100 netmask 255.255.255.255
    [root@node03 ~]# ifconfig
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 192.168.80.134 netmask 255.255.255.0 broadcast 192.168.80.255
    inet6 fe80::14d3:34b3:cbe:db15 prefixlen 64 scopeid 0x20<link>
    ether 00:0c:29:b5:48:2d txqueuelen 1000 (Ethernet)
    RX packets 344 bytes 33631 (32.8 KiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 303 bytes 37666 (36.7 KiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
    inet 127.0.0.1 netmask 255.0.0.0
    inet6 ::1 prefixlen 128 scopeid 0x10<host>
    loop txqueuelen 1000 (Local Loopback)
    RX packets 0 bytes 0 (0.0 B)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 0 bytes 0 (0.0 B)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    lo:8: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
    inet 192.168.80.100 netmask 255.255.255.255
    loop txqueuelen 1000 (Local Loopback)

    [root@node03 ~]# route -n
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    0.0.0.0 192.168.80.2 0.0.0.0 UG 100 0 0 eth0
    192.168.80.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

注意: 如果-bash: ifconfig: 未找到命令

yum upgrade | yum install net-tools

此时centos的网络是ens33不是eth0, 看下网卡符号:

1
2
3
4
5
6
7
8
9
en  以太网    Ethernet
wl  无线局域网  WLAN
ww  无线广域网  WWAN
# 第三个符号
o<index> on-board device index number
s<slot> hotplug slot index number
x<MAC> MAC address
p<bus>s<slot> PCI geographical location
p<bus>s<slot> USB port number chain

更改ens33为eth0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@node01 ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.133 netmask 255.255.255.0 broadcast 192.168.80.255
inet6 fe80::37e7:f8eb:43c2:b25d prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ad:18:5f txqueuelen 1000 (Ethernet)
RX packets 68 bytes 8216 (8.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 102 bytes 11726 (11.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@node01 ~]# vim /etc/sysconfig/grub
# GRUB_CMDLINE_LINUX="...net.ifnames=0 biosdevname=0 rhgb quiet" 添加
# GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap net.ifnames=0 biosdevname=0 rhgb quiet"
[root@node01 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-957.1.3.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.1.3.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-fc191819a9854dbba98e8e06dbe41ee2
Found initrd image: /boot/initramfs-0-rescue-fc191819a9854dbba98e8e06dbe41ee2.img
done
[root@node01 ~]# cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak

[root@node01 ~]# mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-eth0

[root@node01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

[root@node01 ~]# ls /etc/sysconfig/network-scripts/
ifcfg-ens33.bak ifdown-eth ifdown-ppp ifdown-tunnel ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6
ifcfg-eth0 ifdown-ippp ifdown-routes ifup ifup-ipv6 ifup-ppp ifup-tunnel
ifcfg-lo ifdown-ipv6 ifdown-sit ifup-aliases ifup-isdn ifup-routes ifup-wireless
ifdown ifdown-isdn ifdown-Team ifup-bnep ifup-plip ifup-sit init.ipv6-global
ifdown-bnep ifdown-post ifdown-TeamPort ifup-eth ifup-plusb ifup-Team network-functions

[root@node01 ~]# systemctl restart network.service
Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.

[root@node01 ~]# systemctl status network.service

出错了,重启虚拟机, 这个时候的网卡名称已经更改,虚拟IP地址也更改了.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@node01 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.135 netmask 255.255.255.0 broadcast 192.168.80.255
inet6 fe80::3e9a:38cd:a02:a4c6 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ad:18:5f txqueuelen 1000 (Ethernet)
RX packets 76 bytes 8657 (8.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 108 bytes 12252 (11.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  • 在rs上启动httpd

    • yum install httpd -y
    • cd /var/www/html
      • vim index.html
      • from 如果id ip
    • systemctl start httpd.service
      • 验证 RIP
      • VIP不显示

    如果访问不到就需要关闭虚拟机的防火墙

    1
    systemctl stop firewalld.service
  • 配置lvs

    • yum install ipvsadm -y
    • ipvsadm -A -t 192.168.80.100:80 -s rr
    • ipvsadm -a -t 192.168.80.100:80 -r 192.168.80.134 -g
    • ipvsadm -a -t 192.168.80.100:80 -r 192.168.80.136 -g
    • ipvsadm -ln
    • 浏览器刷新
    • ipvsadm -lnc
    • netstat -natp

Keepalived

解决单点故障

删除多余内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# look
[root@node01 ~]# uname -sr
Linux 3.10.0-957.1.3.el7.x86_64
[root@node01 ~]# rpm -qa | grep kernel
kernel-tools-libs-3.10.0-957.1.3.el7.x86_64
kernel-tools-3.10.0-957.1.3.el7.x86_64
kernel-3.10.0-957.1.3.el7.x86_64
kernel-headers-3.10.0-957.1.3.el7.x86_64
kernel-3.10.0-957.el7.x86_64
[root@node01 ~]# rpm -e kernel-3.10.0-957.el7.x86_64
[root@node01 ~]# rpm -qa | grep kernel
kernel-tools-libs-3.10.0-957.1.3.el7.x86_64
kernel-tools-3.10.0-957.1.3.el7.x86_64
kernel-3.10.0-957.1.3.el7.x86_64
kernel-headers-3.10.0-957.1.3.el7.x86_64
[root@node01 ~]#
Donate comment here