前言:
Debian篇请点击链接访问---->Debian篇
一、系统环境
[root@node1 ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
二、系统配置
2.1、关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2.2、关闭selinux
2.2.1、查看selinux状态
sestatus
2.2.2、配置selinux文件
查看selinux配置文件
cat /etc/sysconfig/selinux
cat /etc/selinux/config
设置关闭selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux /etc/selinux/config
因为selinux属于安全服务,所以需要重启系统才能生效,如果不想重启系统,可以选择临时关闭,但是可能会有莫名其妙的错误
临时关闭 命令:
setenforce 0
2.3、关闭swap
swapoff -a
sudo sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab #永久
2.4、内核转发
sudo echo -e "net.bridge-nf-call-ip6tables = 1\nnet.bridge-nf-call-iptables = 1\nnet.ipv4.ip_forward = 1" > /etc/sysctl.conf
sysctl --system
三、配置源
3.1、配置系统源
3.1.1、备份源文件
rm -rf /yum_bak&&mkdir -p /yum_bak
mv /etc/yum.repos.d/* /yum_bak/&&ls /yum_bak/
3.1.2、添加阿里Centos7源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
3.1.3、清除源缓存
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache
3.2、添加Dcoker源
3.2.1、添加阿里Docker源文件
yum install -y yum-utils device-mapper-persistent-data lvm2 -q
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat /etc/yum.repos.d/docker-ce.repo
3.2.2、验证Docker源
yum clean all
yum makecache
查看当前源仓库列表
yum repolist
3.3、添加K8S源
3.3.1、添加阿里kubernetes源
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
cat /etc/yum.repos.d/kubernetes.repo
3.3.2、验证源配置
yum clean all
yum makecache
注意,建立K8S的索引的时候,会提示GPG导入的提示,输入小写y回车即可
3.4、关闭所有源检测(可选)
理论关闭之后安装服务会更快
sed -i 's/gpgcheck=.*/gpgcheck=0/g' /etc/yum.repos.d/*.repo
四、安装服务
4.1、安装Docker-ce及K8s服务
由于网络插件主要在K8SV1.17.0版本上测试成功过,所以建议使用固定版本进行安装
yum install -y docker-ce kubelet kubectl kubeadm
到这里,服务安装就完成了
五、配置服务
5.1、配置Docker
5.1.1、启动Docker服务
systemctl start docker
5.1.2、添加镜像加速地址及驱动更改
echo -e "{\n\t\"exec-opts\": [\"native.cgroupdriver=systemd\"],\n\t\"registry-mirrors\": [\"http://hub-mirror.c.163.com\"]\n}" > /etc/docker/daemon.json
cat /etc/docker/daemon.json
5.1.3、查看Docker信息
systemctl restart docker
docker info
5.2、加入K8S集群
5.2.1、设置服务开机自启
systemctl enable docker.service kubelet
5.2.2、设置hosts主机映射
查看master节点IP地址
查看node1节点IP地址
写入主机映射
echo -e "10.1.1.6 master\n10.1.1.3 node1" >> /etc/hosts
cat /etc/hosts
5.2.3、从master节点获取注册命令
查看token列表(master)
kubeadm token list
删除所有token
kubeadm token delete ase2do.81uoht529t59h6xf
kubeadm token delete noqayg.ktjwrge2wuei54eo
删除的值属于随机,请根据实际情况进行替换
添加token
kubeadm token create
kubeadm token create --print-join-command
生成注册命令
kubeadm token create --print-join-command
然后回到node节点,重启K8S服务
重启出错,提示需要重新加载配置文件,解决办法如下:
systemctl daemon-reload&&systemctl restart docker kubelet
重启之后,重新注册K8S集群
此时,K8S服务重启启动了,但是注册还是提示错误,解决办法如下:
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
然后继续注册
kubeadm join 10.1.1.6:6443 --token spe0vk.8hzmo89y8z5cjh9a --discovery-token-ca-cert-hash sha256:297aaa6fd16faf71f979c57a9fc4e6ceea4af87d9eeb4de9199df1a353a2e59a
成功终于到来
六、验证集群
6.1、获取集群节点信息(master)
kubectl get nodes