1 基础配置和组件安装(所有节点)
1.1设置主机名hostname
# 设置主机名(三台主机名分别为 master-01,node-01,node-02)
hostnamectl set-hostname master-01
1.2 添加域名解析
# 域名解析
cat <<EOF>>/etc/hosts
27.1.100.128 master-01
27.1.100.129 node-01
27.1.100.130 node-02
EOF
1.3 关闭防火墙
# 关闭防火墙
systemctl stop firewalld
# 关闭防火墙开机启动
systemctl disable firewalld
1.4 禁用SELinux
# 临时关闭
setenforce 0
# 永久关闭
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
1.5 关闭系统Swap
临时关闭Swap和永久关闭Swap
# 临时关闭
swapoff -a
# 永久关闭
sed -i 's/.*swap.*/#&/' /etc/fstab
1.6 IPv4流量传递到iptables
将桥接的IPv4流量传递到iptables的链
#将桥接的 IPv4 流量传递到 iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 让流量传递到生效
sysctl --system
1.7 拉取源
# 拉取docker源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# 拉取阿里yum源
wget -O/etc/yum.repos.d/aliyun-yilai.repo http://mirrors.aliyun.com/repo/Centos-7.repo
1.8 安装docker
# 安装docker
yum -y install docker-ce-18.06.1.ce-3.el7
# 启动并开机自启
systemctl enable docker && systemctl start docker
# 配置docker镜像源
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://kcniy8yf.mirror.aliyuncs.com"],
"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
# 重启docker服务
systemctl daemon-reload
systemctl restart docker
1.9 安装Kubernetes组件
# 配置国内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
# 安装Kubernetes组件依赖
yum -y install conntrack
# 安装Kubernetes组件
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# 开机启动kubelet
systemctl enable kubelet
2 初始化master节点(主节点)
2.1 初始化主节点
kubeadm init --apiserver-advertise-address=27.1.100.128 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
# 参数说明
# --apiserver-advertise-address=27.1.100.128(master主机地址)
# --image-repository registry.aliyuncs.com/google_containers (镜像获取地,换为阿里源)
# --kubernetes-version v1.18.0 (集群版本)
# --service-cidr=10.96.0.0/12
# --pod-network-cidr=10.244.0.0/16
2.2 添加kubectl命令的变量
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
2.3 安装网络组件
可以直接用
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
搞定,但是 raw.githubusercontent.com已经被墙了,如果可以*,上面可以直接搞定,如果不能*,这里放上kube-flannel.yml文件的源码
由于源码太多,放在CSDN的页面上面,复制下就可以
https://blog.csdn.net/qq_22409661/article/details/113371921
生成文件后,在文件所有目录执行
kubectl apply -f kube-flannel.yml
3 把node节点添加到主节点(计算节点)
# 这里只是做一个比方,token在24小时后就会失效,需要重新生成
kubeadm join 27.1.100.130:6443 --token r2jjze.n9b8yf1qacr3dnab \
--discovery-token-ca-cert-hash sha256:c8b905cd622ad97f11a0e15012089974dd2878dbc2a36122542fdf91c56cea45
# 重新生成token
kubeadm token create --print-join-command
4 快速部署方法
上面的方法是分解了步骤,我们可以利用下面的方法快速部署
4.1 所有节点执行
# 角色ip地址
master_01=27.1.100.128
node_01=27.1.100.129
node_02=27.1.100.130
# 设置主机名
temp=$(ifconfig ens33 | grep "inet " | awk -F " " '{print $2}')
if [ $temp = $master_01 ];then
hostnamectl set-hostname master-01
elif [ $temp = $node_01 ];then
hostnamectl set-hostname node-01
elif [ $temp = $node_02 ];then
hostnamectl set-hostname node-02
fi
# 域名解析
cat <<EOF>>/etc/hosts
$master_01 master-01
$node_01 node-01
$node_02 node-02
EOF
# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
# 禁用虚拟内存
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 禁用SELinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#将桥接的 IPv4 流量传递到 iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 让流量传递到生效
sysctl --system
# 时间同步
yum install ntpdate -y
ntpdate time.windows.com
# 拉取docker源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# 拉取阿里yum源
wget -O/etc/yum.repos.d/aliyun-yilai.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 安装docker
yum -y install docker-ce-18.06.1.ce-3.el7
# 启动并开机自启
systemctl enable docker && systemctl start docker
# 配置docker镜像源
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://kcniy8yf.mirror.aliyuncs.com"],
"exec-opts":["native.cgroupdriver=systemd"]
}
EOF
# 重启docker服务
systemctl daemon-reload
systemctl restart docker
# 配置国内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
# 安装Kubernetes组件依赖
yum -y install conntrack
# 安装Kubernetes组件
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
# 开机启动kubelet
systemctl enable kubelet