安装Python
yum clean all
yum makecache
yum -y install gcc make zlib-devel openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
yum -y install libSM libXrender libXext libffi-devel wget
wget -P /home/ "https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz"
tar -xvf /home/Python-3.7.9.tgz -C /home/
cd /home/Python-3.7.9
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/local/python3/bin/python
/usr/local/python3/bin/pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
rm -f /usr/bin/pip
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
echo export PATH=/usr/local/python3/bin:PATH >> /etc/profile.d/source_profile.sh
rm -rf /home/*
yum clean all
rm -rf /var/cache/yum
vi /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
cat >> /etc/hosts << EOF
192.168.137.130 K8SMASTER
192.168.137.131 K8SNODE1
192.168.137.132 K8SNODE2
EOF
更换yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
配置安装k8s需要的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
按照必备组件
yum -y install wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack bind-utils libffi-devel device-mapper-persistent-data lvm2 yum-utils ntpdate
关闭防火墙
systemctl disable firewalld && systemctl stop firewalld
yum install -y iptables-services
iptables -F && service iptables save
service iptables stop && systemctl disable iptables
同步时间
ntpdate -u cn.pool.ntp.org
关闭selinux
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
关闭swap
swapoff -a
sed -i 's/.swap./#&/' /etc/fstab
配置阿里云的docker源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
清理缓存
yum clean all
yum makecache fast
安装和启动docker
yum install docker-ce-19.03.12 docker-ce-cli-19.03.12 containerd.io
systemctl enable docker && systemctl start docker
修改Docker配置
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"]
"oom-score-adjust": -1000,
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
},
"max-concurrent-downloads": 10,
"max-concurrent-uploads": 10,
"registry-mirrors": ["https://2inti5wg.mirror.aliyuncs.com"],
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF
systemctl daemon-reload && systemctl restart docker
开启k8s网络桥接相关内核配置
设置网桥包经IPTables,core文件生成路径,配置永久生效
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 >/proc/sys/net/bridge/bridge-nf-call-ip6tables
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
cat >> /etc/sysctl.conf << EOF
vm.swappiness = 0
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl -p
开启ipvs,不开启ipvs将会使用iptables,但是效率低,所以官网推荐需要开通ipvs内核
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
/sbin/modinfo -F filename ${kernel_module} > /dev/null 2>&1
if [ {kernel_module}
fi
done
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules
bash /etc/sysconfig/modules/ipvs.modules
lsmod | grep ip_vs
安装网络
curl -o calico.yaml https://docs.projectcalico.org/v3.18/manifests/calico.yaml
下载镜像
docker pull docker.io/calico/cni:v3.18.5
docker pull docker.io/calico/pod2daemon-flexvol:v3.18.5
docker pull docker.io/calico/node:v3.18.5
docker pull docker.io/calico/kube-controllers:v3.18.5
kubectl apply -f calico.yaml
卸载
kubectl delete -f calico.yaml
安装K8S
yum install kubeadm-1.18.2 kubelet-1.18.2 kubectl-1.18.2 -y && systemctl enable kubelet && systemctl start kubelet
启动主节点
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.2 --apiserver-advertise-address 192.168.137.130 --pod-network-cidr=10.244.0.0/16 --token-ttl 0
重置
kubeadm reset
rm -rf ~/.kube/
rm -rf /etc/kubernetes/
rm -rf /var/lib/kubelet/
rm -rf /var/lib/etcd
rm -rf /var/lib/dockershim
rm -rf /var/run/kubernetes
rm -rf /var/lib/cni
rm -rf /etc/cni/net.d