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

經簡單瞭解,k8s集羣部署的方式有不少:node

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

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

step0.準備工做bootstrap

  兩臺服務器的準備api

step1.每臺機器上安裝runtime安全

  docker的安裝bash

step2.在每臺機器上安裝kubeadm,kubelet和kubectl服務器

  基本組件安裝網絡

step3.建立集羣app

  在master節點,建立k8s集羣dom

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

http://www.javashuo.com/article/p-kpjzwnba-kc.html

相關文章
相關標籤/搜索