Centos7

  • Linux 5.4.138-1.el7.elrepo.x86_64

docker

  • 20.10.8

Kubernetes

  • v1.21.3
  • flannel v0.14.0

nodes

一.Centos系统优化 -> all nodes

1.更换yum源

首先在国内镜像源,如华为源下载个Centos7的minimal iso镜像,安装后,先更换为阿里云的国内yum源。

# 备份原系统的yum源
cd /etc/yum.repo.d
mkdir bak
mv *.repo bak/

# 下载阿里云的yum源
yum install -y wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum update
yum upgrade

# 新装的server没有ifconfig命令
yum install -y net-tools

2.配置静态ip

本实验基于NAT模式新建三台虚拟机,以master节点为例,其他从节点类似。

# 查看网卡 -> 通常是 ens33
ifconfig | ip addr

# 修改配置文件 
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak
vi /etc/sysconfig/network-scripts/ifcfg-ens33
---修改后---
BOOTPROTO="static"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.181.135
NETMASK=255.255.255.0
GATEWAY=192.168.181.2
DNS1=114.114.114.114
-----------

# 重启网卡
service network restart
ifconfig | ip addr 

3.配置hostname

永久修改主机名,以master为例,其他类似。

# master节点,node1 node2同样设置hostname
hostnamectl set-hostname master

# exit 后重新 login

# 修改hosts文件,追加模式
cat >> /etc/hosts << EOF
> 192.168.181.135 master
> 192.168.181.136 node1
> 192.168.181.137 node2
> EOF

# 分发hosts文件,输入密码后确认
scp /etc/hosts  node1:/etc/hosts 
scp /etc/hosts  node2:/etc/hosts 

4.配置ssh互信

在master节点上配置,然后分发即可

# 生成公私钥,一路回车
ssh-keygen

# 分发,不指定公钥默认用 /root/.ssh/id_rsa.pub
ssh-copy-id root@node1
ssh-copy-id root@node2

# 验证ssh互信,首次选择yes
ssh node1
ssh node2

5..升级内核

Centos7的kernel版本为3.10,运行docker和k8s的时候,会出错,升级较新版本的kernel。

# 查看内核信息
uname -rs 

# elrepo pub key
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

# install elrepo, choose u own version
yum install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm

# 查看内核版本
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

# 安装内核 lt代表long term 长期支持版本
yum --disablerepo='*' --enablerepo=elrepo-kernel install kernel-lt

# 查看配置
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
# 0 : CentOS Linux (4.4.186-1.el7.elrepo.x86_64) 7 (Core)
# 1 : CentOS Linux (3.10.0-957.27.2.el7.x86_64) 7 (Core)
# 2 : CentOS Linux (3.10.0-957.21.3.el7.x86_64) 7 (Core)

# 默认用更新的内核,重启主机
grub2-set-default 0 && reboot

6.脚本运行-> set_env.sh

#!/bin/bash

# 1.配置国内源
cd /etc/yum.repo.d
mkdir bak
mv *.repo bak/

yum install -y wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

yum clean all
yum makecache
yum update
yum upgrade

yum install -y net-tools

# 2.配置静态IP
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens33.bak

cat > /etc/sysconfig/network-scripts/ifcfg-ens33 << EOF
BOOTPROTO="static"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.181.135
NETMASK=255.255.255.0
GATEWAY=192.168.181.2
DNS1=114.114.114.114
EOF

service network restart

# 3.配置hostname,手动分发hosts文件
hostnamectl set-hostname master


cat >> /etc/hosts << EOF
192.168.181.135 master
192.168.181.136 node1
192.168.181.137 node2
EOF

# 4.ssh互信参考上面,下同

# 5.升级内核
二.docker安装 -> all nodes

1.清理docker环境

# rm docker already occupied env
yum remove -y docker*

# cfg docker yum source && docker dependencies
yum install -y yum-utils   device-mapper-persistent-data   lvm2

# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# up yum cache
yum makecache fast

2.查看docker版本

yum list docker-ce --showduplicates | sort -r

3.安装docker

# install specific version docker
yum install -y docker-ce-20.10.8 

# 开机自启
systemctl start docker
systemctl enable docker


# 验证docker
docker run hello-world
docker rm hello-world_containerName
docker rmi hello-world:latest

4.配置docker源

由于国内网络问题,需要配置加速器来加速。
需要修改配置文件,Docker 使用 /etc/docker/daemon.json来配置daemon,同时修改启动参数cgroup driversystemd

vi /etc/docker/daemon.json
---begin---
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}
---end---

systemctl daemon-reload
systemctl restart docker

# check
docker info | grep Cgroup 
三.kubernetes安装

1.配置k8s环境 -> all nodes

我写成脚本形式,k8s_env.sh,如下:

#!/bin/bash
# 关闭防火墙
systemctl status firewalld
systemctl disable firewalld
systemctl stop firewalld

# 关闭selinux
# 临时禁用selinux
setenforce 0
# 永久关闭 修改/etc/sysconfig/selinux文件设置
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

# 禁用交换分区
swapoff -a
# 永久禁用,打开/etc/fstab注释掉swap那一行。
sed -i 's/.*swap.*/#&/' /etc/fstab

# 修改内核参数
cat <<EOF >/etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

sysctl --system # sysctl -p

执行时

chmod 755 k8s_env.sh
./k8s_env.sh

2.master节点安装k8s组件 -> master node

同样,这部分脚本形式,k8s_install.sh,如下:

#/bin/bash

K8S_BASEURL=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
K8S_GPGKEY="https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg"
POD_NETWORK=10.244.0.0

# 执行配置k8s阿里云源
cat <<EOF >/etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=${K8S_BASEURL}
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=${K8S_GPGKEY}
EOF

# 更新yum源
yum clean all
yum -y makecache

# 安装kubeadm、kubectl、kubelet
yum install -y kubectl-1.21.3-0 kubeadm-1.21.3-0 kubelet-1.21.3-0

# 启动kubelet服务
systemctl enable kubelet && systemctl start kubelet

执行时

chmod 755 k8s_install.sh
./k8s_install.sh

3.从节点安装 -> all work nodes

同样,这部分脚本形式,k8s_install_node.sh,如下:

#/bin/bash

K8S_BASEURL=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
K8S_GPGKEY="https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg"

cat <<EOF >/etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=${K8S_BASEURL}
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=${K8S_GPGKEY}
EOF

# 更新yum源
yum clean all
yum -y makecache

# 安装kubeadm、kubectl、kubelet  version=v1.21.3
yum install -y kubectl-1.21.3-0 kubeadm-1.21.3-0 kubelet-1.21.3-0

# 启动kubelet服务
systemctl enable kubelet && systemctl start kubelet

执行时

chmod 755 k8s_install_node.sh
./k8s_install_node.sh