前言
這是
和我一步步部署kubernetes叢集項目((fork自
opsnull))中的一篇文章,下 文是結合我
之前部署kubernetes的過程産生的kuberentes環境,部署master節點的
kube-apiserver
、
kube-controller-manager
和
kube-scheduler
的過程。
高可用Kubernetes Master節點安裝
kubernetes master 節點包含的元件:
- kube-apiserver
- kube-scheduler
- kube-controller-manager
目前這三個元件需要部署在同一台機器上。
-
kube-scheduler
kube-controller-manager
三者的功能緊密相關;kube-apiserver
- 同時隻能有一個
kube-scheduler
程序處于工作狀态,如果運作多個,則需要通過選舉産生一個 leader;kube-controller-manager
本文檔記錄部署一個三個節點的高可用 kubernetes master 叢集步驟。(後續建立一個 load balancer 來代理通路 kube-apiserver 的請求)
TLS 證書檔案
pem和token.csv證書檔案我們在
TLS證書和秘鑰這一步中已經建立過了。我們再檢查一下。
$ ls /etc/kubernetes/ssl
admin-key.pem admin.pem ca-key.pem ca.pem kube-proxy-key.pem kube-proxy.pem kubernetes-key.pem kubernetes.pem
下載下傳最新版本的二進制檔案
有兩種下載下傳方式
方式一
從
github release 頁面下載下傳釋出版 tarball,解壓後再執行下載下傳腳本
$ wget https://github.com/kubernetes/kubernetes/releases/download/v1.6.0/kubernetes.tar.gz $ tar -xzvf kubernetes.tar.gz
... $ cd kubernetes
$ ./cluster/get-kube-binaries.sh
...
方式二
CHANGELOG
頁面 下載下傳
client
或
server
tarball 檔案
server
的 tarball
kubernetes-server-linux-amd64.tar.gz
已經包含了
client
(
kubectl
) 二進制檔案,是以不用單獨下載下傳
kubernetes-client-linux-amd64.tar.gz
檔案;
$ # wget https://dl.k8s.io/v1.6.0/kubernetes-client-linux-amd64.tar.gz $ wget https://dl.k8s.io/v1.6.0/kubernetes-server-linux-amd64.tar.gz $ tar -xzvf kubernetes-server-linux-amd64.tar.gz
... $ cd kubernetes
$ tar -xzvf kubernetes-src.tar.gz
将二進制檔案拷貝到指定路徑
$ cp -r server/bin/{kube-apiserver,kube-controller-manager,kube-scheduler,kubectl,kube-proxy,kubelet} /root/local/bin/
配置和啟動 Kube-Apiserver
建立 kube-apiserver的service配置檔案
serivce配置檔案
/usr/lib/systemd/system/kube-apiserver.service
内容:
[Unit] Description=Kubernetes API Service Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=network.target After=etcd.service [Service] EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/apiserver ExecStart=/usr/bin/kube-apiserver \
$KUBE_LOGTOSTDERR \
$KUBE_LOG_LEVEL \
$KUBE_ETCD_SERVERS \
$KUBE_API_ADDRESS \
$KUBE_API_PORT \
$KUBELET_PORT \
$KUBE_ALLOW_PRIV \
$KUBE_SERVICE_ADDRESSES \
$KUBE_ADMISSION_CONTROL \
$KUBE_API_ARGS
Restart=on-failure Type=notify LimitNOFILE=65536 [Install] WantedBy=multi-user.target
/etc/kubernetes/config
檔案的内容為:
### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # logging to stderr means we get it in the systemd journal KUBE_LOGTOSTDERR="--logtostderr=true" # journal message level, 0 is debug KUBE_LOG_LEVEL="--v=0" # Should this cluster be allowed to run privileged docker containers KUBE_ALLOW_PRIV="--allow-privileged=true" # How the controller-manager, scheduler, and proxy find the apiserver #KUBE_MASTER="--master=http://sz-pg-oam-docker-test-001.tendcloud.com:8080" KUBE_MASTER="--master=http://172.20.0.113:8080"
該配置檔案同時被kube-apiserver、kube-controller-manager、kube-scheduler、kubelet、kube-proxy使用。
apiserver配置檔案
/etc/kubernetes/apiserver
内容為:
### ## kubernetes system config ## ## The following values are used to configure the kube-apiserver ## # ## The address on the local server to listen to. #KUBE_API_ADDRESS="--insecure-bind-address=sz-pg-oam-docker-test-001.tendcloud.com"
KUBE_API_ADDRESS="--advertise-address=172.20.0.113 --bind-address=172.20.0.113 --insecure-bind-address=172.20.0.113" # ## The port on the local server to listen on. #KUBE_API_PORT="--port=8080" # ## Port minions listen on #KUBELET_PORT="--kubelet-port=10250" # ## Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=https://172.20.0.113:2379,172.20.0.114:2379,172.20.0.115:2379" # ## Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" # ## default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=ServiceAccount,NamespaceLifecycle,NamespaceExists,LimitRanger,ResourceQuota" # ## Add your own!
KUBE_API_ARGS="--authorization-mode=RBAC --runtime-config=rbac.authorization.k8s.io/v1beta1 --kubelet-https=true --experimental-bootstrap-token-auth --token-auth-file=/etc/kubernetes/token.csv --service-node-port-range=30000-32767 --tls-cert-file=/etc/kubernetes/ssl/kubernetes.pem --tls-private-key-file=/etc/kubernetes/ssl/kubernetes-key.pem --client-ca-file=/etc/kubernetes/ssl/ca.pem --service-account-key-file=/etc/kubernetes/ssl/ca-key.pem --etcd-cafile=/etc/kubernetes/ssl/ca.pem --etcd-certfile=/etc/kubernetes/ssl/kubernetes.pem --etcd-keyfile=/etc/kubernetes/ssl/kubernetes-key.pem --enable-swagger-ui=true --apiserver-count=3 --audit-log-maxage=30 --audit-log-maxbackup=3 --audit-log-maxsize=100 --audit-log-path=/var/lib/audit.log --event-ttl=1h"
-
指定在安全端口使用 RBAC 授權模式,拒絕未通過授權的請求;--authorization-mode=RBAC
- kube-scheduler、kube-controller-manager 一般和 kube-apiserver 部署在同一台機器上,它們使用非安全端口和 kube-apiserver通信;
- kubelet、kube-proxy、kubectl 部署在其它 Node 節點上,如果通過安全端口通路 kube-apiserver,則必須先通過 TLS 證書認證,再通過 RBAC 授權;
- kube-proxy、kubectl 通過在使用的證書裡指定相關的 User、Group 來達到通過 RBAC 授權的目的;
- 如果使用了 kubelet TLS Boostrap 機制,則不能再指定
--kubelet-certificate-authority
--kubelet-client-certificate
選項,否則後續 kube-apiserver 校驗 kubelet 證書時出現 ”x509: certificate signed by unknown authority“ 錯誤;--kubelet-client-key
-
值必須包含--admission-control
;ServiceAccount
-
不能為--bind-address
127.0.0.1
-
配置為runtime-config
,表示運作時的apiVersion;rbac.authorization.k8s.io/v1beta1
-
指定 Service Cluster IP 位址段,該位址段不能路由可達;--service-cluster-ip-range
- 預設情況下 kubernetes 對象儲存在 etcd
路徑下,可以通過/registry
參數進行調整;--etcd-prefix
完整 unit 見
kube-apiserver.service啟動kube-apiserver
$ systemctl daemon-reload
$ systemctl enable kube-apiserver
$ systemctl start kube-apiserver
$ systemctl status kube-apiserver
配置和啟動 Kube-Controller-Manager
建立 kube-controller-manager的serivce配置檔案
檔案路徑
/usr/lib/systemd/system/kube-controller-manager.service
Description=Kubernetes Controller Manager Documentation=https://github.com/GoogleCloudPlatform/kubernetes [Service] EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/controller-manager ExecStart=/usr/bin/kube-controller-manager \
$KUBE_LOGTOSTDERR \
$KUBE_LOG_LEVEL \
$KUBE_MASTER \
$KUBE_CONTROLLER_MANAGER_ARGS
Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
配置檔案
/etc/kubernetes/controller-manager
。
### # The following values are used to configure the kubernetes controller-manager # defaults from config and apiserver should be adequate # Add your own! KUBE_CONTROLLER_MANAGER_ARGS="--address=127.0.0.1 --service-cluster-ip-range=10.254.0.0/16 --cluster-name=kubernetes --cluster-signing-cert-file=/etc/kubernetes/ssl/ca.pem --cluster-signing-key-file=/etc/kubernetes/ssl/ca-key.pem --service-account-private-key-file=/etc/kubernetes/ssl/ca-key.pem --root-ca-file=/etc/kubernetes/ssl/ca.pem --leader-elect=true"
-
參數指定 Cluster 中 Service 的CIDR範圍,該網絡在各 Node 間必須路由不可達,必須和 kube-apiserver 中的參數一緻;--service-cluster-ip-range
-
指定的證書和私鑰檔案用來簽名為 TLS BootStrap 建立的證書和私鑰;--cluster-signing-*
-
用來對 kube-apiserver 證書進行校驗,指定該參數後,才會在Pod 容器的 ServiceAccount 中放置該 CA 證書檔案;--root-ca-file
-
值必須為--address
,因為目前 kube-apiserver 期望 scheduler 和 controller-manager 在同一台機器,否則:127.0.0.1
$ kubectl get componentstatuses
NAME STATUS MESSAGE ERROR
scheduler Unhealthy Get http://127.0.0.1:10251/healthz: dial tcp 127.0.0.1:10251: getsockopt: connection refused
controller-manager Healthy ok
etcd-2 Unhealthy Get http://172.20.0.113:2379/health: malformed HTTP response "\x15\x03\x01\x00\x02\x02"
etcd-0 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
參考:
https://github.com/kubernetes-incubator/bootkube/issues/64 kube-controller-manager.service啟動 Kube-Controller-Manager
$ systemctl daemon-reload
$ systemctl enable kube-controller-manager
$ systemctl start kube-controller-manager
配置和啟動 Kube-Scheduler
建立 kube-scheduler的serivce配置檔案
/usr/lib/systemd/system/kube-scheduler.serivce
[Unit] Description=Kubernetes Scheduler Plugin Documentation=https://github.com/GoogleCloudPlatform/kubernetes [Service] EnvironmentFile=-/etc/kubernetes/config EnvironmentFile=-/etc/kubernetes/scheduler ExecStart=/usr/bin/kube-scheduler \
$KUBE_LOGTOSTDERR \
$KUBE_LOG_LEVEL \
$KUBE_MASTER \
$KUBE_SCHEDULER_ARGS
Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
/etc/kubernetes/scheduler
### # kubernetes scheduler config # default config should be adequate # Add your own!
KUBE_SCHEDULER_ARGS="--leader-elect=true --address=127.0.0.1"
-
--address
,因為目前 kube-apiserver 期望 scheduler 和 controller-manager 在同一台機器;127.0.0.1
啟動 Kube-Scheduler
$ systemctl daemon-reload
$ systemctl enable kube-scheduler
$ systemctl start kube-scheduler
驗證 Master 節點功能
$ kubectl get componentstatuses
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health": "true"}
etcd-1 Healthy {"health": "true"}
etcd-2 Healthy {"health": "true"}
後記
當時在配置過程中遇到了問題
TLS認證相關的問題,其實就是因為配置apiserver時候etcd的協定寫成了http導緻的,應該是用https。
Opsnull寫的kubernetes高可用master叢集部署過程中似乎并沒有包括高可用的配置,才雲科技的唐繼元分享過
Kubernetes Master High Availability 進階實踐究竟如何實作kubernetes master的高可用還需要繼續探索。
本文轉自中文社群-
Kubernetes高可用Master節點安裝