目前生产部署Kubernetes 集群主要有两种方式:
Kubeadm 降低部署门槛,但屏蔽了很多细节,遇到问题很难排查。如果想更容易可控,推荐使用二进制包部署Kubernetes 集群,虽然手动部署麻烦点,期间可以学习很多工作原理,也利于后期维护。
本文使用一主两从,共三台服务器搭建k8s集群
三台服务器都需如下操作
#临时关闭 swapoff -a
#永久关闭,需要重启
sed -i 's/.*swap.*/#&/' /etc/fstab
#查看,swap必须为0
free -g
echo "192.168.48.14 k8s" >>/etc/hosts
echo "192.168.48.13 k8s-node1" >>/etc/hosts
echo "192.168.48.11 k8s-node2" >>/etc/hosts
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
EOF
#刷新
sysctl --system
#关闭防火墙与selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
#安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
#配置阿里源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装docker
yum install -y docker-ce
systemctl start docker
systemctl enable docker
#修改docker资源管理
vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
systemctl daemon-reload
systemctl restart docker
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#安装软件
yum install -y kubelet-1.21.3 kubeadm-1.21.3 kubectl-1.21.3
systemctl enable kubelet
systemctl start kubelet
在master主服务器操作,进行集群初始化,注意按实际更改自己的ip信息
kubeadm init --apiserver-advertise-address=192.168.48.14 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version v1.21.3 --service-cidr=10.125.0.0/16 --pod-network-cidr=10.150.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
ps:加入集群的命令中的token==有效期为24小时==,若过期,需要重新申请token
#生成token
kubeadm token create
#查看token
kubeadm token list | awk -F" " '{print $1}'
#获取CA公钥的hash值
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^ .* //'
kubeadm join 192.168.48.14:6443 --token brc1q1.tnuas794b21c6l7a \
--discovery-token-ca-cert-hash sha256:c3b8b3b770b60af4bc884b4034cc9ab922ae49700f41628b83a25b651e72111d
######################网络添加############################
#添加
kubectl apply -f \
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#删除
kubectl delete -f \
https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
######################weget下载##########################
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#更改配置文件
vim kube-flannel.yml
#将IP更改为master节点制作时的IP地址
84 "Network": "10.150.0.0/16"
#添加插件
kubectl apply -f + kube-flannel.yml的路径
kubectl label node k8s-node1 node-role.kubernetes.io/node=node
kubectl label node k8s-node2 node-role.kubernetes.io/node=node
#获取节点信息
kubectl get nodes
master检查
#查询master是否正常
kubectl get cs
#若为unhealthy
vim /etc/kubernetes/manifests/kube-scheduler.yaml
vim /etc/kubernetes/manifests/kube-controller-manager.yaml
- --port=0 #注释掉,每个文件均有一行
kube-controller-manager.yaml文件修改:注释掉27行
kube-scheduler.yaml配置修改:注释掉19行,- --port=0
检测K8s集群是否正常后,再运行服务部署
#查询所有pod是否正常运行
kubectl get pods -A
#查询master是否正常
kubectl get cs
#查询node节点是否ready
kubectl get nodes
#部署服务
kubectl create deployment nginx --image=nginx:1.14
#暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pods
kubectl get svc
#删除pod与svc
kubectl delete deploy/nginx
kubectl delete svc/nginx
若出现如下错误:
Failed to run kubelet" err="failed to run Kubelet: misconfiguration: kubelet cgroup driver: \"cgroupfs\" is diffelet: misconfiguration: kubelet cgroup driver: \"cgroupfs\" is different from docker cgroup driver: \"systemd\""
报错故障:docker与k8s的资源管理器不同kubelet cgroup driver: “cgroupfs” is different from docker cgroup driver: “systemd”"
解决方法:修改docker与k8s的资源管理器为systemd
#docker修改
vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
#重启服务
systemctl daemon-reload
systemctl restart docker
#k8s修改方法
vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
#或vim /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
--cgroup-driver=systemd
#重启服务
systemctl daemon-reload
systemctl restart kubelet