1. 程式人生 > >使用Helm charts在Kubernetes 1.4 MongoDB上部署_Kubernetes中文社群

使用Helm charts在Kubernetes 1.4 MongoDB上部署_Kubernetes中文社群

有人說:“如果你覺得國慶7天短的話,那麼接下來上班的7天你就知道有多長了【二哈】”,今天週一,照列來一波乾貨來襲,喜歡來收。

2016年9月26日釋出Kubernetes 1.4版本,其中包括幾項新的功能。一個有趣的是使用擴充套件的狀態的應用支援 Helm Charts。在這篇文章中,我們將使用Kubernetes 1.4的這個新功能部署MongoDB例項來Kubernetes。

他們的部落格公告:

策劃和預測試Helm Charts普通狀態的應用,如MariaDB的,MySQL和詹金斯將可使用頭盔包管理器的版本2單命令啟動。

請記住,即使Helm只是官方Kubernetes的一部分,但是不要它的話法,將無法正常工作。如果你發現有任何問題,可以在GitHub上建立一個問題來需求解決。

什麼是 Helm?

Helm,是Kubernetes的軟體包管理器。Charts表示可以安裝並組成的預配置Kubernetes資源包。

配置 Helm

Helm採用客戶端機伺服器模式。伺服器部分被稱為tiller,同時包括你執行Kubernetes叢集。客戶端部分被稱為helm,安裝在本地的開發系統上。

安裝客戶端Helm

首先我們安裝需要的客戶端,以便我們能在Kubernetes群集上安裝helm。在helm的每個版本中大多數OS是二進位制檔案。去他們的GitHub庫kubernetes 或者Helm,並找到最新版本。在這篇文章撰寫時最新的版本是V2.0.0,alpha.4,所以我們將使用該版本。

注意:安裝helm的同時請確保你已經把kubectl安裝在相同環境下。這將使我們能夠從開發環境的群集上安裝helm。

執行下面的命令下載並解壓二進位制檔案:

export HELM_OS=linux && wget https://github.com/kubernetes/helm/releases/download/v2.0.0-alpha.4/helm-v2.0.0-alpha.4-$HELM_OS-amd64.tar.gz && tar -zxvf helm-v2.0.0-alpha.4-$HELM_OS-amd64.tar.gz && cd $HELM_OS-amd64

將二進位制放到有用的地方:

sudo mv linux-amd54/helm /usr/local/bin/helm

驗證是否安裝正確:

helm help

安裝 Helm server

現在,我們已經安裝了客戶端helm,我們可以用它在我們的Kubernetes群集上安裝helm。要安裝簡單helm執行以下命令:

$ helm init
Creating /home/stackadmin/.helm 
Creating /home/stackadmin/.helm/repository 
Creating /home/stackadmin/.helm/repository/cache 
Creating /home/stackadmin/.helm/repository/local 
Creating /home/stackadmin/.helm/repository/repositories.yaml 
Creating /home/stackadmin/.helm/repository/local/index.yaml 
$HELM_HOME has been configured at $HOME/.helm.

Tiller (the helm server side component) has been installed into your Kubernetes Cluster.
Happy Helming!

該命令完成後,您可以通過列出所有kube-system驗證它安裝helm:

$ kubectl get pods –namespace=kube-system
tiller-deploy-500364655-e3ldg           1/1       Running   0          1m

現在,我們可以驗證客戶端和伺服器部分被重新執行。我們應該看到這兩個部分中列出的版本:(實際上,它指出在這個從GitHub的helm note,但在實踐中使用命令,我只看到它顯示了客戶端版)

helm version
v2.0.0-alpha.4

準備 GCE

為了讓我們的Chart正常執行,我們需要得到一些底層架構。該圖表將建立3 個MongoDB的例項,每個都需要有一個持久Kubernetes Persistent Volume。由於我們這篇文章使用GCE的雲服務提供商,我們需要使用gcloud SDK第一設定3個 GCE永久磁碟。確保你在同一區域作為你kubernetes叢集建立的磁碟。

$ gcloud compute disks create pd-disk-1 pd-disk-2 pd-disk-3 --zone us-central1-b –size=10GB

WARNING: You have selected a disk size of under [200GB]. This may result in poor I/O performance. For more information, see: https://developers.google.com/compute/docs/disks#pdperformance.
Created [https://www.googleapis.com/compute/v1/projects/compact-market-142402/zones/us-central1-b/disks/pd-disk-1].
Created [https://www.googleapis.com/compute/v1/projects/compact-market-142402/zones/us-central1-b/disks/pd-disk-2].
Created [https://www.googleapis.com/compute/v1/projects/compact-market-142402/zones/us-central1-b/disks/pc-disk-3].
NAME       ZONE           SIZE_GB  TYPE         STATUS
pd-disk-1  us-central1-b  10       pd-standard  READY
pd-disk-2  us-central1-b  10       pd-standard  READY
pc-disk-3  us-central1-b  10       pd-standard  READY

New disks are unformatted. You must format and mount a disk before it
can be used. You can find instructions on how to do this at:

https://cloud.google.com/compute/docs/disks/add-persistent-disk#formatting

不要擔心警告,新的磁碟格式化,MongoDB會提醒我們。現在,我們有GCE PD的建立,我們需要建立相應的Kubernetes Persistent Volumes。建立一個名為GCE-pv.yaml的檔案,內容如下:

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0001
  annotations:
    volume.beta.kubernetes.io/storage-class: generic
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    fsType: ext4
    pdName: pd-disk-1
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0002
  annotations:
    volume.beta.kubernetes.io/storage-class: generic
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    fsType: ext4
    pdName: pd-disk-2
---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv0003
  annotations:
    volume.beta.kubernetes.io/storage-class: generic
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    fsType: ext4
    pdName: pd-disk-3

儲存檔案,然後使用kubectl來建立它們

$ kubectl create -f gce-pv.yaml

persistentvolume "pv0001" created
persistentvolume "pv0002" created
persistentvolume "pv0003" created

現在我們可以來安裝Chart!

Charts

Charts是描述軟體包的Kubernetes方式。chart基本上與描述瞭如何部署應用程式檔案的目錄。這與Puppet Modules非常相似,因為它們是描述應用程式程式碼的目錄。

檔案結構

頂層目錄的名稱是應用程式的名稱。structore概述如下:

mongodb/
  Chart.yaml
  LICENSE
  README.md
  values.yaml
  charts/
  templates/
  templates/NOTES.txt

該Chart.yaml和values.yaml檔案是唯一需要的檔案,包括有關chart資訊。

Chart.yaml

我不會把Chart.yaml檔案的詳細資訊弄的那麼繁雜。這種東西應該是幫助我們理解抽象的部署應用程式,就像MongoDB的那麼容易,因為用apt或yum的安裝。如果你真的想知道更多有關如何將檔案的組織知識,你可以自行閱讀相關知識。

MongoDB Chart

Incubator 狀態

Chart被認為是“_incubator status_’,這意味著它不符合下列條件之一是:

  • 提供了資料永續性的方法(如適用)
  • 支援應用升級
  • 允許應用程式配置的定製
  • 提供一個安全的預設配置
  • 不要利用Kubernetes alpha功能

MongoDB chart使用大量的Kubernetesalpha功能這使得它處於孵化器的狀態。

獲取Chart

第一步,通過用git clone到本地儲存庫以獲得圖表。

git clone https://github.com/kubernetes/charts.git

安裝 Chart

現在,可以用一個命令來安裝我們的chart。

helm install charts/incubator/mongodb/

reeling-indri
Last Deployed: Thu Oct  6 22:49:15 2016
Namespace: default
Status: DEPLOYED

Resources:
==> v1/Service
NAME                    CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE
reeling-indri-mongodb   None         <none>        27017/TCP   0s

==> apps/PetSet
NAME                    DESIRED   CURRENT   AGE
reeling-indri-mongodb   3         3         0s


Notes:
Getting Started:

1. After the petset is created completely, one can check which instance is primary by running:
    $ for i in `seq 0 2`; do kubectl exec  reeling-indri-mongodb-$i -- sh -c '/usr/bin/mongo --eval="printjson(rs.isMaster())"'; done.
    This assumes 3 replicas, 0 through 2. It should be modified to reflect the actual number of replicas specified.

2. One can insert a key into the primary instance of the mongodb replica set by running the following:
    $ kubectl exec MASTER_POD_NAME -- /usr/bin/mongo --eval="printjson(db.test.insert({key1: 'value1'}))"
    MASTER_POD_NAME must be replaced with the name of the master found from the previous step.

3. One can fetch the keys stored in the primary or any of the slave nodes in the following manner.
    $ kubectl exec POD_NAME -- /usr/bin/mongo --eval="rs.slaveOk(); db.test.find().forEach(printjson)"
    POD_NAME must be replaced by the name of the pod being queried.

這將在預設的名稱空間內為MongoDB建立Kubernetes服務和petset。從helm來看,先安裝命令是PetSet釋出的名稱。這很重要,因為這是我們以後的一切引用。幾分鐘後,檢查我們的pods的狀態。

kubectl get pods –namespace=default
NAME                      READY     STATUS    RESTARTS   AGE
reeling-indri-mongodb-0   1/1       Running   0          2m
reeling-indri-mongodb-1   1/1       Running   0          1m
reeling-indri-mongodb-2   1/1       Running   0          51s

現在,我們在Kubernetes叢集中有執行的的MongoDB叢集。最後,我們將學習如何訪問它,並驗證一切工作。

驗證 MongoDB 的執行

現在,我們的MongoDB在執行中,我們可以在上面執行一些命令來檢查MongoDB是否在真正執行。

export RELEASE_NAME=reeling-indri

現在執行以下命令來找出哪一個是主要的MongoDB pods。

$ for i in 0 1 2; do kubectl exec $RELEASE_NAME-mongodb-$i -- sh -c '/usr/bin/mongo --eval="printjson(rs.isMaster())"'; done
MongoDB shell version: 3.2.10
connecting to: test
{
	"hosts" : [
		"whopping-elk-mongodb-0.whopping-elk-mongodb.default.svc.cluster.local:27017"
	],
	"setName" : "rs0",
	"ismaster" : false,
	"secondary" : false,
	"info" : "Does not have a valid replica set config",
	"isreplicaset" : true,
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 1000,
	"localTime" : ISODate("2016-10-07T03:58:29.718Z"),
	"maxWireVersion" : 4,
	"minWireVersion" : 0,
	"ok" : 1
}
MongoDB shell version: 3.2.10
connecting to: test
{
	"hosts" : [
		"reeling-indri-mongodb-1.reeling-indri-mongodb.default.svc.cluster.local:27017"
	],
	"setName" : "rs0",
	"setVersion" : 1,
	"ismaster" : true,
	"secondary" : false,
	"primary" : "reeling-indri-mongodb-1.reeling-indri-mongodb.default.svc.cluster.local:27017",
	"me" : "reeling-indri-mongodb-1.reeling-indri-mongodb.default.svc.cluster.local:27017",
	"electionId" : ObjectId("7fffffff0000000000000001"),
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 1000,
	"localTime" : ISODate("2016-10-07T03:58:30.775Z"),
	"maxWireVersion" : 4,
	"minWireVersion" : 0,
	"ok" : 1
}
MongoDB shell version: 3.2.10
connecting to: test
{
	"hosts" : [
		"lanky-bronco-mongodb-0.lanky-bronco-mongodb.default.svc.cluster.local:27017"
	],
	"setName" : "rs0",
	"ismaster" : false,
	"secondary" : false,
	"info" : "Does not have a valid replica set config",
	"isreplicaset" : true,
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 1000,
	"localTime" : ISODate("2016-10-07T03:58:31.878Z"),
	"maxWireVersion" : 4,
	"minWireVersion" : 0,
	"ok" : 1

你可以從上面舉的例子中看到,第二個pod是主MongoDB例項。

解除安裝

如果你想MongoDB停止執行,你可以使用“uninstall”命令。

helm delete reeling-indri

結論

現在你有一個MongoDB的安裝配置好並存儲資料。我們用一個簡單方法安裝了它Kubernetes稱為Helm。由於這仍然是相當新的技術,所以這篇文章只有幾個chart可用,但我相信在不久的將來會有很多。