??Hello,各位小伙伴,大家好呀。老哥最近在接觸云原生相關(guān)的東西,然后的話,需要接觸到k8s,之前一直沒(méi)有時(shí)間搭建。今天趁著周末放假,趕緊動(dòng)起來(lái)!
??給大家?guī)?lái)的是最新版k8s集群的搭建,希望給大家?guī)?lái)一點(diǎn)小小的幫助。老哥資金有限,這次采用一主一從進(jìn)行搭建集群
k8s官方文檔:https://kubernetes.io/docs/home/
k8s集群(kubeadm)官方搭建教程: https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
1、首先,準(zhǔn)備兩臺(tái)干凈的Centos8 服務(wù)器,設(shè)置一下主機(jī)名
第一臺(tái)
[root@VM-4-2-centos ~]# hostnamectl set-hostname master
第二臺(tái)
[root@VM-4-12-centos /]# hostnamectl set-hostname node1
2、k8s集群搭建,需要用到內(nèi)網(wǎng)ip,千萬(wàn)別用公網(wǎng)ip!!!,否則工作節(jié)點(diǎn)無(wú)法加入主節(jié)點(diǎn),然后報(bào)錯(cuò),Failed to load kubelet config file" err=“failed to load Kubelet config file /var/lib/kubelet/config.yaml, error failed to read kubelet config file “/var/lib/kubelet/config.yaml”, error: open /var/lib/kubelet/config.yaml: no such file or directory” path=“/var/lib/kubelet/config.yaml”
提前查看好內(nèi)網(wǎng)ip,我們可以通過(guò) ifconfig命令查看
[root@VM-4-2-centos ~]# ifconfig
3、然后兩臺(tái)服務(wù)器,設(shè)置主機(jī)映射
[root@VM-4-2-centos ~]# vi /etc/hosts
你的內(nèi)網(wǎng)ip master
你的內(nèi)網(wǎng)ip node1
3A、內(nèi)網(wǎng)訪問(wèn),我們可以用ping一下,能不能ping通,如果不能,需要做 ip地址重定向,老哥簡(jiǎn)單解釋一下,就是通過(guò)iptable,將內(nèi)網(wǎng)訪問(wèn)的ip重定向到外網(wǎng),這樣互通的時(shí)候,集群配置的時(shí)候,雖然是內(nèi)網(wǎng),但是我們依舊可以通過(guò)外網(wǎng)形式訪問(wèn)
第一臺(tái)服務(wù)器,主節(jié)點(diǎn)
ping node1內(nèi)網(wǎng)ip
iptables -t nat -A OUTPUT -d node1內(nèi)網(wǎng)ip -j DNAT --to-destination 101.34.7.236
第二臺(tái)服務(wù)器,工作節(jié)點(diǎn)
ping master內(nèi)網(wǎng)ip
iptables -t nat -A OUTPUT -d master內(nèi)網(wǎng)ip -j DNAT --to-destination 101.35.245.191
5、然后我們?cè)诘谝慌_(tái)服務(wù)器測(cè)試一下,沒(méi)有任何問(wèn)題
[root@VM-4-2-centos ~]# ping node1
PING node1 (xxx) 56(84) bytes of data.
64 bytes from node1 (xxx): icmp_seq=1 ttl=57 time=2.93 ms
64 bytes from node1 (xxx): icmp_seq=2 ttl=57 time=2.93 ms
64 bytes from node1 (xxx): icmp_seq=3 ttl=57 time=2.92 ms
^C
--- node1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 2.920/2.926/2.933/0.062 ms
6、接著,兩臺(tái)服務(wù)器執(zhí)行以下命令,關(guān)閉一些不必要的檢查,生產(chǎn)環(huán)境建議不要這樣做
所有節(jié)點(diǎn)確保防火墻關(guān)閉
systemctl stop firewalld
systemctl disable firewalld
7、然后添加K8s安裝源,移動(dòng)到 /etc/yum.repos.d/目錄下,(所有節(jié)點(diǎn)執(zhí)行)
cat <<EOF > kubernetes.repo
[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
[root@VM-4-2-centos ~]# mv kubernetes.repo /etc/yum.repos.d/
8、安裝yum 倉(cāng)庫(kù)依賴(lài),兩個(gè)節(jié)點(diǎn)執(zhí)行
[root@VM-4-2-centos ~]# yum -y install yum-utils
9、添加Docker安裝源,兩個(gè)節(jié)點(diǎn)執(zhí)行
[root@VM-4-2-centos ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
10、安裝kubelet、docker,兩個(gè)節(jié)點(diǎn)執(zhí)行
[root@VM-4-2-centos ~]# yum install -y kubelet-1.22.4 kubectl-1.22.4 kubeadm-1.22.4 docker-ce
11、啟動(dòng)docker,設(shè)置docker和kubelet開(kāi)機(jī)自啟,注意,這里千萬(wàn)不要啟動(dòng)kubelet,因?yàn)檫€沒(méi)有做初始化,不然等下報(bào)錯(cuò),哭都沒(méi)地方哭,兩個(gè)節(jié)點(diǎn)執(zhí)行
[root@VM-4-2-centos ~]# systemctl start docker
[root@VM-4-2-centos ~]# systemctl enable kubelet
[root@VM-4-2-centos ~]# systemctl enable docker
12、添加一下docker鏡像,兩個(gè)節(jié)點(diǎn)執(zhí)行
cat <<EOF > daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://ud6340vz.mirror.aliyuncs.com"]
}
EOF
mv daemon.json /etc/docker/
13、刷新docker配置,兩個(gè)節(jié)點(diǎn)執(zhí)行
[root@VM-4-2-centos ~]# systemctl daemon-reload
[root@VM-4-2-centos ~]# systemctl restart docker
14、kubelet初始化,主節(jié)點(diǎn)執(zhí)行,如果你能夠看到以下提示,說(shuō)明初始化成功了
kubeadm init \
--kubernetes-version=v1.22.12 \
--pod-network-cidr=10.244.0.0/16 \
--service-cidr=10.96.0.0/12 \
--apiserver-advertise-address=主節(jié)點(diǎn)內(nèi)網(wǎng)ip \
--image-repository=registry.aliyuncs.com/google_containers
15、然后,主節(jié)點(diǎn)需要暴露配置,給工作節(jié)點(diǎn),可以使用普通用戶(hù)命令和root用戶(hù)命令,這里我們都用上,主節(jié)點(diǎn)執(zhí)行
[root@VM-4-2-centos ~]# mkdir -p $HOME/.kube
[root@VM-4-2-centos ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@VM-4-2-centos ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@VM-4-2-centos ~]# export KUBECONFIG=/etc/kubernetes/admin.conf
16、啟動(dòng)kubelet,主節(jié)點(diǎn)執(zhí)行,執(zhí)行完以后,云服務(wù)器開(kāi)放6443端口
[root@VM-4-2-centos ~]# systemctl start kubelet
17、將主節(jié)點(diǎn)初始化后的join命令,在工作節(jié)點(diǎn)執(zhí)行
[root@VM-4-12-centos opt]# kubeadm join xxx:6443 --token 054e0q.0vkelrud3t8oafdk \
> --discovery-token-ca-cert-hash sha256:e3114194a4b5a18cfc08fadd26de0b18e0d08baa9132ff594366d6453c282a8a
18、然后我們到主節(jié)點(diǎn)查看信息,工作節(jié)點(diǎn)加入進(jìn)來(lái)了
[root@VM-4-2-centos ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady control-plane,master 105m v1.22.4
node1 NotReady <none> 104m v1.22.4
19、加入進(jìn)來(lái)以后,發(fā)現(xiàn)主節(jié)點(diǎn)和工作節(jié)點(diǎn)都沒(méi)有準(zhǔn)備就緒,我們需要借助一個(gè)網(wǎng)絡(luò)插件 flannel,安裝好以后它不會(huì)馬上是準(zhǔn)備狀態(tài),需要我們等待一下,大概1分鐘左右
[root@VM-4-2-centos ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
20、flannel 還需要配置一個(gè)插件,這個(gè)是方便后面創(chuàng)建pod,運(yùn)行pod使用的,主節(jié)點(diǎn)需要執(zhí)行,最新版本好像已經(jīng)配置了,如果沒(méi)有配置的話,會(huì)報(bào)錯(cuò) Failed to create pod sandbox: rpc error: code = Unknown desc = failed to set up sandbox container “dbd0d27f829c8c6ebae6bdcc8593e3af1849ce9d2b4deeb724de1070a46e4ea7” network for pod “nginx-56bc9fcd6b-5djbw”: networkPlugin cni failed to set up pod “nginx-56bc9fcd6b-5djbw_default” network: open /run/flannel/subnet.env: no such file or directory
#剛開(kāi)始進(jìn)不去,等待1分鐘,才可以cd 進(jìn)去
[root@VM-4-2-centos ~]# cd /run/flannel/
[root@VM-4-2-centos flannel]# vim subnet.env
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
21、主節(jié)點(diǎn)查看,ok,大功告成文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-437961.html
[root@VM-4-2-centos ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 113m v1.22.4
node1 Ready <none> 112m v1.22.4
22、最后,各位小伙伴,麻煩給老哥一個(gè)點(diǎn)贊、關(guān)注、收藏三連好嗎,你的支持是老哥更新最大的動(dòng)力,謝謝!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-437961.html
到了這里,關(guān)于Linux 搭建K8s集群,最新教程,細(xì)到極致的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!