一、docker环境搭建
1、移除以前docker相关包
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
2、配置yam源
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
3、安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6
4、启动
systemctl enable docker --now
二、kubeadm创建集群
给每个节点设置主机名
hostnamectl set-hostname k8smaster
将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
关闭swap
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
配置生效
sudo sysctl --system
安装kubectl、kubeadm、kubectl
cat <<EOF | sudo tee /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
exclude=kubelet kubeadm kubectl
EOF
安装
sudo yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes
启动kubelet
sudo systemctl enable --now kubelet
2、使用kubeadm引导集群
1、下载各个机器需要的镜像
sudo tee ./images.sh <<-'EOF'
#!/bin/bash
images=(
kube-apiserver:v1.20.9
kube-proxy:v1.20.9
kube-controller-manager:v1.20.9
kube-scheduler:v1.20.9
coredns:1.7.0
etcd:3.4.13-0
pause:3.2
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
done
EOF
chmod +x ./images.sh && ./images.sh
3、初始化主节点
给所有机器添加master域名映射,以下需要修改为自己的
echo "192.168.245.66 cluster-endpoint">> /etc/hosts
#主节点初始化
kubeadm init \
--apiserver-advertise-address=192.168.245.66 \
--control-plane-endpoint=cluster-endpoint \
--image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \
--kubernetes-version v1.20.9 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=192.168.0.0/16
#所有网络范围不重叠
可能会遇到的问题:
1.Port 10259 is in use
/proc/sys/net/ipv4/ip_forward contents are not set to 1
参考链接
搭建kubenetes集群环境过程中遇到的问题
4、k8s安装成功后master节点会出现
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
Alternatively, if you are the root user, you can run:
export KUBECONFIG=/etc/kubernetes/admin.conf
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/
You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:
kubeadm join cluster-endpoint:6443 --token ibpnqm.enw3hmsxuw8uz7gx \
--discovery-token-ca-cert-hash sha256:c98fd1b4c69a56dffc737c39f29c7f00b82ce16914c067440702f0994742467e \
--control-plane
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token ibpnqm.enw3hmsxuw8uz7gx \
--discovery-token-ca-cert-hash sha256:c98fd1b4c69a56dffc737c39f29c7f00b82ce16914c067440702f0994742467e
检查master节点是否部署成功
kubectl get nodes -A
不成功会出现
遇到的问题
1、在pod加载过程中
可能会出现master节点处于notReady的状态
首先排查/etc/hosts
中没有有给节点加入域名
如
2、排查所有的k8s服务是否正常运行
kubectl get pods -A
没有启动成功会出现
这种情况大概率是因为网络插件配置与当前k8s版本不匹配,或者calico安装错误,
下面的calico.yaml
是我使用的可以参考
caclio.yaml 文件链接
若以前在节点上搭建过k8s集群,在节点上再次添加会失败,如下图
此时需要重置
使用如下命令:
kubeadm reset
接入master节点会出现,如下图
至此,一台主机两台工作节点的k8s集群搭建完成!!!!!!!!!!!!!!