一、背景:
1、实验背景🧪
2、准备工作
这里准备了三台服务器进行测试
110.42.149.60 master 腾讯云 4h8g centos7.6
42.193.254.253 node1 腾讯云 2h4g centos7.6
69.165.74.251 node2 俄罗斯 1h2g centos7.6
----------------------------------------------------------------------
1、搭建时建议是最干净的系统(可以是刚开通的机器或者重装系统后的机器)
2、不同版本的k8s对服务器的系统环境也是不一样的,如果搭建有问题建议更换系统版本~~
二、部署流程🚗
1、更新yum源
yum -y update
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp
2、安装docker
2.1 安装必要的依赖
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
2.2 设置docker仓库
sudo yum-config-manager --add-repo https://mirror.azure.cn/docker-ce/linux/centos/docker-ce.repo
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'eof'
{
"registry-mirrors": ["https://nmxk8hna.mirror.aliyuncs.com"]
}
eof
sudo systemctl daemon-reload
2.3 安装docker
yum install -y docker-ce-19.03.14 docker-ce-cli-19.03.14
2.4 启动docker
sudo systemctl start docker && sudo systemctl enable docker
2.5 检查docker是否安装成功
ps -ef|grep docker
3、修改hosts文件
3.1 设置主机名称
sudo hostnamectl set-hostname master
bash
3.2 增加hosts配置
cat >> /etc/hosts << eof
110.42.149.60 master
42.193.254.253 node1
69.165.74.251 node2
eof
4、系统基础配置(每个机器都要配置)
4.1 系统配置
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^selinux=enforcing$/selinux=permissive/' /etc/selinux/config
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
iptables -f && iptables -x && iptables -f -t nat && iptables -x -t nat && iptables -p forward accept
cat <<eof > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
eof
sysctl --system
4.2 安装 kubectl,kubelet ,kubeadm
4.2.1 配置源
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
4.2.2 kubectl,kubelet ,kubeadm安装
yum install -y kubectl-1.23.1-0
yum install -y kubelet-1.23.1-0
yum install -y kubeadm-1.23.1-0
kubectl version
kubelet --version
kubeadm version
4.2.3 设置服务开机启动
systemctl enable kubelet
4.3 docker和k8s设置同一个cgroup
vi /etc/docker/daemon.json
"exec-opts": ["native.cgroupdriver=systemd"]
systemctl restart docker
4.4 在master节点初始化kubeadm
4.4.1 初始化
ip a | grep 110.42.149.60
cat > /etc/sysconfig/network-scripts/ifcfg-eth0:1 <<eof
bootproto=static
device=eth0:1
ipaddr=110.42.149.60
prefix=32
type=ethernet
userctl=no
onboot=yes
eof
cat > /etc/sysconfig/network-scripts/ifcfg-eth0:1 <<eof
bootproto=static
device=eth0:1
ipaddr=42.193.254.253
prefix=32
type=ethernet
userctl=no
onboot=yes
eof
systemctl restart network
kubeadm init --image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version=1.23.1 \
--pod-network-cidr=192.168.0.0/16 \
--control-plane-endpoint=110.42.149.60 \
--apiserver-advertise-address=110.42.149.60
echo 1 > /proc/sys/net/ipv4/ip_forward

kubeadm join 110.42.149.60:6443 --token dh45ui.f510jekx2lr3968j --discovery-token-ca-cert-hash sha256:c538ce89ee60b9b091a049d84758799b04da0b2ae0073ded7dd9b302d7bc17e9
mkdir -p $home/.kube
cd .kube/
sudo cp -i /etc/kubernetes/admin.conf $home/.kube/config
sudo chown $(id -u):$(id -g) $home/.kube/config
vim /etc/profile
export kubeconfig="/etc/kubernetes/admin.conf"
source /etc/profile
4.4.2 部署calico网络插件
kubectl create -f https://docs.projectcalico.org/manifests/calico.yaml
kubectl get pods --all-namespaces -w

kubectl get pods -n kube-system
curl -k https://localhost:6443/healthz

4.4.3 需要使用 ipvs 替换iptables,操作是在所有节点上
cat >> /etc/sysctl.conf << eof
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
eof
sysctl -p
cat > /etc/sysconfig/modules/ipvs.modules <<eof
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
eof
kubectl edit cm kube-proxy -n kube-system
ipvs:
excludecidrs: null
minsyncperiod: 0s
scheduler: ""
syncperiod: 30s
kind: kubeproxyconfiguration
metricsbindaddress: ""
mode: "ipvs"
在master重启kube-proxy
kubectl get pod -n kube-system | grep kube-proxy | awk '{print $1}' | xargs kubectl delete pod -n kube-system
4.5 node节点加入集群
4.5.1 到node节点的服务器上执行以下命令
kubeadm join 110.42.149.60:6443 --token emtzcn.qgjtmzwrt5dhg7l1 \
--discovery-token-ca-cert-hash sha256:fe1e876dff5884e52c491874ce6b4073a40f6edeb1860120b379401010105e8e
4.5.2 切换到master服务器执行一下命令查看节点加入情况
kubectl get nodes

5、定义一个nginx pod进行集群测试
cat > pod_nginx_rs.yaml <<eof
apiversion: apps/v1
kind: replicaset
metadata:
name: nginx
labels:
tier: frontend
spec:
replicas: 3
selector:
matchlabels:
tier: frontend
template:
metadata:
name: nginx
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerport: 80
eof
kubectl apply -f pod_nginx_rs.yaml
kubectl get pods
kubectl get pods -o wide
kubectl describe pod nginx
kubectl scale rs nginx --replicas=5
kubectl get pods -o wide
kubectl delete -f pod_nginx_rs.yaml
6、安装 dashboard 插件
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
vi recommended.yaml
kubectl create -f recommended.yaml

vi dashboard-admin.yml
apiversion: v1
kind: serviceaccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiversion: rbac.authorization.k8s.io/v1
kind: clusterrolebinding
metadata:
name: admin-user
roleref:
apigroup: rbac.authorization.k8s.io
kind: clusterrole
name: cluster-admin
subjects:
- kind: serviceaccount
name: admin-user
namespace: kubernetes-dashboard
kubectl apply -f dashboard-admin.yml
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

kubectl -n kubernetes-dashboard get service kubernetes-dashboard

7、疑惑解答
7.1 安装k8s时为什么需要限定docker版本、k8s版本版本❓


7.2 kubelet节点加入的token忘记了或者失效了怎么办 ❓
kubeadm token create --print-join-command
7.3 为什么要给master节点创建一个和master节点公网ip一样的虚拟网卡❓
7.4 calico是个啥❓
7.5 calico插件版本怎么知道和k8s的版本对应❓
kubelet --version


8、最终的master节点效果图
watch kubectl get pods -a -o wide

本文参考:
kubernetes第二篇:从零开始搭建k8s集群(亲测可用)
基于阿里云公网ip安装部署k8s集群
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论