阿里雲ubuntu16.04搭建k8s雙節點(master,worker)集群


經簡單了解,k8s集群部署的方式有很多:

  • 使用kops,社區提供的工具,此方法不利於學習k8s
  • 使用minikube安裝單節點集群
  • 采用工具kubuadm,官方提供的工具(本文采用此方法)
  • 純手動安裝,有助於了解k8s的細節

本文主要包括以下五個步驟:

step0.准備工作

  兩台服務器的准備

step1.每台機器上安裝runtime

  docker的安裝

step2.在每台機器上安裝kubeadm,kubelet和kubectl

  基本組件安裝

step3.創建集群

  在master節點,創建k8s集群

step4加入子節點

  在子節點操作,加入到master的集群中

 

 

step0.准備工作

為了方便學習和實踐k8s,需要搭建一套自己的k8s環境,因此在阿里雲買了兩台機器,信息如下:

  地域:美國(弗吉尼亞)

  CPU:2核(共享型)

  內存:2GB

  存儲:40GB

  寬帶:100Mbps按流量計費(0.5/GB)

  數量:*2

  時間:一個月

  總額:183.8元

(ps:k8s官方聲明的最低配置是2u2g,所以先買一個月試試,后面如果不需要的話就不續費了)

 

step1.每台機器上安裝runtime

k8s官方從v1.6.0起開始支持CRI,即容器運行時接口。官方提到的CRI有:Docker、CRI-O、Containerd、fracti等等。不過目前Docker幾乎成為業界主流的容器引擎,且k8s默認支持它,所以我們選擇使用Docker。

k8s發行說明中跟蹤最新驗證的Docker版本,因此我們安裝最新的docker 19.03.1-ce

Docker官方出品了一件安裝腳本,腳本會自動區分不同的操作系統。

$ sudo wget -qO- https://get.docker.com/ | bash

$ # 如果上面的不行,執行下面兩句
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

$ # 安裝成功執行下面語句,如果有類似回顯,說明安裝成功
$ docker --version
Docker version 18.06.1-ce, build e68fc7a

 

安裝成功截圖:

 參考:https://zhuanlan.zhihu.com/p/54147784

 

step2.在每台機器上安裝kubeadm,kubelet和kubectl

kubeadm: 用來初始化集群的指令。

kubelet: 在集群中的每個節點上用來啟動 pod 和 container 等。

kubectl: 用來與集群通信的命令行工具。

官方文檔提示,kubeadm不能幫忙管理kubelet或kubectl,因此他們之間的版本一定要一致,不然會出問題。

接下來根據官方文檔的提示一步一步操作。

參考: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

 

apt-get update && apt-get install -y apt-transport-https curl

 

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

 

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

 

apt-get update

 

 

apt-get install -y kubelet kubeadm kubectl

  

。。。。。。

 

apt-mark hold kubelet kubeadm kubectl

 

kubelet 現在每隔幾秒就會重啟,因為它陷入了一個等待 kubeadm 指令的死循環。

版本信息如下:

 

如果安裝的CRI不是Docker的話,這里還要加一個步驟,在master節點上配置kubelet所需的cgroup驅動。

由於我們用的是docker所以跳過此步。

 

step3.創建集群

在master節點使用kubeadm init進行初始化。

這個操作包括了以下11個大步驟

 

kubeadm init這個命令的參數非常多,官方給出的全部參數如下:

kubeadm init [flags]
--apiserver-advertise-address string The IP address the API Server will advertise it's listening on. If not set the default network interface will be used.
--apiserver-bind-port int32 Port for the API Server to bind to. (default 6443)
--apiserver-cert-extra-sans strings Optional extra Subject Alternative Names (SANs) to use for the API Server serving certificate. Can be both IP addresses and DNS names.
--cert-dir string The path where to save and store the certificates. (default "/etc/kubernetes/pki")
--certificate-key string Key used to encrypt the control-plane certificates in the kubeadm-certs Secret.
--config string Path to a kubeadm configuration file.
--cri-socket string Path to the CRI socket to connect. If empty kubeadm will try to auto-detect this value; use this option only if you have more than one CRI installed or if you have non-standard CRI socket.
--dry-run Don't apply any changes; just output what would be done.
--feature-gates string A set of key=value pairs that describe feature gates for various features. No feature gates are available in this release.
-h, --help help for init
--ignore-preflight-errors strings A list of checks whose errors will be shown as warnings. Example: 'IsPrivilegedUser,Swap'. Value 'all' ignores errors from all checks.
--image-repository string Choose a container registry to pull control plane images from (default "k8s.gcr.io")
--kubernetes-version string Choose a specific Kubernetes version for the control plane. (default "stable-1")
--node-name string Specify the node name.
--pod-network-cidr string Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node.
--service-cidr string Use alternative range of IP address for service VIPs. (default "10.96.0.0/12")
--service-dns-domain string Use alternative domain for services, e.g. "myorg.internal". (default "cluster.local")
--skip-certificate-key-print Don't print the key used to encrypt the control-plane certificates.
--skip-phases strings List of phases to be skipped
--skip-token-print Skip printing of the default bootstrap token generated by 'kubeadm init'.
--token string The token to use for establishing bidirectional trust between nodes and control-plane nodes. The format is [a-z0-9]{6}\.[a-z0-9]{16} - e.g. abcdef.0123456789abcdef
--token-ttl duration The duration before the token is automatically deleted (e.g. 1s, 2m, 3h). If set to '0', the token will never expire (default 24h0m0s)
--upload-certs Upload control-plane certificates to the kubeadm-certs Secret.

 

這里我們只選擇幾個基本參數嘗試以下初始化

kubeadm init --apiserver-advertise-address=0.0.0.0 --pod-network-cidr=10.244.0.0/16

得到結果:

 

顯示創建成功,只是在preflight階段有一個warning,先不管他。

記錄下這里的信息,加入worker節點時候會用到。

 

下一步, api認證。

 

因為我准備只用root所以運行下面的命令

export KUBECONFIG=/etc/kubernetes/admin.conf

執行了這一步操作后,kubectl已經可以開始工作了,也可以查看一下node的情況。

可以查看node,不過STATUS依然是NotReady狀態。

 

官方文檔上面在加入子節點前還給了兩個操作選項:

  • 安裝pod網絡附加組件(必要)
  • 控制面節點隔離

 網絡pod插件的功能是讓pod之間能夠進行網絡通信。官方提供了很多的pod網絡組件,而我們在kubeadm init的時候已經選擇了Canal網絡組件。

現在需要執行這個命令配置net-port

kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/canal.yaml

現在node的status狀態也是正常Ready狀態了

 

而控制面節點隔離操作是這樣的,原本k8s不在master節點上安排pod編排,而執行控制面隔離后后,master節點上也講安排pod,這是默認不允許的,也是不常用的,因為會引起安全問題。

雖然我現在只有一個機器做子節點,我也不想做這個操作,所以跳過。

 

 

整個step3中如果出現了什么錯誤可以用以下命令充值kubeadm,然后從kubeinit操作開始重新來。

 sudo kubeadm reset

 

 現在的namespace和pod狀態如下:看時間都是50m左右,因此都是再kubeadm init操作后產生的。

 canal是pod網絡有關組件,是剛配置好的,所以是新的。這里不能再墨跡了。

 

 step4加入子節點

如果忘記了剛才master節點給出的hash信息,可以執行以下命令重新獲取

 kubeadm token create --print-join-command

 

復制得到的命令,在子節點執行

 kubeadm token create --print-join-command

 成功加入集群

 再到master節點查看結果。成功!!!

 

 

 

 

 本文可能有很多描述不清楚或不准確的地方,大家請諒解。

 記錄k8s安裝過程也是為了深入理解它,以后發現哪里可以補充的,我會再回來補充。

 

參考:

https://kubernetes.io/zh/docs/setup/independent/install-kubeadm/#%E5%AE%89%E8%A3%85-runtime

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

https://blog.csdn.net/weixin_38070561/article/details/82982710

https://blog.csdn.net/u010827484/article/details/83025404

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM