环境
centOs7(一台master, 几台node都可以,2CPU 4G内存 400G硬盘)
Docker docker-ce-18.06.1.ce-3.el7
kubernetes v1.23.5
crictl-v1.23.0
socat
基础配置
- 关闭防火墙、selinux、SWAP
systemctl stop firewalld && systemctl disable firewalld && setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
- 内核开启网络支持
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p /etc/sysctl.conf
- 设置 /etc/hosts
cat <<EOF > /etc/hosts
192.168.100.2 zbf001
192.168.100.3 zbf002
EOF
一、Docker部署
// 1.安装Docker源
yum install -y wget && wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
// 2.安装Docker
yum -y install docker-ce-18.06.1.ce-3.el7
// 3.开启自启和启动
systemctl enable docker && systemctl start docker
// 4.查看版本
docker --version
二、安装最新版本k8s
curl -sSL https://dl.k8s.io/release/stable.txt
wget -q https://dl.k8s.io/v1.23.5/kubernetes-server-linux-amd64.tar.gz
tar -zxf kubernetes-server-linux-amd64.tar.gz
ls kubernetes/server/bin/ | grep -E 'kubeadm|kubelet|kubectl'
mv kubernetes/server/bin/kube{adm,ctl,let} /usr/bin/
kubeadm version
kubectl version --client
kubelet --version
cat <<'EOF' > /etc/systemd/system/kubelet.service
[Unit]
Description=kubelet: The Kubernetes Agent
Documentation=http://kubernetes.io/docs/
[Service]
ExecStart=/usr/bin/kubelet
Restart=always
StartLimitInterval=0
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
mkdir -p /etc/systemd/system/kubelet.service.d
cat <<'EOF' > /etc/systemd/system/kubelet.service.d/kubeadm.conf
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
EnvironmentFile=-/etc/default/kubelet
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
EOF
systemctl enable kubelet
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.23.0/crictl-v1.23.0-linux-amd64.tar.gz
tar zxvf crictl-v1.23.0-linux-amd64.tar.gz
mv crictl /usr/bin/
sudo yum install -y socat
yum -y install socat conntrack-tools
cat <<'EOF' > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl restart docker
三、初始化master(只有这一个步骤仅master执行)
kubeadm init \
--apiserver-advertise-address=192.168.100.2 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.5 \
--service-cidr=192.168.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--ignore-preflight-errors=all
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
四、node加入到master
kubeadm token create --print-join-command
kubeadm join 10.20.10.235:6443 --token h5oegb.ops4pu6ynhmixhwh \
--discovery-token-ca-cert-hash sha256:c058feb51154138d0783cb8403970e5463aa2301f791217b8c3a1e658cfbebbf
五、安装 通信组件 flannel 或者 calico
1.安装
mkdir ~/kubernetes-flannel && cd ~/kubernetes-flannel
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
kubectl get nodes
2.报错解决
kubectl get nodes
journalctl -f -u kubelet
会报错:
"Container runtime network not ready" networkReady="NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized"
rm -rf /opt/cni/bin
sudo mkdir -p /opt/cni/bin
cd /opt/cni/bin
下载 https://github.com/containernetworking/plugins/releases/tag/v0.8.6
文件名:cni-plugins-linux-amd64-v0.8.6.tgz
tar -zxvf cni-plugins-linux-amd64-v0.8.6.tgz
kubectl get nodes
3.node也要完成这一步(强调)
journalctl -f -u kubelet
kubectl get nodes
六、维护
1.master和node重置
kubeadm reset
2.master删除节点
kubectl cordon zbf002
kubectl drain zbf002 --ignore-daemonsets
kubectl delete node zbf002
3. node节点执行kubectl命令
scp /etc/kubernetes/admin.conf [email protected]:/etc/kubernetes/
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile
七、相关报错和解决记录(可忽略)
[preflight] Running pre-flight checks
error execution phase preflight: [preflight] Some fatal errors occurred:
[ERROR FileExisting-conntrack]: conntrack not found in system path
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
yum -y install socat conntrack-tools
[kubelet-check] Initial timeout of 40s passed.
[kubelet-check] It seems like the kubelet isn't running or healthy.
[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.
#解决:Docker是用yum安装的,docker的cgroup驱动程序默认设置为systemd。默认情况下Kubernetes cgroup为system,我们需要更改Docker cgroup驱动
cat <<'EOF' > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
systemctl restart docker
kubeadm reset
八、参考资料
https://blog.csdn.net/qq_36002737/article/details/123678418