CentOS-7.9通过kubeadm搭建k8s集群(解决国内无法下载flannel镜像及yaml配置问题)
环境说明
虚拟机软件:VmWare16pro
虚拟机系统:CentOS-7.9
虚拟机配置:2核CPU+2G内存+50G硬盘
k8s版本:v1.17.4
docker版本:docker-ce-18.06.3.ce-3.el7
集群规划
1个模板机+1主2从的k8s集群
模板机用来做系统初始化操作,之后克隆出1主2从的k8s集群,然后通过kubeadm搭建k8s集群
模板机IP:10.0.0.99
master IP:10.0.0.100
node1 IP:10.0.0.101
node2 IP:10.0.0.102
VmWare16pro安装CentOS-7.9模板机
设置VmWare网络
- 点击菜单栏编辑-虚拟网络编辑器
- 选中VMnet8,NAT模式,点击右下角更改设置
- 子网IP设为10.0.0.0,子网掩码设为255.255.255.0
- 点击NAT设置,网关地址设置为10.0.0.254
- 宿主机VMnet8地址设置为10.0.0.1,自动跃点调到10
最小化安装CentOS-7.9
- 官网下载CentOS-7.9镜像iso
- 新建虚拟机,2核CPU+2G内存+50G硬盘
- 选择最小化安装
设置模板机网络
模板机IP地址设置为10.0.0.99
vi /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.0.0.99
GATEWAY=10.0.0.254
NETMASK=255.255.255.0
DNS1=10.0.0.254
关闭NetworkManager (重要)
systemctl disable NetworkManager
systemctl stop NetworkManager
systemctl restart network
#保证能ping通baidu.com
设置主机名
vi /etc/hostname
CentOS-7.9-Base
#或者用下面命令
hostnamectl set-hostname CentOS-7.9-Base
#设置完可以hostname查看主机名
hostname
安装常用命令
yum install -y wget net-tools telnet tree nmap sysstat lrzsz dos2unix bind-utils vim less
yum -y install bash-completion
yum -y install epel-release
yum -y install yum-plugin-fastestmirror
source /etc/profile.d/bash_completion.sh
切换阿里云的源
mkdir -p /etc/yum.repos.d/back/
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/back/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#iptables本来就没用到,无需处理
#yum -y install iptables-services
#systemctl start iptables
#systemctl enable iptables
#iptables -F
#service iptables save
关闭selinux
cd /etc/selinux
vi /etc/selinux/config
#找到 SELINUX=enforcing 改成 SELINUX=disabled
关闭swap分区
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
#其实就是找到swap那一行,在开头加#
调整时区
# 设置系统时区为中国/上海
timedatectl set-timezone Asia/Shanghai
# 将当前的UTC时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
关闭系统不需要的服务
systemctl stop postfix
systemctl disable postfix
设置系统默认日志systemd journald(原本为rsyslogd )
#持久化保存日志的目录
mkdir /var/log/journal
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
# 最大占用空间10G
SystemMaxUse=10G
# 单日志文件最大200M
SystemMaxFileSize=200M
# 日志保存时间2周
MaxRetentionSec=2week
# 不将日志转发到syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald
配置k8s参数
vi /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
#刷新配置
sysctl -p /etc/sysctl.d/kubernetes.conf
#加载网桥过滤模块
modprobe br_netfilter
lsmod |grep br_netfilter
sysctl -p /etc/sysctl.d/kubernetes.conf
配置ipvs功能
在kubernetes中service有两种代理模型,一种是基于iptables,一种是基于ipvs,ipvs的性能更好
#安装ipset
yum install ipset -y
#添加需要加载的模块写入脚本文件
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
#添加权限
chmod 755 /etc/sysconfig/modules/ipvs.modules
#执行脚本
/bin/bash /etc/sysconfig/modules/ipvs.modules
#查看模块是否加载成功
lsmod |grep -e ipvs -e nf_conntrack_ipv4
#至此CentOS-7.9模板机及就搭建好了
创建k8s集群
初始化1主2从虚拟机
克隆出1主2从虚拟机
通过CentOS-7.9模板机克隆出1主2从虚拟机,IP地址如下
master IP:10.0.0.100
node1 IP:10.0.0.101
node2 IP:10.0.0.102
分别修改3台机器的ip地址
vi /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=10.0.0.100
IPADDR=10.0.0.101
IPADDR=10.0.0.102
#保证能ping通baidu.com
分别修改3台机器主机名
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2
修改3台机器的hosts文件
#通过Mobaxterm可以同时在多个终端执行命令 MultiExec
vi /etc/hosts
10.0.0.100 master
10.0.0.101 node1
10.0.0.102 node2
安装docker
3台机器切换阿里云docker镜像源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
#查看docker有哪些可用版本镜像
yum list docker-ce --showduplicates
3台机器安装docker
yum install --setopt=obsoletes=0 docker-ce-18.06.3.ce-3.el7 -y
3台机器设置阿里云docker镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://zhjxhme4.mirror.aliyuncs.com"]
}
EOF
3台机器设置docker开机自启
systemctl start docker
systemctl enable docker
安装k8s基础组件
3台机器设置k8s镜像源
vim /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
3台机器安装kubeadm、kubelet、kubectl
yum install --setopt=obsoletes=0 kubeadm-1.17.4-0 kubelet-1.17.4-0 kubectl-1.17.4-0 -y
3台机器配置kubelet的cgroup,并设置开机自启
vi /etc/sysconfig/kubelet
KUBELET_CGROUP_ARGS="--cgroup-driver=systemd"
KUBE_PROXY_MODE="ipvs"
systemctl enable kubelet
3台机器从阿里云下载k8s镜像
images=(
kube-apiserver:v1.17.4
kube-controller-manager:v1.17.4
kube-scheduler:v1.17.4
kube-proxy:v1.17.4
pause:3.1
etcd:3.4.3-0
coredns:1.6.5
)
for imageName in ${images[@]}; do
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done
master节点执行kubeadm init
kubeadm init \
--kubernetes-version=v1.17.4 \
--pod-network-cidr=10.244.0.0/16 \
--service-cidr=10.96.0.0/12 \
--apiserver-advertise-address=10.0.0.100
执行成功会看到如下信息:
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 10.0.0.100:6443 --token pe3ge3.jsluu3wpjcks32ry \
--discovery-token-ca-cert-hash sha256:fb0be4c2c3a15e8e7e856619d53f9f21fb582fd17b647fadb752d8d88ed8dd59
根据提示,在master节点执行如下命令启动k8s集群
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
在master节点配置k8s集群网络
#先跳过,下面单独章节讲
在2个node节点加入k8s集群
kubeadm join 10.0.0.100:6443 --token pe3ge3.jsluu3wpjcks32ry \
--discovery-token-ca-cert-hash sha256:fb0be4c2c3a15e8e7e856619d53f9f21fb582fd17b647fadb752d8d88ed8dd59
默认token的有效期为24小时,当过期之后,该token就不可用了. 如果后续有nodes节点加入,解决方法如下:
#重新生成新的token
#kubeadm token create
#kubeadm token list
#获取ca证书sha256编码hash值
#openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
安装网络插件flannel
下载flannel yaml配置文件
#官网地址国内访问不了:https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#可以去这里下载
https://download.csdn.net/download/l229568441/16734444
#或者这个地址下载
https://blog.csdn.net/zhou_zhao_xu/article/details/106790396
下载flannel docker镜像
#国内无法访问flannel镜像,我尝试了七牛云,阿里云都下载不了
#最后去github下载docker镜像,然后手动导入docker仓库
#推荐迅雷下载,浏览器容易下一半下不完
https://github.com/flannel-io/flannel/releases/download/v0.12.0/flanneld-v0.12.0-amd64.docker
#上传到3台虚拟机
3台机器把flannel镜像导入docker仓库
docker load < flanneld-v0.12.0-amd64.docker
master机器运行flannel插件
kubectl apply -f kube-flannel.yml
查看k8s集群状态
#查看所有节点是否ready
kubectl get nodes
#查看flannel节点是否运行正常
kubectl get pod -n kube-system | grep flannel
#查看kube-system启动的所有pod
kubectl get pod -n kube-system