1. 环境准备

        1.1 准备三台服务器(这里演示用最低配置)

hostnameip配置部署节点
host101192.168.125.1012C4Gmaster
host102192.168.125.1022C4Gnode
host103192.168.125.1032C4Gnode

       1.2 修改每台机器的hostname

#修改为每台机器的hostname
hostnamectl set-hostname host101
echo "127.0.0.1   $(hostname)" >> /etc/hosts
#查看hostname
hostname

        1.3 每台服务器分别执行以下操作:

#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

#关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

#关闭swag
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab 

#修改/etc/sysctl.conf配置 存在则修改,不存在则追加配置
#修改
sed -i "s#^net.ipv4.ip_forward.*#net.ipv4.ip_forward=1#g"  /etc/sysctl.conf
sed -i "s#^net.bridge.bridge-nf-call-ip6tables.*#net.bridge.bridge-nf-call-ip6tables=1#g"  /etc/sysctl.conf
sed -i "s#^net.bridge.bridge-nf-call-iptables.*#net.bridge.bridge-nf-call-iptables=1#g"  /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.all.disable_ipv6.*#net.ipv6.conf.all.disable_ipv6=1#g"  /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.default.disable_ipv6.*#net.ipv6.conf.default.disable_ipv6=1#g"  /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.lo.disable_ipv6.*#net.ipv6.conf.lo.disable_ipv6=1#g"  /etc/sysctl.conf
sed -i "s#^net.ipv6.conf.all.forwarding.*#net.ipv6.conf.all.forwarding=1#g"  /etc/sysctl.conf
#追加
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding = 1"  >> /etc/sysctl.conf
# 执行命令以应用
sysctl -p

        1.4 为每台机器安装docker环境并配置镜像加速

参考:【Docker-02】Docker安装并配置镜像加速(CentOS7)_求索-的博客-CSDN博客

2. 安装

        2.1 安装K8s核心

# 配置K8S的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
       http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 卸载旧版本
yum remove -y kubelet kubeadm kubectl

# 查看可以安装的版本
yum list kubelet --showduplicates | sort -r

# 安装kubelet、kubeadm、kubectl 指定版本
yum install -y kubelet-1.21.0 kubeadm-1.21.0 kubectl-1.21.0

# 开机启动kubelet
systemctl enable kubelet && systemctl start kubelet

        2.2 准备所需镜像

(1)查看所需镜像

kubeadm config images list

(2)每台机器均准备需要的镜像(这里从阿里云私有仓库拉取,没有镜像的需要自行搜索。。)

docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/kube-apiserver:v1.21.14
docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/kube-controller-manager:v1.21.14
docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/kube-scheduler:v1.21.14
docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/kube-proxy:v1.21.14
docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/pause:3.4.1
docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/etcd:3.4.13-0
docker pull registry.cn-hangzhou.aliyuncs.com/easytuop/coredns:v1.8.0

        因为阿里云个人仓库不能创建个人版不能创建多级目录,这里需要修改一下tag:

docker tag registry.cn-hangzhou.aliyuncs.com/easytuop/coredns:v1.8.0 registry.cn-hangzhou.aliyuncs.com/easytuop/coredns/coredns:v1.8.0

        准备好镜像之后是这样的:

        2.3 初始化master节点

#apiserver-advertise-address为master地址,与service-cidr、pod-network-cidr网段能有交叉
kubeadm init \
--apiserver-advertise-address=192.168.125.101 \
--image-repository registry.cn-hangzhou.aliyuncs.com/easytuop \
--kubernetes-version v1.21.14 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.20.0.0/16

         执行成功可以看到下面输出:

         部署一个pod网络(这里推荐使用calico网络:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

         2.3 初始化Node节点

        按上面提示在master节点执行:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config


export KUBECONFIG=/etc/kubernetes/admin.conf

        在node节点上执行上图kubeadm join命令加入集群:

kubeadm join 192.168.125.101:6443 --token b6ruxb.3q97cspyyuos4dx0 \
        --discovery-token-ca-cert-hash sha256:9f770fe3f3791ab210ebf0992841c69f5e0f1de2642a804ab43947191e261b3c 

        说明:这里的kubeadm join命令是会过期的,如果超时未执行,可以执行下面命令重新生成

kubeadm token create --print-join-command

        2.4 执行命令查看所有节点,STATUS全为Ready代表集群准备完成

kubectl get nodes

 3. 其它配置

        3.1 设置ipvs模式

#1、查看默认kube-proxy 使用的模式
kubectl logs -n kube-system kube-proxy-28xv4
#2、需要修改 kube-proxy 的配置文件,修改mode 为ipvs
kubectl edit cm kube-proxy -n kube-system
#3、删除所有kube-proxy(删除后自动创建从而生效)如下图
#查看所有pod 
kubectl get pod -A
#删除kube-proxy
kubectl delete pod kube-proxy-5tpd6 kube-proxy-ftjbj kube-proxy-v2mvg -n kube-system

          3.1 其它节点使用kubctl访问集群:

#master查看conf
cat /etc/kubernetes/admin.conf
#在其它节点写入查到的文件内容到config
mkdir ~/.kube && cd ~/.kube
vim config

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐