我使用的是腾讯云,节点与节点使用公网IP通信
确保2台服务器都安装了docker
建议学习阶段关闭防火墙及安全策略
如果需要添加安全组端口放行,我这里附上我的端口放行列表
放行端口如下:
K8S要求虚拟机必须配置的内容,如下:
#各个机器设置自己的域名
#master节点为:k8s-master,node节点为:k8s-node01
hostnamectl set-hostname xxxx
# 将 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 <br_netfilter
EOFcat <
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
kubelet、kubeadm、kubect为K8S所有节点的三大件(每个节点都必须存在的)
注意:处理kubelet是以后台运行方式运行,其他的组件都是通过docker运行
cat <
[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
sudo systemctl enable --now kubelet
kubelet 现在每隔几秒就会重启,因为它陷入了一个等待 kubeadm 指令的死循环
- 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
注意:上述镜像是master节点所需的镜像,而node节点只需要 kube-proxy即可,为了防止搭建过程失败,可以再node节点中安装上述所有镜像
如果只安装kube-proxy,脚本应为:
- sudo tee ./images.sh <<-'EOF'
- #!/bin/bash
- images=(
- kube-proxy:v1.20.9
- )
- for imageName in ${images[@]} ; do
- docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName
- done
- EOF
chmod +x images.sh
./images.sh
给所有服务器添加master节点信息
#cluster-endpoint 集群入口,这里的集群入口就是master节点的IP地址,111.230.19.178为master节点的公网IP
echo "111.230.19.178 cluster-endpoint" >> /etc/hosts
- kubeadm init \
- --apiserver-advertise-address=111.230.19.178 \
- --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
注意:必须保证service-cid与pod-network-cidr不能重叠,且不能与master和所有的node重叠
这个失败问题,首先需要明确解决该问题的思路
思路如下:
1> 明确kubectl服务是否正常
systemctl status kubelet
2>docker运行的k8s组件服务是否正常
kubeadm init 其实就是在启动docker中的容器,初始化失败的话,也就证明容器启动是失败的,我们去定位到底是哪个容器失败了,如下
docker ps -a
定位到2个容器启动失败,如下:
查看容器启动时日志:
docker logs 容器ID/容器名称
etcd容器出现的错误:
etcdmain: listen tcp 111.230.19.178:2380: bind: cannot assign requested address,就是绑定这个IP+端口失败了
kube-apiserver出现的错误:
Err :connection error: desc = "transport: Error while dialing dial tcp 127.0.0.1:2379: connect: connection refused"
注意:kube-apiserver连接2379端口(2379:etcd的client端口),其实就是去连接etcd!!!我们只需要解决etcd就可以了
解决方案:参考:天翼云服务器部署 k8s etcdmain: listen tcp xx.xx.xx.xx:2380: bind: cannot assign requested address-CSDN博客
实测没问题
内容需要记录,这里面有node节点加入master集群的token信息(该token有效期为24h)
kubeadm token create --print-join-command
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/configAlternatively, 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 8vtydf.izdgell5pz20sm8n \
--discovery-token-ca-cert-hash sha256:c91feabc45fb32737dc63a491ef5f41d41c1c80660ebb48e1aef487a27130f98 \
--control-planeThen you can join any number of worker nodes by running the following on each as root:
kubeadm join cluster-endpoint:6443 --token 8vtydf.izdgell5pz20sm8n \
--discovery-token-ca-cert-hash sha256:c91feabc45fb32737dc63a491ef5f41d41c1c80660ebb48e1aef487a27130f98
- mkdir -p $HOME/.kube
- sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- sudo chown $(id -u):$(id -g) $HOME/.kube/config
在master节点执行
curl https://docs.projectcalico.org/v3.20/manifests/calico.yaml -O
使用calico
kubectl apply -f calico.yaml
注意,calico.yaml文件中,有一处与master初始化时是一致的,如果kubectl init时,pod-network-cidr=192.168.0.0/16 calico默认也是192.168
查看k8s当前节点信息
kubectl get nodes
#查看集群所有节点
kubectl get nodes#根据配置文件,给集群创建资源
kubectl apply -f xxxx.yaml#查看集群部署了哪些应用?等同于docker ps
kubectl get pods -A
# 运行中的应用在docker里面叫容器,在k8s里面叫Pod
在kubectl init中,把node节点加入命令复制,在node节点的机器上执行即可
如果执行join时失败,如下
accepts at most 1 arg(s), received 3
To see the stack trace of this error execute with --v=5 or higher
请把join命令在记事本中打开,编辑下格式,可能是shell连接工具导致的错误
kubectl get nodes
<1> 先查看一下这个node节点上的pod信息
kubectl get nodes -o wide
<2> 驱逐该node节点上的pod
kubectl drain k8s-node01 --delete-local-data --force --ignore-daemonsets
<3> 删除这个node节点
kubectl delete nodes k8s-node01
至此,集群搭建完毕
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml
如果无反应,可以在浏览器访问网址,然后在linux中新建yaml文件,把网页中的内容复制进yaml中,再kubectl apply -f yaml文件
kubectl get pod -A
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
kubectl get svc -A |grep kubernetes-dashboard
注意,在访问前,一定要坚持所有的pods是否允许成功,否则访问会失败
kubectl get pods -A
- #创建访问账号,准备一个yaml文件; vi dash.yaml
- 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 dash.yaml
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
eyJhbGciOiJSUzI1NiIsImtpZCI6Ik02dmU1ZnBScUIxaE45YUdYWUJqZmVvY3FyYmVkSkNXZHhTaVp4bmZxdk0ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXZwZjJjIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI5ZDBlZDcwOC1kOTJlLTQzOTQtOTA0OS04N2UxNjJmZmZmODIiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.luGUTFWDp79_UUa02-cBo44Vqs4JahXcNpqIe4IYAA93-WNr-5s3lCHuHXnWtY-eGMDqCqCuBMWnChIdKi97ZvxF6JcwOFkd0EEi04pM-EOGT37nJHGjx4KhjWPU4VQZgP9c172DD8HAMe6_VF4PEarB4lrTUXJqoAMufYO13rVRw8WxS-RPDfXSc7d2nEcy0x_fYd1LEKXwfYq_PvJFnoE2STNAbXcazQbfe0cKbyInkOpbhY_gV4WU1FH8pHNH3BQ4E-Hs6VO9UWujZ3f7jphlMPlIRnprg55CeHSvYHsrX6s6SDMGaWfLfa9REgSkzE-mPelV16EocffLhmMZFg
复制上述令牌,登录即可
整合成功