install-kubeadm/
create-cluster-kubeadm/
control-plane
https://www.rfc-archive.org/getrfc.php?rfc=3746
網絡層面的定義。control plane , data plane。
1. 網絡配置準備
開啟br_netfilter mod
test@single_k8s:~$ lsmod |grep br_netfilter
test@single_k8s:~$ sudo modprobe br_netfilter
[sudo] password for test:
test@single_k8s:~$ lsmod |grep br_netfilter
br_netfilter 24576 0
bridge 155648 1 br_netfilter
配置內核
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
test@single_k8s:~$ sudo sysctl -a |grep bridge-nf-call
net.bridge.bridge-nf-call-arptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
sysctl: reading key "net.ipv6.conf.all.stable_secret"
sysctl: reading key "net.ipv6.conf.default.stable_secret"
sysctl: reading key "net.ipv6.conf.ens160.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
2. 安裝runtime
Kubernetes 使用 container runtime 來在Pods中運行容器。
container runtime
負責運行容器的軟件。
默認,Kubernetes 使用 Container Runtime Interface (CRI) 來面向 container runtime。
默認,Kubeadm 使用 Docker 作為 container runtime。
Kubelet 通過內建的 dockershim
CRI 實現與 Docker 的集成。
root@single_k8s:~# apt-get update && apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg2
root@single_k8s:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
OK
root@single_k8s:~# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
root@single_k8s:~# apt-get update && apt-get install -y containerd.io=1.2.13-1 docker-ce=5:19.03.8~3-0~ubuntu-$(lsb_release -cs) docker-ce-cli=5:19.03.8~3-0~ubuntu-$(lsb_release -cs)
root@single_k8s:~# cat /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
root@single_k8s:~# mkdir -p /etc/systemd/system/docker.service.d
root@single_k8s:~# systemctl daemon-reload
root@single_k8s:~# systemctl restart docker
test@single_k8s:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
3. 安裝 kubeadm, kubelet and kubectl
國內連不上google的,所以必須手動安裝
test@single_k8s:~$ sudo apt-get update && sudo apt-get install -y apt-transport-https curl
test@single_k8s:~$ cat /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
國內源安裝
如下使用root執行
sudo apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
安裝指定版本的kubernetes
apt-get install kubeadm=1.10.2-00 kubectl=1.10.2-00 kubelet=1.10.2-00
4. 初始化 control-plane 節點
Control-plane 節點即為運行 control plane 組件的主機,包括 etcd 和 API Server (用于對接 kubectl )。
(推薦) 如果有計劃升級 單control-plane節點集群到 HA ,建議配置 --control-plane-endpoint
來設置 shared endpoint 來向所有 control-plane 節點提供訪問。Endpoint 可以是 DNS name 或 負載均衡IP 。
選擇 Pod 網絡插件, 然后確認是否有參數提交給 kubeadm init
。取決與你選擇的第三方插件,你可能需要設置 --pod-network-cidr
為第三方指定的值。參見 Installing a Pod network add-on。
(可選) 自版本 1.14 以后, kubeadm
使用一份domain socket paths清單來自動發現容器運行環境。如果使用不同的容器環境,或者安裝了多個,需要指定 --cri-socket
參數給 kubeadm init
。參見 Installing runtime.
(可選) 除非特別指定,kubeadm 使用默認網關對應的網卡接口地址作為這個control-plane 節點 API server的通告地址。要使用其他網絡接口地址,需要指定 --apiserver-advertise-address=<ip-address>
參數給 kubeadm init。要部署使用IPv6,需要指定IPv6地址,比如 --apiserver-advertise-address=fd00::101
。
(可選) 運行 kubeadm config images pull
prior to kubeadm init
to verify connectivity to the gcr.io container image registry.
kubeadm init <args>
Considerations about apiserver-advertise-address and ControlPlaneEndpoint
--apiserver-advertise-address 可以設置 control-plane 節點 API server 的 通告地址。
--control-plane-endpoint 可以設置所有control-plane 節點的 shared endpoint。
--control-plane-endpoint 允許IP地址,也允許DNS names。請聯系管理員設置DNS和IP的映射。
More information
-
kubeadm init
參數,參見 kubeadm reference guide。 - 配置列表,參見 configuration file documentation.
- 定制化 control plane 組件,比如可選的IPv6 assignment to liveness probe for control plane components 和 etcd server,提供額外的配置參數,參見 custom arguments.
- 重復執行
kubeadm init
,必須先執行 tear down the cluster.
如果加入的節點與cluster部署于同一架構,要確保部署的 DaemonSets 支持這種架構的容器鏡像。
kubeadm init 在運行Kubernetes前會執行一系列預檢,來確保可以運行Kubernetes。這些預檢會報warnings ,或者在errors的時候退出。
kubeadm init 然后下載和安裝cluster control plane的組件。
- 如果以非root用戶運行 kubectl ,配置如下:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- 如果以root用戶運行 kubectl,配置如下:
export KUBECONFIG=/etc/kubernetes/admin.conf
使用 kubeadm join
命令添加節點到集群,參見 join nodes to your cluster.
Token 用于 control-plane 節點 和 joining 節點之間的認證。Token的內容要保密,任何擁有token的人都可以添加認證的節點到集群。Token 可以通過kubeadm token
命令執行 listed, created, deleted 。參見 kubeadm reference guide.
5. 安裝Pod網絡插件
注意:
這里有網絡設置和部署順序的重要內容。
必須部署基于Pod網絡插件的Container Network Interface (CNI) ,這樣 Pods 之間才可以互相通信。Cluster >DNS (CoreDNS) 在Pod網絡插件安裝前不會啟動。
Pod network 必須不能與 host networks 重疊。
(如果 Pod network 和 某些host networks沖突,要考慮使用 CIDR block 來替代,然后kubeadm init
指定--pod-network-cidr
,以及替換network插件的YAML內容。)。默認,
kubeadm
在設置集群的時候會開啟 RBAC (role based access control)。
確保 Pod network plugin 支持 RBAC,然后按提示部署。如果使用雙棧IPv6,或在cluster內使用單棧IPv6,確保 Pod network plugin 支持IPv6。
IPv6 的支持在 v0.6.0 添加到 CNI 。
一個集群只能安裝一個 Pod network 。
使用如下命令在 control-plane 節點或擁有kubeconfig credentials的節點安裝 Pod network 插件。
kubectl apply -f <add-on.yaml>
如下網絡插件:
Calico
Calico提供networking和network policy。Calico 提供一些可選的網絡配置集,根據你的環境選擇最有效的配置,比如是否可重疊網絡,是否擁有BGP。Calico 使用同一個引擎來向hosts, pods, 和工作在service mesh層的applications(如果開啟Istio & Envoy)提供network policy。Calico 支持 amd64, arm64, and ppc64le 架構。
Calico 會基于--pod-network-cidr
或 kubeadm’s configuration 自動檢測pods使用的IP范圍。
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
Cilium
Cilium 可以作為 kube-proxy 的替代,參見 Kubernetes without kube-proxy。
關于在Kubernetes上使用Cilium的更多內容,參見Kubernetes Install guide for Cilium。
Cilium 需要在kubeadm init
添加 --pod-network-cidr=10.217.0.0/16
參數。
部署 Cilium :
kubectl create -f https://raw.githubusercontent.com/cilium/cilium/v1.6/install/kubernetes/quick-install.yaml
當所有的 Cilium Pods 標記為 READY,就可以使用了。如下:
kubectl get pods -n kube-system --selector=k8s-app=cilium
NAME READY STATUS RESTARTS AGE
cilium-drxkl 1/1 Running 0 18m
Contiv-VPP
Contiv-VPP 使用了基于FD.io VPP的可編程 CNF vSwitch,提供多功能&高性能的云原生網絡和服務。
它在用戶空間層實現 k8s 服務和網絡策略(on VPP)。
Contiv-VPP Manual Installation
Kube-router
Kube-router 依賴 kube-controller-manager 來分配Pod CIDR地址。因此,kubeadm init
使用配置項 --pod-network-cidr
。
Kube-router 提供 Pod 網絡、網絡策略、高性能的基于IP Virtual Server(IPVS)/Linux Virtual Server(LVS) 的服務代理。
設置參見setup guide。
Weave Net
Weave Net集成,參見 Integrating Kubernetes via the Addon.
Weave Net 支持 amd64
, arm
, arm64
and ppc64le
架構,而不需要額外的設置。Weave Net 默認設置為 hairpin 模式。這允許Pods在不知道它們的PodIP,以Service IP address訪問它們自己。
Pod network 安裝完畢后,可以通過查看CoreDNS Pod 正在 Running
來判斷是否安裝成功。查看命令kubectl get pods --all-namespaces
。在 CoreDNS Pod 起來并且運行后,你才可以添加節點進集群。
如果 network 沒有工作,或者 CoreDNS 不在 Running
狀態,參見 troubleshooting guide 。
6. Control Plane 節點隔離
默認,基于安全原因,集群并不會在control-plane節點上部署Pods。如果你需要在control-plane上部署Pods,比如用于development的單主機集群,需要執行如下:
kubectl taint nodes --all node-role.kubernetes.io/master-
輸出范例如下:
node "test-01" untainted
taint "node-role.kubernetes.io/master:" not found
taint "node-role.kubernetes.io/master:" not found
這將從任何擁有node-role.kubernetes.io/master taint的節點上刪除它, 包括control-plane節點,即意味著scheduler可以規劃、部署Pods在任何地方。
7. 添加節點
節點( nodes )即容器(containers and Pods...)運行的地方。添加節點到集群,執行如下操作:
- SSH 到指定主機
- 切到 root (比如 sudo su -)
- 執行
kubeadm init
完畢時提示的如下命令。如下:
kubeadm join --token <token> <control-plane-host>:<control-plane-port> --discovery-token-ca-cert-hash sha256:<hash>
如果不知道 token,你可以在control-plane節點上執行如下命令查看:
kubeadm token list
輸出范例:
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
8ewj1p.9r9hcjoqgajrj4gi 23h 2018-06-12T02:51:28Z authentication, The default bootstrap system:
signing token generated by bootstrappers:
'kubeadm init'. kubeadm:
default-node-token
默認,tokens 超時時間是24小時。如果你在當前token過期后添加節點,你可以在control-plane節點上執行如下命令創建新token:
kubeadm token create
輸出范例:
5didvk.d09sbcov8ph2amjw
如果你沒有 --discovery-token-ca-cert-hash,你可以在control-plane節點執行如下命令獲取:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
輸出范例:
8cb2de97839780a412b93877f8507ad6c94f73add17d5d7058e91741c9d5ec78
Note:如果<control-plane-host>:<control-plane-port>指定IPv6地址 ,IPv6 地址必須被方括號包圍,比如: [fd00::101]:2073
添加節點成功的輸出范例:
[preflight] Running pre-flight checks
... (log output of join workflow) ...
Node join complete:
* Certificate signing request sent to control-plane and response
received.
* Kubelet informed of new secure connection details.
Run 'kubectl get nodes' on control-plane to see this machine join.
(可選) 在非control-plane節點上控制集群
要在其他地方(比如:你的筆記本)上通過kubectl連到集群,你需要從control-plane節點上拷貝administrator kubeconfig file 到你的電腦,如下:
scp root@<control-plane-host>:/etc/kubernetes/admin.conf .
kubectl --kubeconfig ./admin.conf get nodes
Note:范例開啟了root的SSH訪問權限。如果沒有權限,可以拷貝 admin.conf 文件到其他可訪問的用戶目錄,然后用那個用戶拷貝過去。admin.conf 文件給與了集群的超級管理員權限。盡量不要使用這個文件。對于普通用戶,建議根據你的白名單權限創建唯一的收取按。使用如下命令
kubeadm alpha kubeconfig user --client-name <CN>
。這個命令會輸出KubeConfig文件到STDOUT,然后你需要保存它并發布給你的用戶。之后,執行kubectl create (cluster)rolebinding
進行白名單授權。
(Optional) Proxying API Server to localhost
如果你需要在集群外訪問API Server,你可以使用kubectl proxy:
scp root@<control-plane-host>:/etc/kubernetes/admin.conf .
kubectl --kubeconfig ./admin.conf proxy
現在你可以在本地通過http://localhost:8001/api/v1訪問 API Server了。
8. 清除集群
如果你使用的可自由支配的服務器,構建的集群是臨時測試使用,你可以關閉集群,而不需要更深入的clean up。你可以使用 kubectl config delete-cluster
來刪除集群。
但是如果你要更深度的清理,首先需要 drain the node 并且確認節點空了,然后再刪除該節點的配置。
Remove the node
使用適當的憑據與 control-plane 節點通信:
kubectl drain <node name> --delete-local-data --force --ignore-daemonsets
kubectl delete node <node name>
然后,在被removed的節點上, reset 所有 kubeadm 安裝的狀態:
kubeadm reset
Reset 進程不會重置或清理iptables rules or IPVS tables。
如果需要重置iptables,手動操作:
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
如果需要重置IPVS tables,手動執行如下命令:
ipvsadm -C
如果If you wish to start over simply run kubeadm init
or kubeadm join
with the appropriate arguments
Clean up the control plane
在control plane節點使用kubeadm reset
可以觸發最有效的清理。
參考 kubeadm reset
What's Next
- Verify that your cluster is running properly with Sonobuoy
- See Upgrading kubeadm clusters for details about upgrading your cluster using
kubeadm
. - Learn about advanced
kubeadm
usage in the kubeadm reference documentation - Learn more about Kubernetes concepts and
kubectl
. - See the Cluster Networking page for a bigger list of Pod network add-ons.
- See the list of add-ons to explore other add-ons, including tools for logging, monitoring, network policy, visualization & control of your Kubernetes cluster.
- Configure how your cluster handles logs for cluster events and from applications running in Pods. See Logging Architecture for an overview of what is involved.
Feedback
- For bugs, visit the kubeadm GitHub issue tracker
- For support, visit the #kubeadm Slack channel
- General SIG Cluster Lifecycle development Slack channel: #sig-cluster-lifecycle
- SIG Cluster Lifecycle SIG information
- SIG Cluster Lifecycle mailing list: kubernetes-sig-cluster-lifecycle
版本偏移
kubeadm
工具 v1.18 的版本可以部署v1.18 or v1.17 版本control plane的集群。kubeadm
v1.18 也可以用于升級v1.17版本kubeadm創建的集群。
但是我們不知道未來,所以kubeadm CLI v1.18 可以,或許不可以部署v1.19集群。
如下資源提供了關于kubelets,control plane,其他Kubernetes 組件版本偏移的更多信息。
- Kubernetes version and version-skew policy
- Kubeadm-specific installation guide
限制
集群的彈性/快速恢復
這里創建的cluster有一個單control-plane節點,運行了單 etcd 數據庫。這意味著,如果control-plane節點故障了,集群會丟失數據,或者需要recreate。
Workarounds:
Regularly back up etcd。kubeadm 配置的 etcd 數據目錄位于control-plane節點的
/var/lib/etcd
。使用多 control-plane 節點。參見Options for Highly Available topology 來選擇一個提供HA的集群拓撲。
平臺兼容
kubeadm deb/rpm 包和二進制 編譯支持平臺 amd64, arm (32-bit), arm64, ppc64le, and s390x following the multi-platform proposal.
自v1.12版本后,control plane and addons的多平臺容器鏡像也被支持了。
只有部分network providers 提供全平臺支持。