Docker之~集群配置

一、前言

Kubernetes 是Google開源的容器集群管理系統,基于Docker構建一個容器的調度服務,提供資源調度、均衡容災、服務注冊、動態擴縮容等功能套件,目前最新版本為0.6.2。

本文介紹如何基于Centos7.0構建Kubernetes平臺,在正式介紹之前,大家有必要先理解Kubernetes幾個核心概念及其承擔的功能。以下為Kubernetes的架構設計圖:


  
Docker之~集群配置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、平臺版本說明

1)Centos7.0 OS
2)Kubernetes V0.6.2
3)etcd version 0.4.6
4)Docker version 1.3.2

2、平臺環境說明
Docker之~集群配置

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:

# 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】

view plainprint?
# 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=""  
view plainprint?
# 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】

 

view plainprint?
# 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】

view plainprint?
# 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】

view plainprint?
###  
# 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】

view plainprint?
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

view plainprint?
{  
  "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中的數據存儲結構
Docker之~集群配置 
觀察單個pods的數據存儲結構,以json的格式存儲。
  Docker之~集群配置
  

二、實戰操作

任務:通過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】

view plainprint?
{  
  "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】

view plainprint?
{  
  "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后端的變化,默認為隨機輪循算法。
Docker之~集群配置
Docker之~集群配置
 
  

三、測試過程

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

  

 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”}保持一致;

原創文章,作者:kang,如若轉載,請注明出處:http://www.www58058.com/79142

(2)
kangkang
上一篇 2017-03-16
下一篇 2017-03-17

相關推薦

  • 初探linux

    計算機的組成及其功能: 存儲器:    實現記憶功能的部件用來存放計算程序及參與運算的各種數據 運算器:    負責數據的算術運算和邏輯運算即數據的加工處理 控制器:    負責對程序規定的控制信息進行分析,控制并協調輸入,輸出操作或內存訪問 輸入設備:    實現計算程序和原始…

    Linux干貨 2016-10-29
  • Linux正則表達式及文件查找

    1、顯示當前系統上root、fedora或者user1用戶的默認shell. #? ?grep? -E? ?“^(root|fedora|user1)”? ?/etc/passwd | cut -d: -f1,7     2、找出/etc/rc.d/init.d/functions文件中某單詞后面跟一組小括號的行,…

    2017-10-22
  • http狀態碼大全

    狀態值:100 客戶端應當繼續發送請求。這個臨時響應是用來通知客戶端它的部分請求已經被服務器接收,且仍未被拒絕??蛻舳藨斃^續發送請求的剩余部分,或者如果請求已經完成,忽略這個響應。服務器必須在請求完成后向客戶端發送一個最終響應。 狀態值:101 服務器已經理解了客戶端的請求,并將通過Upgrade 消息頭通知客戶端采用不同的協議來完成這個請求。在發送完這個…

    Linux干貨 2016-11-01
  • HTTP詳解(2)-請求、響應、緩存

    1. HTTP請求格式              做過Socket編程的人都知道,當我們設計一個通信協議時,“消息頭/消息體”的分割方式是很常用的,消息頭告訴對方這個消息是干什么的,消息體告訴對方怎么干。HTTP協議傳輸的消息也是這樣規定的…

    Linux干貨 2015-04-04
  • 今天正式加入馬幫開啟我的學習Linux之路

    3月26日馬哥教育30期開學儀式,終于見到了我們的馬哥馬永亮先生,同時馬哥還有前大眾點評架構師張Sir以及國內首批通過紅帽授權認證講師(RHCI)的老王給我們新生做了開學演講。接著我們的宗華老師以及云珍老師對我們平時的學習以及生活做出了各種介紹與規定,還有我們同學之間也進行了相互自我介紹彼此熟悉….

    2018-03-26
  • 循環的特殊用法及函數

    while特殊用法 while read 變量名;do 循環體 done<文件路徑(將文件中的每一行依次讀入循環體,賦值給變量)   (())可以實現C語言風格的變量操作 for循環特殊格式 for((控制變量初始化;條件判斷表達式;控制變量修正表達式)) do 循環體 done 控制變量初始化僅在循環開始時執行一次,進行條件判斷成立后執行循…

    Linux干貨 2016-08-21
欧美性久久久久