摘要:Kubernetes是Google開源的容器集群管理系統,基于Docker構建一個容器的調度服務,提供資源調度、均衡容災、服務注冊、動態擴縮容等功能套件。本文介紹如何基于CentOS7.0構建Kubernetes平臺。
Kubernetes是Google開源的容器集群管理系統,基于Docker構建一個容器的調度服務,提供資源調度、均衡容災、服務注冊、動態擴縮容等功能套件,目前新版本為0.6.2。本文介紹如何基于CentOS7.0構建Kubernetes平臺。
作者簡介:劉天斯,目前就職于騰訊-互動娛樂部(高級工程師),曾就職于天涯社區,擔任架構師/系統管理員,熱衷開源技術的研究,包括系統架構、運維開發、負載均衡、緩存技術、數據庫、分布式存儲及云計算等領域,擅長大規模集群的運維工作。關注互聯網技術發展動向,努力緊靠技術前沿。充當一名普通的傳播者和分享者。 著有《python自動化運維:技術與實踐》、《Docker技術與實踐》(預計2015年5月出版)。
在正式介紹之前,大家有必要先理解Kubernetes幾個核心概念及其承擔的功能。以下為Kubernetes的架構設計圖:
1. Pods
在Kubernetes系統中,調度的小顆粒不是單純的容器,而是抽象成一個Pod,Pod是一個可以被創建、銷毀、調度、管理的小的部署單元。比如一個或一組容器。
2. Replication Controllers
Replication Controller是Kubernetes系統中有用的功能,實現復制多個Pod副本,往往一個應用需要多個Pod來支撐,并且可以保證其復制的副本數,即使副本所調度分配的宿主機出現異常,通過Replication Controller可以保證在其它主宿機啟用同等數量的Pod。Replication Controller可以通過repcon模板來創建多個Pod副本,同樣也可以直接復制已存在Pod,需要通過Label selector來關聯。
3. Services
Services是Kubernetes外圍的單元,通過虛擬一個訪問IP及服務端口,可以訪問我們定義好的Pod資源,目前的版本是通過iptables的nat轉發來實現,轉發的目標端口為Kube_proxy生成的隨機端口,目前只提供GOOGLE云上的訪問調度,如GCE。如果與我們自建的平臺進行整合?請關注下篇《kubernetes與HECD架構的整合》文章。
4. Labels
Labels是用于區分Pod、Service、Replication Controller的key/value鍵值對,僅使用在Pod、Service、Replication Controller之間的關系識別,但對這些單元本身進行操作時得使用name標簽。
5. Proxy
Proxy不但解決了同一主宿機相同服務端口沖突的問題,還提供了Service轉發服務端口對外提供服務的能力,Proxy后端使用了隨機、輪循負載均衡算法。
說說個人一點看法,目前Kubernetes 保持一周一小版本、一個月一大版本的節奏,迭代速度極快,同時也帶來了不同版本操作方法的差異,另外官網文檔更新速度相對滯后及欠缺,給初學者帶來一定挑戰。在上游接入層官方側重點還放在GCE(Google Compute Engine)的對接優化,針對個人私有云還未推出一套可行的接入解決方案。在v0.5版本中才引用service代理轉發的機制,且是通過iptables來實現,在高并發下性能令人擔憂。但作者依然看好Kubernetes未來的發展,至少目前還未看到另外一個成體系、具備良好生態圈的平臺,相信在V1.0時就會具備生產環境的服務支撐能力。
一、環境部署
1. 平臺版本說明
Centos7.0 OS
Kubernetes V0.6.2
etcd version 0.4.6
Docker version 1.3.2
2. 平臺環境說明
3. 環境安裝
1)系統初始化工作(所有主機)
系統安裝-選擇[小化安裝]
# yum -y install wget ntpdate bind-utils
# wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/epel-release-7-2.noarch.rpm
# yum update
CentOS 7.0默認使用的是firewall作為防火墻,這里改為iptables防火墻(熟悉度更高,非必須)。
1.1 關閉firewall:1.1、關閉firewall:
# systemctl stop firewalld.service #停止firewall
# systemctl disable firewalld.service #禁止firewall開機啟動
1.2 安裝iptables防火墻
# yum install iptables-services #安裝
# systemctl start iptables.service #后重啟防火墻使配置生效
# systemctl enable iptables.service #設置防火墻開機啟動
2)安裝Etcd(192.168.1.10主機)
# mkdir -p /home/install && cd /home/install
# wget https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz
# tar -zxvf etcd-v0.4.6-linux-amd64.tar.gz
# cd etcd-v0.4.6-linux-amd64
# cp etcd* /bin/
# /bin/etcd -version
etcd version 0.4.6
啟動服務etcd服務,如有提供第三方管理需求,另需在啟動參數中添加“-cors='*'”參數。
# mkdir /data/etcd
# /bin/etcd -name etcdserver -peer-addr 192.168.1.10:7001 -addr 192.168.1.10:4001 -data-dir /data/etcd -peer-bind-addr 0.0.0.0:7001 -bind-addr 0.0.0.0:4001 &
配置etcd服務防火墻,其中4001為服務端口,7001為集群數據交互端口。
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 4001 -j ACCEPT
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 7001 -j ACCEPT
3)安裝Kubernetes(涉及所有Master、Minion主機)
通過yum源方式安裝,默認將安裝etcd, docker, and cadvisor相關包。
# curl https://copr.fedoraproject.org/coprs/eparis/kubernetes-epel-7/repo/epel-7/eparis-kubernetes-epel-7-epel-7.repo -o /etc/yum.repos.d/eparis-kubernetes-epel-7-epel-7.repo
#yum -y install kubernetes
升級至v0.6.2,覆蓋bin文件即可,方法如下:
# mkdir -p /home/install && cd /home/install
# wget https://github.com/GoogleCloudPlatform/kubernetes/releases/download/v0.6.2/kubernetes.tar.gz
# tar -zxvf kubernetes.tar.gz
# tar -zxvf kubernetes/server/kubernetes-server-linux-amd64.tar.gz
# cp kubernetes/server/bin/kube* /usr/bin
校驗安裝結果,出版以下信息說明安裝正常。
[root@SN2014-12-200 bin]# /usr/bin/kubectl version
Client Version: version.Info{Major:"0", Minor:"6+", GitVersion:"v0.6.2", GitCommit:"729fde276613eedcd99ecf5b93f095b8deb64eb4", GitTreeState:"clean"}
Server Version: &version.Info{Major:"0", Minor:"6+", GitVersion:"v0.6.2", GitCommit:"729fde276613eedcd99ecf5b93f095b8deb64eb4", GitTreeState:"clean"}
4)Kubernetes配置(僅Master主機)
master運行三個組件,包括apiserver、scheduler、controller-manager,相關配置項也只涉及這三塊。
4.1【/etc/kubernetes/config】
# Comma seperated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd_servers=http://192.168.1.10:4001"
# 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 privleged docker containers
KUBE_ALLOW_PRIV="--allow_privileged=false"
4.2【/etc/kubernetes/apiserver】
# The address on the local server to listen to.
KUBE_API_ADDRESS="--address=0.0.0.0"
# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"
# How the replication controller and scheduler find the kube-apiserver
KUBE_MASTER="--master=192.168.1.200:8080"
# Port minions listen on
KUBELET_PORT="--kubelet_port=10250"
# Address range to use for services
KUBE_SERVICE_ADDRESSES="--portal_net=10.254.0.0/16"
# Add you own!
KUBE_API_ARGS=""
4.3【/etc/kubernetes/controller-manager】
# Comma seperated list of minions
KUBELET_ADDRESSES="--machines= 192.168.1.201,192.168.1.202"
# Add you own!
KUBE_CONTROLLER_MANAGER_ARGS=""
4.4【/etc/kubernetes/scheduler】
# Add your own!
KUBE_SCHEDULER_ARGS=""
啟動master側相關服務
# systemctl daemon-reload
# systemctl start kube-apiserver.service kube-controller-manager.service kube-scheduler.service
# systemctl enable kube-apiserver.service kube-controller-manager.service kube-scheduler.service
5)Kubernetes配置(僅minion主機)
minion運行兩個組件,包括kubelet、proxy,相關配置項也只涉及這兩塊。
Docker啟動腳本更新
# vi /etc/sysconfig/docker
添加:-H tcp://0.0.0.0:2375,終配置如下,以便以后提供遠程API維護。
OPTIONS=--selinux-enabled -H tcp://0.0.0.0:2375 -H fd://
修改minion防火墻配置,通常master找不到minion主機多半是由于端口沒有連通。
iptables -I INPUT -s 192.168.1.200 -p tcp --dport 10250 -j ACCEPT
修改kubernetes minion端配置,以192.168.1.201主機為例,其它minion主機同理。
5.1【/etc/kubernetes/config】
# Comma seperated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd_servers=http://192.168.1.10:4001"
# 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 privleged docker containers
KUBE_ALLOW_PRIV="--allow_privileged=false"
5.2【/etc/kubernetes/kubelet】
###
# kubernetes kubelet (minion) config
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"
# The port for the info server to serve on
KUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname_override=192.168.1.201"
# Add your own!
KUBELET_ARGS=""
5.3【/etc/kubernetes/proxy】
KUBE_PROXY_ARGS=""
啟動kubernetes服務
# systemctl daemon-reload
# systemctl enable docker.service kubelet.service kube-proxy.service
# systemctl start docker.service kubelet.service kube-proxy.service
3.校驗安裝(在master主機操作,或可訪問master主機8080端口的client api主機)
1) kubernetes常用命令
# kubectl get minions #查查看minion主機
# kubectl get pods #查看pods清單
# kubectl get services 或 kubectl get services -o json #查看service清單
# kubectl get replicationControllers #查看replicationControllers清單
# for i in `kubectl get pod|tail -n +2|awk '{print $1}'`; do kubectl delete pod $i; done #刪除所有pods
或者通過Server api for REST方式(推薦,及時性更高):
# curl -s -L http://192.168.1.200:8080/api/v1beta1/version | python -mjson.tool #查看kubernetes版本
# curl -s -L http://192.168.1.200:8080/api/v1beta1/pods | python -mjson.tool #查看pods清單
# curl -s -L http://192.168.1.200:8080/api/v1beta1/replicationControllers | python -mjson.tool #查看replicationControllers清單
# curl -s -L http://192.168.1.200:8080/api/v1beta1/minions | python -m json.tool #查查看minion主機
# curl -s -L http://192.168.1.200:8080/api/v1beta1/services | python -m json.tool #查看service清單
注:在新版Kubernetes中,所有的操作命令都整合至kubectl,包括kubecfg、kubectl.sh、kubecfg.sh等
2)創建測試pod單元
# /home/kubermange/pods && cd /home/kubermange/pods
# vi apache-pod.json
{
"id": "fedoraapache",
"kind": "Pod",
"apiVersion": "v1beta1",
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "fedoraapache",
"containers": [{
"name": "fedoraapache",
"image": "fedora/apache",
"ports": [{
"containerPort": 80,
"hostPort": 8080
}]
}]
}
},
"labels": {
"name": "fedoraapache"
}
}
# kubectl create -f apache-pod.json
# kubectl get pod
NAME IMAGE(S) HOST LABELS STATUS
fedoraapache fedora/apache 192.168.1.202/ name=fedoraapache Running
啟動瀏覽器訪問http://192.168.1.202:8080/,對應的服務端口切記在iptables中已添加。效果圖如下:
觀察kubernetes在etcd中的數據存儲結構
觀察單個pods的數據存儲結構,以json的格式存儲。
二、實戰操作
任務:通過Kubernetes創建一個LNMP架構的服務集群,以及觀察其負載均衡,涉及鏡像“yorko/webserver”已經push至registry.hub.docker.com,大家可以通過“docker pull yorko/webserver”下載。
# mkdir -p /home/kubermange/replication && mkdir -p /home/kubermange/service
# cd /home/kubermange/replication
1. 創建一個replication,本例直接在replication模板中創建pod并復制,也可獨立創建pod再通過replication來復制。
【replication/lnmp-replication.json】
{
"id": "webserverController",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"labels": {"name": "webserver"},
"desiredState": {
"replicas": 2,
"replicaSelector": {"name": "webserver_pod"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "webserver",
"volumes": [
{"name":"httpconf", "source":{"hostDir":{"path":"/etc/httpd/conf"}}},
{"name":"httpconfd", "source":{"hostDir":{"path":"/etc/httpd/conf.d"}}},
{"name":"httproot", "source":{"hostDir":{"path":"/data"}}}
],
"containers": [{
"name": "webserver",
"image": "yorko/webserver",
"command": ["/bin/sh", "-c", "/usr/bin/supervisord -c /etc/supervisord.conf"],
"volumeMounts": [
{"name":"httpconf", "mountPath":"/etc/httpd/conf"},
{"name":"httpconfd", "mountPath":"/etc/httpd/conf.d"},
{"name":"httproot", "mountPath":"/data"}
],
"cpu": 100,
"memory": 50000000,
"ports": [{
"containerPort": 80,
},{
"containerPort": 22,
}]
}]
}
},
"labels": {"name": "webserver_pod"},
},
}
}
執行創建命令
#kubectl create -f lnmp-replication.json
觀察生成的pod副本清單:
[root@SN2014-12-200 replication]# kubectl get pod
NAME IMAGE(S) HOST LABELS STATUS
84150ab7-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.202/ name=webserver_pod Running
84154ed5-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.201/ name=webserver_pod Running
840beb1b-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.202/ name=webserver_pod Running
84152d93-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.202/ name=webserver_pod Running
840db120-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.201/ name=webserver_pod Running
8413b4f3-89f8-11e4-970d-000c292f1620 yorko/webserver 192.168.1.201/ name=webserver_pod Running
2.創建一個service,通過selector指定 "name": "webserver_pod"與pods關聯。
【service/lnmp-service.json】
{
"id": "webserver",
"kind": "Service",
"apiVersion": "v1beta1",
"selector": {
"name": "webserver_pod",
},
"protocol": "TCP",
"containerPort": 80,
"port": 8080
}
執行創建命令:
# kubectl create -f lnmp-service.json
登錄minion主機(192.168.1.201),查詢主宿機生成的iptables轉發規則(后一行)
# iptables -nvL -t nat
Chain KUBE-PROXY (2 references)
pkts bytes target prot opt in out source destination
2 120 REDIRECT tcp -- * * 0.0.0.0/0 10.254.102.162 /* kubernetes */ tcp dpt:443 redir ports 47700
1 60 REDIRECT tcp -- * * 0.0.0.0/0 10.254.28.74 /* kubernetes-ro */ tcp dpt:80 redir ports 60099
0 0 REDIRECT tcp -- * * 0.0.0.0/0 10.254.216.51 /* webserver */ tcp dpt:8080 redir ports 40689
訪問測試,http://192.168.1.201:40689/info.php,刷新瀏覽器發現proxy后端的變化,默認為隨機輪循算法。
三、測試過程
1.pods自動復制、銷毀測試,觀察Kubernetes自動保持副本數(6份)
刪除replicationcontrollers中一個副本fedoraapache
[root@SN2014-12-200 pods]# kubectl delete pods fedoraapache
I1219 23:59:39.305730 9516 restclient.go:133] Waiting for completion of operation 142530
fedoraapache
[root@SN2014-12-200 pods]# kubectl get pods
NAME IMAGE(S) HOST LABELS STATUS
5d70892e-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d715e56-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
5d717f8d-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
5d71c584-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d71a494-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
#自動生成出一個副本,保持6份的效果
[root@SN2014-12-200 pods]# kubectl get pods
NAME IMAGE(S) HOST LABELS STATUS
5d717f8d-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
5d71c584-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d71a494-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
2a8fb993-8798-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d70892e-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.201/ name=fedoraapache Running
5d715e56-8794-11e4-970d-000c292f1620 fedora/apache 192.168.1.202/ name=fedoraapache Running
2.測試不同角色模塊中的hostPort
1)pod中hostPort為空,而replicationcontrollers為指定端口,則異常;兩側都指定端口,相同或不同時都異常;pod的hostport為指定,另replicationcon為空,則正常;pod的hostport為空,另replicationcon為空,則正常;結論是在replicationcontrollers場景不能指定hostport,否則異常,待持續測試。
2)結論:在replicationcontronllers.json中,"replicaSelector": {"name": "webserver_pod"}要與"labels": {"name": "webserver_pod"}以及service中的"selector": {"name": "webserver_pod"}保持一致;
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。