您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)docker中如何初始化k8s集群的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
k8s的部署有多種方式,但我們采用kubeadm工具部署。
kubeadm官方地址:https://github.com/kubernetes/kubeadm
master,etcd: 172.16.1.100
node1: 172.16.1.101
node2: 172.16.1.102
k8s版本:1.11
1、基于主機(jī)名通信:/etc/hosts;
172.16.1.100 master 172.16.1.101 node01 172.16.1.102 node02
2、時(shí)間同步;
3、關(guān)閉firewalld和iptables.service,這兩個(gè)一定要禁用,因?yàn)閗8s會(huì)自己設(shè)置iptables網(wǎng)絡(luò)策略等;
systemctl stop iptables.service systemctl disable iptables.service systemctl stop firewalld.service systemctl disable firewalld.service
4、網(wǎng)絡(luò)橋接全部設(shè)置為1
[root@k8s-master yum.repos.d]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables 1 [root@k8s-master yum.repos.d]# cat /proc/sys/net/bridge/bridge-nf-call-iptables 1
下載kubernetes包: https://github.com/kubernetes/kubernetes/releases
我們?yōu)榱朔奖?,不使用上面安裝包的來(lái)安裝k8s,這里只是讓大家了解一下。我們此次安裝使用kubeadm方式安裝。
a) docker源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
b)k8s源
[root@k8s-master yum.repos.d]# cat k8s.repo [k8s] name=k8s repo baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg enabled=1
[root@k8s-master yum.repos.d]# yum repolist
[root@k8s-master yum.repos.d]# wget https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg [root@k8s-master yum.repos.d]# rpm --import yum-key.gpg
[root@k8s-master yum.repos.d]# wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg [root@k8s-master yum.repos.d]# rpm --import rpm-package-key.gpg
yum -y install docker-ce kubelet kubeadm kubectl (master上執(zhí)行)
[root@k8s-master yum.repos.d]# rpm -ql kubelet /etc/kubernetes/manifests #清單目錄 /etc/sysconfig/kubelet #配置文件 /etc/systemd/system/kubelet.service /usr/bin/kubelet #主程序
由于中國(guó)某種不可描述的原因,需要更改docker 默認(rèn)拉取鏡像的源
root@k8s-master yum.repos.d]# vim /usr/lib/systemd/system/docker.service [Service] #表示訪問https服務(wù)時(shí),通過(guò)下面的代理來(lái)訪問,本次這么做的目的是為了能訪問外國(guó)的docer鏡像,要不會(huì)被墻,用完了再注釋掉,從而繼續(xù)使用國(guó)內(nèi) 的鏡像 Environment="HTTPS_PROXY=http://www.ik8s.io:10080" Environment="NO_PROXY=127.0.0.0/8,172.16.0.0/16"
[root@k8s-master yum.repos.d]# systemctl daemon-reload [root@k8s-master yum.repos.d]# systemctl start docker
[root@k8s-master yum.repos.d]# docker info #能看到如下兩個(gè) HTTPS Proxy: http://www.ik8s.io:10080 No Proxy: 127.0.0.0/8,172.16.0.0/16
[chenzx@sa ~]$ telnet www.ik8s.io 10080 #要確保這個(gè)端口是通的
該過(guò)程會(huì)做先決條件預(yù)檢、生成證書、私鑰、生成配置文件、生成靜態(tài)pod的清單文件并完成部署(addons)
[root@k8s-master yum.repos.d]# systemctl enable kubelet #首先只能設(shè)置為開機(jī)自啟動(dòng),但先不要不要手工啟動(dòng)該服務(wù)(即使現(xiàn)在啟動(dòng)也啟動(dòng)不起來(lái)),等初始化完成了再啟動(dòng)。
[root@k8s-master chenzx]# systemctl enable docker
[root@k8s-master chenzx]# kubeadm init --help
--apiserver-advertise-address:表示apiserver對(duì)外的地址是什么,默認(rèn)是0.0.0.0
--apiserver-bind-port:表示apiserver的端口是什么,默認(rèn)是6443
--cert-dir:加載證書的目錄,默認(rèn)在/etc/kubernetes/pki
--config:配置文件
--ignore-preflight-errors:在預(yù)檢中如果有錯(cuò)誤可以忽略掉,比如忽略 IsPrivilegedUser,Swap.等
--kubernetes-version:指定要初始化k8s的版本信息是什么
--pod-network-cidr :指定pod使用哪個(gè)網(wǎng)段,默認(rèn)使用10.244.0.0/16
--service-cidr:指定service組件使用哪個(gè)網(wǎng)段,默認(rèn)10.96.0.0/12
[root@k8s-master chenzx]# cat /etc/sysconfig/kubelet #指定額外的初始化信息,下面表示禁用操作系統(tǒng)的swap功能 KUBELET_EXTRA_ARGS="--fail-swap-on=false"
[root@k8s-master chenzx]# kubeadm init --kubernetes-version=v1.11.1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap [preflight/images] Pulling images required for setting up a Kubernetes cluster ##表示開始拉取鏡像 [preflight/images] This might take a minute or two, depending on the speed of your internet connection [preflight/images] You can also perform this action in beforehand using 'kubeadm config images pull' ##如果你感覺網(wǎng)速慢,可以運(yùn)行kubeadm config images pull命令把鏡像拖到本地 [certificates] Generated apiserver-kubelet-client certificate and key. ##可以看到生成一堆證書 [certificates] Generated sa key and public key. [certificates] Generated front-proxy-ca certificate and key. [certificates] Generated front-proxy-client certificate and key. [certificates] Generated etcd/ca certificate and key. [controlplane] wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml" ##yml控制給pod分多少cpu和內(nèi)存 [controlplane] wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager. ###markmaster幫我們把此節(jié)點(diǎn)標(biāo)記為主節(jié)點(diǎn) [markmaster] Marking the node k8s-master as master by adding the label "node-role.kubernetes.io/master=''" [markmaster] Marking the node k8s-master as master by adding the taints [node-role.kubernetes.io/master:NoSchedule] ##bootstraptoken是引導(dǎo)令牌,讓其他nodes加入集群時(shí)用的 [bootstraptoken] using token: as5gwu.ktojf6cueg0doexi [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials ##從k8s 1.11版開始,DNS正式被CoreDNS取代,它支持很多新的功能,比如資源的動(dòng)態(tài)配置等 [addons] Applied essential addon: CoreDNS ##kube-proxy托管在K8S之上,負(fù)責(zé)生產(chǎn)service的iptables和ipvs規(guī)則,從k8s1.11開始默認(rèn)支持ipvs [addons] Applied essential addon: kube-proxy ##看到初始化成功了 Your Kubernetes master has initialized successfully! To start using your cluster, you need to run the following as a regular user: ##還需要手工運(yùn)行一下命令 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config ##其他機(jī)器裝好包后,可以執(zhí)行下面的命令來(lái)把nodes節(jié)點(diǎn)加入集群,把下面的命令記得自己保存起來(lái),要不將來(lái)找不著就加不進(jìn)去了 ##其實(shí)這么設(shè)計(jì)的目的就是不是誰(shuí)都能加入集群的,需要拿著下面的令牌來(lái)加入 You can now join any number of machines by running the following on each node as root: kubeadm join 172.16.1.100:6443 --token as5gwu.ktojf6cueg0doexi --discovery-token-ca-cert-hash sha256:399a7de763b95e52084d7bd4cad71dc8fa1bf6dd453b02743d445eee59252cc5
[root@k8s-master chenzx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE k8s.gcr.io/kube-proxy-amd64 v1.11.1 d5c25579d0ff 7 weeks ago 97.8MB k8s.gcr.io/kube-apiserver-amd64 v1.11.1 816332bd9d11 7 weeks ago 187MB k8s.gcr.io/kube-controller-manager-amd64 v1.11.1 52096ee87d0e 7 weeks ago 155MB k8s.gcr.io/kube-scheduler-amd64 v1.11.1 272b3a60cd68 7 weeks ago 56.8MB k8s.gcr.io/coredns 1.1.3 b3b94275d97c 3 months ago 45.6MB k8s.gcr.io/etcd-amd64 3.2.18 b8df3b177be2 4 months ago 219MB k8s.gcr.io/pause 3.1 da86e6ba6ca1 8 months ago 742kB
說(shuō)明:pause可以做一個(gè)容器,這個(gè)容器不用啟動(dòng),pause可以使其他容器復(fù)制基礎(chǔ)的網(wǎng)絡(luò)和存儲(chǔ)構(gòu)件。
如果安裝出錯(cuò)了,可以執(zhí)行kubeadm reset命令進(jìn)行重置,再重新執(zhí)行kubeadm init...命令
注意:上面初始化中輸出的kubeadm join 172.16.1.100:6443 --token.....這句話,一定要粘貼到記事本保存好,因?yàn)橐院笠褂眠@個(gè)命令把node加入集群,并且該命令無(wú)法復(fù)現(xiàn),切記切記?。?!
[root@k8s-master chenzx]# mkdir -p $HOME/.kube [root@k8s-master chenzx]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
yum -y install docker-ce kubelet kubeadm (node上執(zhí)行,nodes上可以不安裝kubectl)
查看組件信息:
[root@k8s-master chenzx]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health": "true"}
查看節(jié)點(diǎn)信息:
[root@k8s-master chenzx]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 51m v1.11.2
說(shuō)明,:狀態(tài)為NotReady,是因?yàn)檫€缺flannel組件,沒有這個(gè)組件是沒法設(shè)置網(wǎng)絡(luò)的。
下載地址:https://github.com/coreos/flannel
安裝flannel: [root@k8s-master chenzx]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
查看當(dāng)前master節(jié)點(diǎn)上kube-system名稱空間里運(yùn)行的所有pod狀態(tài):
[root@k8s-master chenzx]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-78fcdf6894-6j6nt 0/1 Running 0 2h coredns-78fcdf6894-pnmjj 0/1 Running 0 2h etcd-k8s-master 1/1 Running 0 1h kube-apiserver-k8s-master 1/1 Running 0 1h kube-controller-manager-k8s-master 1/1 Running 0 1h kube-flannel-ds-amd64-txxw2 1/1 Running 0 1h kube-proxy-frkp9 1/1 Running 0 2h kube-scheduler-k8s-master 1/1 Running 0 1h
另外,以上所有pod必須保證都是running狀態(tài)的,如果哪個(gè)不是,可以通過(guò)類似如下命令查看為什么:
kubectl dscrible pods coredns-78fcdf6894-6j6nt -n kube-system
查看flannel鏡像: [root@k8s-master chenzx]# docker images quay.io/coreos/flannel REPOSITORY TAG IMAGE ID CREATED SIZE quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 7 months ago 44.6MB
查看nodes節(jié)點(diǎn)信息,看到status這回變成ready了 [root@k8s-master chenzx]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 1h v1.11.2
查看當(dāng)前節(jié)點(diǎn)名稱空間:
[root@k8s-master chenzx]# kubectl get ns NAME STATUS AGE default Active 3h kube-public Active 3h kube-system Active 3h
該過(guò)程也會(huì)先檢查先決條件是否滿足需求,然后基于域共享的令牌認(rèn)證方式完成master節(jié)點(diǎn)的認(rèn)證,并完成本地的pod的資源安裝,包含以addons方法部署的kubbe-proxy、DNS。
1)在node1和node2上修改配置文件并啟動(dòng)服務(wù):
[root@k8s-master chenzx]# vim /usr/lib/systemd/system/docker.service
[Service] Environment="HTTPS_PROXY=http://www.ik8s.io:10080" Environment="NO_PROXY=127.0.0.0/8,172.16.0.0/16"
[root@k8s-master chenzx]# vim /etc/sysconfig/kubelet
#指定額外的初始化信息 KUBELET_EXTRA_ARGS="--fail-swap-on=false"
[root@k8s-node1 chenzx]# systemctl daemon-reload [root@k8s-node1 chenzx]# systemctl start docker [root@k8s-node1 chenzx]# systemctl enable docker [root@k8s-node1 chenzx]# systemctl enable kubelet
[root@k8s-node1 chenzx]# docker info
HTTPS Proxy: http://www.ik8s.io:10080 No Proxy: 127.0.0.0/8,172.16.0.0/16
[root@k8s-node1 chenzx]# kubeadm join 172.16.1.100:6443 --token as5gwu.ktojf6cueg0doexi --discovery-token-ca-cert-hash sha256:399a7de763b95e52084d7bd4cad71dc8fa1bf6dd453b02743d445eee59252cc5 --ignore-preflight-errors=Swap (注意:這個(gè)命令是在kubeadm init初始化中得到的)
[tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap... [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-node1" as an annotation This node has joined the cluster: * Certificate signing request was sent to master and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the master to see this node join the cluster.
[root@k8s-node1 chenzx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE k8s.gcr.io/kube-proxy-amd64 v1.11.1 d5c25579d0ff 7 weeks ago 97.8MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 7 months ago 44.6MB k8s.gcr.io/pause 3.1 da86e6ba6ca1 8 months ago 742kB
[root@k8s-master chenzx]# kubectl get nodes (master上看)
NAME STATUS ROLES AGE VERSION k8s-master Ready master 4h v1.11.2 k8s-node1 Ready <none> 55m v1.11.2
[root@k8s-master chenzx]# kubectl get pods -n kube-system -o wide (master上看)
NAME READY STATUS RESTARTS AGE IP NODE coredns-78fcdf6894-6j6nt 0/1 Running 0 4h <none> k8s-master coredns-78fcdf6894-pnmjj 0/1 Running 0 4h <none> k8s-master etcd-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master kube-apiserver-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master kube-controller-manager-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master kube-flannel-ds-amd64-87tqv 1/1 Running 0 57m 172.16.1.101 k8s-node1 kube-flannel-ds-amd64-txxw2 1/1 Running 0 3h 172.16.1.100 k8s-master kube-proxy-2rf4m 1/1 Running 0 57m 172.16.1.101 k8s-node1 kube-proxy-frkp9 1/1 Running 0 4h 172.16.1.100 k8s-master kube-scheduler-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master
以上命令在node2上也執(zhí)行一遍。
此時(shí)已經(jīng)完成k8s的安裝。
感謝各位的閱讀!關(guān)于“docker中如何初始化k8s集群”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。