安装K8S为什么要修改hosts文件,安装k8s1.35初始化卡住
墨初 知识笔记 119阅读
完成安装k8s集群以后推荐使用 准备环境
准备两台服务器节点如果需要安装虚拟机可以参考《wmware和centos安装过程》

vim /etc/hosts#增加192.168.109.130 centos01192.168.109.131 centos02
关闭防火墙所有节点都执行 systemctl stop firewalldsystemctl disable firewalldsetenforce 0vim /etc/selinux/config#修改SELINUX的值SELINUXdisabled
关闭swap内存所有节点都执行 swapoff -avim /etc/fstab# 将该行注释掉#/dev/mapper/cs-swap swap
配置网桥所有节点都执行 1.修改参数
vim /etc/sysctl.confnet.bridge.bridge-nf-call-ip6tables 1net.bridge.bridge-nf-call-iptables 1net.ipv4.ip_forward 1vm.swappiness 0
2.然后加载如下两个模块所有节点都执行

modprobe ip_vs_rrmodprobe br_netfilter
3.生效配置
[rootcentos01 opt]# sysctl -pnet.bridge.bridge-nf-call-ip6tables 1net.bridge.bridge-nf-call-iptables 1net.ipv4.ip_forward 1vm.swappiness 0
安装containerd 以下步骤所有节点都执行。
安装wget Cxzvf /usr/local containerd-1.7.2-linux-amd64.tar.gz
修改配置 mkdir /etc/containerdcontainerd config default > /etc/containerd/config.tomlvim /etc/containerd/config.toml#SystemdCgroup的值改为trueSystemdCgroup true#由于国内下载不到registry.k8s.io的镜像修改sandbox_image的值为sandbox_image registry.aliyuncs.com/google_containers/pause:3.9
启动服务 mkdir -p /usr/local/lib/systemd/systemwget containerd.service /usr/local/lib/systemd/systemsystemctl daemon-reloadsystemctl enable --now containerd
验证安装 [rootcentos01 opt]# ctr versionClient: Version: v1.7.2 Revision: 0cae528dd6cb557f7201036e9f43420650207b58 Go version: go1.20.4Server: Version: v1.7.2 Revision: 0cae528dd6cb557f7201036e9f43420650207b58 UUID: 747cbf1b-17d4-4124-987a-203d8c72de7c
安装runc 以下步骤所有节点都执行。
准备文件wget x runc.amd64
查找containerd安装时已安装的runc所在的位置如果不存在runc文件则直接进行下一步 [rootcentos01 opt]# which runc/usr/bin/runc
替换上一步的结果文件 mv -f runc.amd64 /usr/bin/runc
验证安装 [rootcentos01 opt]# runc -vrunc version 1.1.7commit: v1.1.7-0-g860f061bspec: 1.0.2-devgo: go1.20.3libseccomp: 2.5.4
安装kubernetes 添加阿里云的kubernetes源所有节点都执行 cat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]nameKubernetesbaseurl 安装最新版所有节点都执行 yum install -y kubeadm kubelet kubectl
开机自启动所有节点都执行 systemctl enable kubelet
验证安装所有节点都执行 [rootcentos01 opt]# kubeadm versionkubeadm version: &version.Info{Major:1, Minor:27, GitVersion:v1.27.1, GitCommit:4c9411232e10168d7b050c49a1b59f6df9d7ea4b, GitTreeState:clean, BuildDate:2023-04-14T13:20:04Z, GoVersion:go1.20.3, Compiler:gc, Platform:linux/amd64}
初始化集群仅在master(centos01)上执行 [rootcentos01 opt]# kubeadm init --apiserver-advertise-address 192.168.109.130 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.27.1 --pod-network-cidr10.244.0.0/16.........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.confYou should now deploy a pod network to the cluster.Run kubectl apply -f [podnetwork].yaml with one of the options listed at: you can join any number of worker nodes by running the following on each as root:kubeadm join 192.168.109.130:6443 --token osh87v.zvo010kamsr8esmp \--discovery-token-ca-cert-hash sha256:ff4607c7c194e9f756b1eb509e64d2d926b5f8f9556a85c3c14a2d25add28230
其中
–apiserver-advertise-address通告侦听地址
–image-repository指定镜像地址使用阿里云的默认会使用谷歌镜像
–kubernetes-version指定当前的kubernetes的版本
–pod-network-cidr10.244.0.0/16flannel网络的固定地址范围
仔细阅读kubeadm init执行的结果根据提示还需要进行3步操作
1.笔者用的是root用户仅在master节点执行
[rootcentos01 opt]# vim /etc/profile#在最后一行增加export KUBECONFIG/etc/kubernetes/admin.conf
生效环境变量
[rootcentos01 opt]# source /etc/profile
2.安装网络插件可以选择calico或flannel这里选择安装flannel仅在master节点执行
下载安装文件
[rootcentos01 opt]# wget 修改配置
[rootcentos01 opt]# vim kube-flannel.yml#修改Network项的值改为和--pod-network-cidr一样的值Network: 10.244.0.0/16#由于有时国内网络的问题需要修改image的地址把所有的docker.io改为dockerproxy.com#共需要修改3处两个值image: dockerproxy.com/flannel/flannel:v0.22.0image: dockerproxy.com/flannel/flannel-cni-plugin:v1.1.2
安装网络插件
[rootcentos01 opt]# kubectl apply -f kube-flannel.yml
验证安装
[rootcentos01 opt]# kubectl get pods -n kube-flannelNAME READY STATUS RESTARTS AGEkube-flannel-ds-dfngh 1/1 Running 17 (13m ago) 6d1hkube-flannel-ds-qll8g 1/1 Running 12 (13m ago) 6d1h
3.其他节点加入集群非master节点都执行
[rootcentos02 opt]# kubeadm join 192.168.109.130:6443 --token osh87v.zvo010kamsr8esmp --discovery-token-ca-cert-hash sha256:ff4607c7c194e9f756b1eb509e64d2d926b5f8f9556a85c3c14a2d25add28230
验证安装结果仅在master节点执行 [rootcentos01 opt]# kubectl get nodesNAME STATUS ROLES AGE VERSIONcentos01 Ready control-plane 134m v1.27.1centos02 Ready <none> 133m v1.27.1
[rootcentos01 opt]# kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-7bdc4cb885-l4vs2 1/1 Running 0 9m3scoredns-7bdc4cb885-wzc8x 1/1 Running 0 9m3setcd-centos01 1/1 Running 0 9m18skube-apiserver-centos01 1/1 Running 0 9m18skube-controller-manager-centos01 1/1 Running 0 9m19skube-proxy-m92hr 1/1 Running 0 28skube-proxy-pv4hw 1/1 Running 0 9m3skube-scheduler-centos01 1/1 Running 0 9m18s
建议重启一遍所有服务器节点有可能会暴露出来隐藏的问题再次验证以上结果仍然正常。
最后推荐一个部署应用的平台:
演示地址
标签: