Kubernetes基于Centos7构建基础环境(一)

发布于 2023-08-18  179 次阅读


环境准备

  1. 准备一台Centos7虚拟机,本教程是使用VMware Workstation pro进行安装
  2. 镜像名称为:CentOS-7-x86_64-Minimal-2009.iso

一、 关闭防火墙、并设置开机不启动防火墙

[root@localhost /]# sudo systemctl stop firewalld && sudo systemctl disable firewalld

二、 开启远程访问

[root@localhost /]# sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config && systemctl restart sshd

三、 永久关闭selinux

修改/etc/sysconfig/selinux文件设置

[root@localhost /]# sudo sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
[root@localhost /]# sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
[root@localhost /]# sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

四、 禁用swap交换分区

打开/etc/fstab注释掉swap

[root@localhost /]# sudo sed -i 's/.*swap.*/#&/' /etc/fstab && sudo swapoff -a && swapon -a &&  sudo sysctl -p

五、 安装wget

提示Complete!则安装成功

[root@localhost ~]# sudo yum install -y wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================
 Package                 Arch                        Version                       Repository                                 Size
=========================================================================================================================================
Installing:
 wget                    x86_64                      1.14-18.el7_6.1               base                                       547 k
Transaction Summary
=========================================================================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                                                                                    | 547 kB  00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7_6.1.x86_64                                                                                         1/1
  Verifying  : wget-1.14-18.el7_6.1.x86_64                                                                                         1/1
Installed:
  wget.x86_64 0:1.14-18.el7_6.1

Complete!
[root@localhost ~]#

六、 更改系统默认镜像源为阿里镜像源

  1. 切换到/etc/yum.repos.d/下查看当前镜像源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ll
total 32
-rw-r--r--. 1 root root 1664 Nov 23  2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
[root@localhost ~]#
  1. 切换到/etc/yum.repos.d/目录下创建backup文件夹,并将上述镜像源移动到backup文件夹下
[root@localhost yum.repos.d]# sudo mkdir /etc/yum.repos.d/backup && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
[root@localhost yum.repos.d]# ll
total 0
drwxr-xr-x. 2 root root 187 Jun  4 04:48 backup
[root@localhost yum.repos.d]#
  1. 拉取阿里云镜像源,看见saved则拉取成功
[root@localhost yum.repos.d]# sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2020-06-04 04:48:58--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 124.200.113.116, 124.200.113.115, 219.238.20.87, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|124.200.113.116|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: '/etc/yum.repos.d/CentOS-Base.repo'

100%[======================================================================================================>] 2,523       --.-K/s   in 0s

2020-06-04 04:48:58 (264 MB/s) - '/etc/yum.repos.d/CentOS-Base.repo' saved [2523/2523]
[root@localhost yum.repos.d]#
  1. 查看目录结构
[root@localhost yum.repos.d]# ll
total 4
drwxr-xr-x. 2 root root  187 Jun  4 04:48 backup
-rw-r--r--. 1 root root 2523 Jun 15  2018 CentOS-Base.repo
[root@localhost yum.repos.d]#
  1. 执行yum clean all清空镜像源缓存
[root@localhost yum.repos.d]# sudo yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors
[root@localhost yum.repos.d]#
  1. 执行yum makecache重新加载缓存,看到Metadata Cache Created则加载成功
[root@localhost yum.repos.d]# sudo yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                  | 3.6 kB  00:00:00
extras                                                                                                                | 2.9 kB  00:00:00
updates                                                                                                               | 2.9 kB  00:00:00
(1/10): base/7/x86_64/group_gz                                                                                        | 153 kB  00:00:00
(2/10): extras/7/x86_64/filelists_db                                                                                  | 205 kB  00:00:00
(3/10): extras/7/x86_64/other_db                                                                                      | 122 kB  00:00:00
(4/10): base/7/x86_64/other_db                                                                                        | 2.6 MB  00:00:00
(5/10): updates/7/x86_64/primary_db                                                                                   | 1.3 MB  00:00:00
(6/10): updates/7/x86_64/filelists_db                                                                                 | 997 kB  00:00:00
(7/10): updates/7/x86_64/other_db                                                                                     | 192 kB  00:00:00
(8/10): base/7/x86_64/primary_db                                                                                      | 6.1 MB  00:00:01
(9/10): extras/7/x86_64/primary_db                                                                                    | 194 kB  00:00:00
(10/10): base/7/x86_64/filelists_db                                                                                   | 7.1 MB  00:00:02
Metadata Cache Created
[root@localhost yum.repos.d]#

七、 查看服务器IP地址(非必须

我个人的是在eth1下,对应的是192.168.1.40

[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 77402sec preferred_lft 77402sec
    inet6 fe80::5054:ff:fe8a:fee6/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:dd:2a:77 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.40/24 brd 192.168.1.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fedd:2a77/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost ~]#

八、 自定义服务器域名(非必须

也可以手动修改vi /etc/hostname(需要重启)

[root@localhost ~]# hostname yunze.base.com
[root@localhost ~]# hostname
yunze.base.com
[root@localhost ~]#

九、 添加/etc/hosts自定义域名映射(非必须

  1. 修改hosts配置
    • 增加 192.168.1.40 base.yunze.cn
[root@localhost ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.40 base.yunze.cn
[root@localhost ~]#
  1. 查看hosts配置
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.40 yunze.base.com
[root@localhost ~]#

十、 同步阿里云时间(墙裂建议)

  1. 安装ntp,提示Complete!则安装成功
[root@localhost ~]# sudo yum install -y ntp
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.ustc.edu.cn
base                                                                                                                  | 3.6 kB  00:00:00
extras                                                                                                                | 2.9 kB  00:00:00
updates                                                                                                               | 2.9 kB  00:00:00
(1/4): base/7/x86_64/group_gz                                                                                         | 153 kB  00:00:00
(2/4): extras/7/x86_64/primary_db                                                                                     | 194 kB  00:00:00
(3/4): updates/7/x86_64/primary_db                                                                                    | 2.1 MB  00:00:01
(4/4): base/7/x86_64/primary_db                                                                                       | 6.1 MB  00:00:05
Resolving Dependencies
--> Running transaction check
---> Package ntp.x86_64 0:4.2.6p5-29.el7.centos will be installed
--> Processing Dependency: ntpdate = 4.2.6p5-29.el7.centos for package: ntp-4.2.6p5-29.el7.centos.x86_64
--> Processing Dependency: libopts.so.25()(64bit) for package: ntp-4.2.6p5-29.el7.centos.x86_64
--> Running transaction check
---> Package autogen-libopts.x86_64 0:5.18-5.el7 will be installed
---> Package ntpdate.x86_64 0:4.2.6p5-29.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================================================================
 Package                     Arch                          Version                          Repository                   Size
=========================================================================================================================================
Installing:
 ntp                          x86_64                      4.2.6p5-29.el7.centos             base                        548 k
Installing for dependencies:
 autogen-libopts              x86_64                      5.18-5.el7                        base                         66 k
 ntpdate                      x86_64                      4.2.6p5-29.el7.centos             base                         86 k

Transaction Summary
=========================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 701 k
Installed size: 1.6 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY- ETA
Public key for ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm is not installed
(1/3): ntpdate-4.2.6p5-29.el7.centos.x86_64.rpm                                                                               |  86 kB  00:00:00
(2/3): autogen-libopts-5.18-5.el7.x86_64.rpm                                                                                  |  66 kB  00:00:00
(3/3): ntp-4.2.6p5-29.el7.centos.x86_64.rpm                                                                                   | 548 kB  00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                956 kB/s | 701 kB  00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : autogen-libopts-5.18-5.el7.x86_64                                                                             1/3
  Installing : ntpdate-4.2.6p5-29.el7.centos.x86_64                                                                          2/3
  Installing : ntp-4.2.6p5-29.el7.centos.x86_64                                                                              3/3
  Verifying  : ntp-4.2.6p5-29.el7.centos.x86_64                                                                              1/3
  Verifying  : ntpdate-4.2.6p5-29.el7.centos.x86_64                                                                          2/3
  Verifying  : autogen-libopts-5.18-5.el7.x86_64                                                                             3/3

Installed:
  ntp.x86_64 0:4.2.6p5-29.el7.centos
Dependency Installed:
  autogen-libopts.x86_64 0:5.18-5.el7                                     ntpdate.x86_64 0:4.2.6p5-29.el7.centos

Complete!
[root@localhost ~]#
  1. 查看当前系统时间、并设置当前时间为上海
[root@localhost /]# date
Thu Jun  4 05:28:48 UTC 2020

# 增加软连接,将时间拨回上海
[root@localhost /]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

[root@localhost /]# date
Thu Jun  4 13:29:01 CST 2020
[root@localhost /]# 
  1. 设置ntp,同步阿里云时间服务器
    • 执行vi /etc/ntp.conf
    • 找到server四行代码注释掉,在其下面增加server aliyun.com iburst
    • 然后通过sudo systemctl start ntpd启动服务,稍等一会执行ntpq -p查看是否同步,如果出现前面的*则,同步成功
    • systemctl start ntpd 启动ntp
    • systemctl restart ntpd 重启ntp
    • systemctl enable ntpd.service    开机启动
    • ntpdc -c loopinfo 查看与时间同步服务器的时间差
    • 注:如果硬件资源允许,单独搞一台服务器作为时间同步服务器,其他几台服务器同步此一台服务器即可
[root@localhost /]# vi /etc/ntp.conf

# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# server 2.centos.pool.ntp.org iburst
# server 3.centos.pool.ntp.org iburst

# 增加阿里云时间同步服务器
server ntp.aliyun.com iburst

[root@localhost /]#
[root@localhost /]# sudo systemctl start ntpd
[root@localhost /]# systemctl enable ntpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost /]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*203.107.6.88    10.137.38.86     2 u   37   64    1   53.476   -5.668   2.224
[root@localhost /]#

十一、 升级内核

  1. 载入公钥, 安装 ELRepo 最新版本
[root@localhost /]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
[root@localhost /]# yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
  1. 列出可以使用的 kernel 包版本
[root@localhost /]# yum list available --disablerepo=* --enablerepo=elrepo-kernel
  1. 安装指定的 kernel 版本:
[root@localhost /]# yum install -y kernel-lt-5.4.238-1.el7.elrepo --enablerepo=elrepo-kernel
  1. 查看系统可用内核 5.4.238
[root@localhost /]# cat /boot/grub2/grub.cfg | grep menuentry

menuentry 'CentOS Linux (3.10.0-1062.el7.x86_64) 7 (Core)' --class centos (略)
menuentry 'CentOS Linux (5.4.238-1.el7.elrepo.x86_64) 7 (Core)' --class centos
...(略)
  1. 设置开机从新内核启动
[root@localhost /]# grub2-set-default "CentOS Linux (5.4.238-1.el7.elrepo.x86_64) 7 (Core)"
  1. 查看内核启动项
[root@localhost /]# grub2-editenv list
saved_entry=CentOS Linux (5.4.238-1.el7.elrepo.x86_64) 7 (Core)
  1. 重启系统使内核生效
[root@localhost /]# reboot
  1. 启动完成查看内核版本是否更新
[root@localhost /]# uname -r
5.4.238-1.el7.elrepo.x86_64

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。