這篇文章給大家介紹CentOS7中怎么搭建kubernetes集群環(huán)境,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
目前成都創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計、臺山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
一、服務(wù)器環(huán)境準(zhǔn)備
192.168.247.128 : k8s-master、etcd、registry
192.168.247.129 : k8s-nodeA
192.168.247.130 : k8s-nodeB
注:安裝lsb_release命令:yum install redhat-lsb -y
三臺機(jī)器配置相同
安裝好相同版本的docker:
[root@localhost ~]# docker -v
Docker version 1.12.6, build 85d7426/1.12.6
三臺機(jī)器上分別修改hostname:
master上運(yùn)行:
[root@localhost ~]# hostnamectl --static set-hostname k8s-master
nodeA上運(yùn)行:
[root@localhost ~]# hostnamectl --static set-hostname k8s-nodeA
nodeB上運(yùn)行:
[root@localhost ~]# hostnamectl --static set-hostname k8s-nodeB
三臺機(jī)器上分別配置hosts, 執(zhí)行如下命令修改hosts文件:
echo '192.168.247.128 k8s-master 192.168.247.128 etcd 192.168.247.128 registry 192.168.247.129 k8s-nodeA 192.168.247.130 k8s-nodeB' >> /etc/hosts
關(guān)閉三臺機(jī)器的防火墻, 三臺機(jī)器上分別執(zhí)行:
[root@localhost ~]# systemctl stop firewalld
注:防火墻相關(guān)命令:
查看防火墻狀態(tài): systemctl status firewalld
關(guān)閉防火墻:systemctl stop firewalld
開啟防火墻:systemctl start firewalld
關(guān)閉前:
關(guān)閉后:
二、安裝etcd
k8s運(yùn)行依賴etcd,需要先安裝etcd, yum方式安裝etcd:
在k8s-master上運(yùn)行:
yum install etcd -y
安裝完成后編輯配置文件 , yum安裝的etcd默認(rèn)配置文件在/etc/etcd/etcd.conf
修改如下三個參數(shù)值:
執(zhí)行如下命令,啟動etcd, 并驗(yàn)證狀態(tài)是否正確 :
[root@localhost ~]# systemctl start etcd
[root@localhost ~]# etcdctl set developer xiejunbo
xiejunbo
[root@localhost ~]# etcdctl get developer
xiejunbo
[root@localhost ~]# etcdctl -C http://etcd:4001 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
cluster is healthy
[root@localhost ~]# etcdctl -C http://etcd:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
cluster is healthy
說明ectd狀態(tài)健康,可以正常使用。
三、部署k8s-master
安裝docker:
yum install docker
修改docker配置文件:vi /etc/sysconfig/docker
設(shè)置docker開機(jī)自啟動,然后開啟docker服務(wù):
[root@localhost ~]# chkconfig docker on
[root@localhost ~]# service docker start
安裝kubernetes :
使用yum方式安裝kubernetes: yum install kubernetes
kubernetes安裝成功后, 配置并啟動kubernetes :
在kubernetes master 上運(yùn)行需要以下組件:
1.kubernetes api server
2.kubernetes controller manager
3.kubernetes scheduler
需要修改相對應(yīng)的配置文件 :
/etc/kubernetes/apiserver: 修改四個參數(shù):
/etc/kubernetes/config: 修改一個參數(shù):
修改完成后,啟動服務(wù),然后設(shè)置開機(jī)自啟動:
[root@localhost ~]# systemctl enable kube-apiserver.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-apiserver.service to /usr/lib/systemd/system/kube-apiserver.service.
[root@localhost ~]# systemctl start kube-apiserver.service
[root@localhost ~]# systemctl enable kube-controller-manager.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-controller-manager.service to /usr/lib/systemd/system/kube-controller-manager.service.
[root@localhost ~]# systemctl start kube-controller-manager.service
[root@localhost ~]# systemctl enable kube-scheduler.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-scheduler.service to /usr/lib/systemd/system/kube-scheduler.service.
[root@localhost ~]# systemctl start kube-scheduler.service
四、部署k8s-node
1.安裝docker略
2.nodeA節(jié)點(diǎn)安裝kubernetes: yum install kubernetes
配置并啟動kubernetes:
在k8s-node上需要運(yùn)行以下組件:
1.kubelet
2.kubernetes proxy
需要對應(yīng)修改兩個配置文件 :
修改/etc/kubernetes/config中的kube_master地址參數(shù):
修改/etc/kubernetes/kubelet中的三個參數(shù):
修改完成后,啟動服務(wù)并設(shè)置開機(jī)自動啟動:
[root@localhost ~]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@localhost ~]# systemctl start kubelet.service
[root@localhost ~]# systemctl enable kube-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[root@localhost ~]# systemctl start kube-proxy.service
節(jié)點(diǎn)啟動后,在master上查看狀態(tài)是否正常:
[root@localhost ~]# kubectl -s http://k8s-master:8080 get node
NAME STATUS AGE
k8s-nodea Ready 2m
[root@localhost ~]# kubectl get nodes
NAME STATUS AGE
k8s-nodea Ready 7m
在節(jié)點(diǎn)k8s-nodeB上按nodeA操作,同樣安裝kubernetes:
安裝kubernetes 成功后, 按k8s-nodeA修改配置:
修改完配置后,啟動服務(wù)并設(shè)置開機(jī)自啟動:
[root@localhost ~]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@localhost ~]# systemctl start kubelet.service
[root@localhost ~]# systemctl enable kube-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[root@localhost ~]# systemctl start kube-proxy.service
在master上查看集群中節(jié)點(diǎn)及節(jié)點(diǎn)狀態(tài):
五、創(chuàng)建Flannel網(wǎng)絡(luò)
在k8s-master、k8s-nodeA、k8s-nodeB上均安裝Flannel, 執(zhí)行命令:
yum install flannel
安裝成功后,k8s-master、k8s-nodeA、k8s-nodeB上均修改配置文件為 :/etc/sysconfig/flanneld
k8s-master中配置etcd中關(guān)于flannel的key:
[root@localhost ~]# etcdctl mk /atomic.io/network/config '{"Network":"192.0.0.0/16"}'
{"Network":"192.0.0.0/16"}
特別注意:Flannel使用Etcd進(jìn)行配置,來保證多個Flannel實(shí)例之間的配置一致性,所以需要在etcd上進(jìn)行如下配置:(‘/atomic.io/network/config’這個key與上文/etc/sysconfig/flannel中的配置項(xiàng)FLANNEL_ETCD_PREFIX是相對應(yīng)的,錯誤的話啟動就會出錯)
啟動Flannel之后,需要依次重啟docker、kubernete:
在master執(zhí)行:
systemctl enable flanneld.service systemctl start flanneld.service service docker restart systemctl restart kube-apiserver.service systemctl restart kube-controller-manager.service systemctl restart kube-scheduler.service
在node上執(zhí)行:
systemctl enable flanneld.service systemctl start flanneld.service service docker restart systemctl restart kubelet.service systemctl restart kube-proxy.service
安裝配置完成
===========================================================
檢查K8S版本:
關(guān)于CentOS7中怎么搭建kubernetes集群環(huán)境就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。