# 基本环境配置

### 1. 配置主机名：

```properties
hostnamectl set-hostname k8s-master01
hostname
```

### 2. 修改 hosts 文件

```bash
echo '192.168.10.121 k8s-master01
192.168.10.122 k8s-node01
192.168.10.123 k8s-node02
192.168.10.124 k8s-master02
192.168.10.125 k8s-master03' >> /etc/hosts
```

```bash
cat /etc/hosts
```

### 3. 配置默认 yum 源：

{% code overflow="wrap" %}

```bash
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
```

{% endcode %}

```bash
yum install -y yum-utils device-mapper device-mapper-persistent-data lvm2
```

{% code overflow="wrap" %}

```bash
sed -i -e '/mirrors.aliyuncs.com/d' -e '/mirrors.cloud.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
```

{% endcode %}

检查 /etc/yum.repos.d/CentOS-Base.repo 内容：

```bash
cat /etc/yum.repos.d/CentOS-Base.repo
```

### 4. 安装一些常用的工具

```bash
yum install -y wget jq psmisc vim net-tools telnet git
```

### 5.  关闭防火墙、SELinux、DNSmasq

```bash
systemctl disable --now firewalld
systemctl disable --now dnsmasq
systemctl disable --now NetworkManager

setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
```

检查防火墙是否关闭：

```properties
systemctl status firewalld
```

### 6. 关闭Swap分区

```bash
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
```

### 7. 配置时间同步

安装 ntpdate：

```bash
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum install ntpdate -y
```

同步时间：

```bash
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' > /etc/timezone
ntpdate time2.aliyun.com
```

```properties
# crontab -e
*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com
```

检查定时任务是否配置成功：

```properties
# crontab -l
*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com
```

### 8. 配置limit

```bash
ulimit -SHn 65535
echo '# 末尾添加如下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited' >> /etc/security/limits.conf
```

### 9. 升级系统并重启

```bash
yum update -y  && reboot
```
