Kubernetes基本要素介紹(下)

bullet045 8年前發布 | 20K 次閱讀 Kubernetes

【編者的話】Rimas Mocevicius是云計算、 CoreOS 、 Kubernetes 、DEIS和應用程序容器解決方案架構師 。CoreOS Essentials一書作者。目前就職于DEIS。本文是其發表在DEIS上的官方博客,介紹Kubernetes的系列教程的第二篇。

在我的上一篇博客中,我介紹了kubectl,控制面板,命名空間,Pod,服務,復制管理器和標簽的概念。

這篇博客中,我們繼續介紹卷,Secret,滾動升級和Helm。

一個卷就是包含一些數據的目錄,它可以被容器作為文件系統的組成部分而使用。卷通常作為存儲有狀態的應用數據而使用。

Kubernetes支持以下卷類型

  • emptyDir
  • hostPath
  • gcePersistentDisk
  • awsElasticBlockStore
  • nfs
  • iscsi
  • glusterfs
  • rbd
  • gitRepo
  • secret
  • persistentVolumeClaim

具體細節請參閱 文檔

在本篇博客中,我們將只涉及其中的一種卷類型:hostPath。

hostPath卷從主機節點的文件系統中掛載一個文件或目錄到Pod中。舉例來說,上一篇博客中我們介紹的nginx復制控制器可以使用此種類型卷去存儲HTML文件。

讓我們使用卷配置去更新 nginxrc.yaml :

apiVersion: v1

kind: ReplicationController

metadata:

name: my-nginx

spec:

replicas: 1

template:

metadata:

  labels:

    app: nginx

spec:

  containers:

  - name: nginx

    image: nginx

    ports:

    - containerPort: 80

  # Mount volume into pod

    volumeMounts:

      # must match the name of the volume name below

      - name: shared-html

        # mount path within the container

        mountPath: /usr/share/nginx/html

# volumes to be mounted to containers

  volumes:

  - name: shared-html

    hostPath:

      path: /somepath/shared/html

使用這種復制控制器配置, nginx 容器將從主機掛載的卷來提供HTML文件的訪問。

如果你改變副本數量這可能是無效的,因為 /some/shared/html 目錄可能不存在于其他節點中。如果你想進行擴展,你需要在Kubernetes節點之間復制這些文件,讓他們在所有節點中都是可訪問的。或者你可以使用NFS或者GlusterFS的卷類型。

Secret

Secret可能是任何一段敏感的數據,錄入授權令牌,加密鑰匙,SSL證書,SSH鑰匙等。Secret可以被容器訪問。將此種信息放在Secret中比放在Pod定義或者Docker鏡像中要更安全并更靈活。

想要使用Secret,Pod或者復制控制器需要引用Secret。

下面是一份Secret主文件的例子:

apiVersion: v1

kind: Secret

metadata:

name: mysecrets

data:

password: dmFsdWUtMg0K

username: someuser

創建文件的命令:

$ kubectl create -f mysecrecs.yaml

我們可以將它作為一個卷掛載到容器中:

...

    volumeMounts:

      # must match the name of the volume name below

      - name: my-passwords

     readOnly: true

        # mount path within the container

        mountPath: /etc/my-passwords-volume

# volumes to be mounted to containers

  volumes:

  - name: my-passwords

    secrect:

      secrectName: mysecrects

我們的應用現在可以從 /etc/my-passwords-volume 目錄中讀取 username 和 password 文件。其中包含了你在Secret主文件中定義的值。非常簡單。

如果你的應用需要環境變量,一個方法是將環境變量寫入這些文件中,然后在應用啟動之前讀取它們并設置為環境變量。

想知道更多關于Secret的內容,請參閱 文檔

滾動升級

在上一篇博客中,我們了解了如果通過復制管理器來發布Pod。這篇文章中,我們將知道如果做 滾動升級 。舉例來說,這可以實現更新Docker鏡像的版本。

以下是你要做的:

$ kubectl rolling-update my-nginx --image=nginx:1.9.10

Created my-nginx-2088ed66ed908b2434289b30bf3dafe3

Scaling up my-nginx-2088ed66ed908b2434289b30bf3dafe3 from 0 to 3, scalling down

Scaling my-nginx-2088ed66ed908b2434289b30bf3dafe3 up to 1

Scaling my-nginx down to 2

Scaling my-nginx-2088ed66ed908b2434289b30bf3dafe3 up to 2

Scaling my-nginx down to 1

Scaling my-nginx-2088ed66ed908b2434289b30bf3dafe3 up to 3

Scaling my-nginx down to 0

Update succeeded. Deleting old controller: my-nginx

Renaming my-nginx-2088ed66ed908b2434289b30bf3dafe3 to my-nginx

replicationcontroller "my-nginx" rolling updated

我們剛剛通過升級Docker鏡像來升級 my-nginx 復制控制器管理的Pod。換句話說,這個過程先刪除了老容器,并使用最新的鏡像來創建一個新容器。

這只適用于每個復制管理器管理一種Docker鏡像的情況。如果你的復制管理器管理多種Dokcer鏡像,一個新的復制管理器將被創建。

下面是例子:

$ kubectl rolling-update my-nginx -f nginxrc-v2.yaml

以上的命令通過使用在 nginxrc-v2.yaml 主文件中定義的新復制控制器去升級 my-nginx 的Pod。當新的復制控制器生效后,老的將會被刪除。

你能在文檔中讀到更多關于 簡單滾動升級 或者 復制管理器滾動升級 的內容。

Helm

Helm 是Kubernetes的包管理器。

我們已經學習了在Kubernetes集群上安裝復制管理器和服務。你可以自己編寫控制器配置文件,或者也可以用已經預寫好的配置文件,這些文件應該是由你進行測試。Helm就是為了后一種場景服務的。

Helm允許你安裝 chart 。一個chart對于一個Kubernetes的主文件。這些chart可以被Helm社區共享。

讓我們安裝Helm:

$ mkdir ~/bin

$ cd ~/bin

$ curl -s https://get.helm.sh | bash

你可以查看可用的命令:

helm -h

使用Helm的第一步是從Github上獲取最新的chart:

$ helm update

---> Creating /home/user/.helm/config.yaml

---> Checking repository charts

---> Cloning into '/home/user/.helm/cache/charts'...Already up-to-date.

---> Done

讓我們搜索某一個chart:

$ helm search weavescope

weavescope - Weave Scope chart

Weave Scope提供了一個你的Docker容器的可視化地圖。可以在 官方產品頁 中獲取更多信息。

我們可以更詳細的了解chart中的信息:

$ helm info weavescope

Name: weavescope

Home: http://weave.works/product/scope/

Version: 0.1.1

Description: Weave Scope chart

Details: This chart allows to run Weave Scope on Kubernetes

并且我們可以拉取這些chart到我們的工作區中:

$ helm fetch weavescope

---> Fetched chart into workspace /home/user/.helm/workspace/charts/weavescope

---> Done

Helm總是保存chart的一份拷貝到 ~/.helm/workspace/charts/目錄中 。

所以讓我們安裝這個Weave Scope chart:

$ helm install weavescope

---> Running `kubectl create -f` ...

service "weave-scope-app" created

replicationcontroller "weave-scope-app" created

daemonset "weave-scope-probe" created

---> Done

# Weave Scope

Using Weave Scope with Kubernetes:

https://github.com/weaveworks/scope#using-weave-scope-with-kubernetes

If you have a scope.weave.works account and want to run Scope in Cloud

Service Mode, comment out the line

"$(WEAVE_SCOPE_APP_SERVICE_HOST):$(WEAVE_SCOPE_APP_SERVICE_PORT)"] in

scope-probe-ds.yaml manifest and uncomment the line below, replacing

"foo" with the Service token which you will obtain when logging in to

your scope.weave.works account: "--probe.token=foo"]

DaemonSets need to be enabled in Kubernetes cluster and all node hosts

are run with the Docker socket at /var/run/docker.sock

========================================

讓我們檢查復制控制器的工作情況:

$ kubectl get rc weave-scope-app

NAME              DESIRED   CURRENT   AGE

weave-scope-app   1         1         8m

完美。現在我們應該檢查守護進程集的工作情況了:

$ kubectl get ds weave-scope-probe

NAME                DESIRED   CURRENT   NODE-SELECTOR   AGE

weave-scope-probe   3         3         <none>          10m

Weave Scope監聽4040端口,但是該個端口并沒有暴露給我們。所以讓我們建立本地端口到遠程端口的跳轉,這樣我們就可以從自己的筆記本電腦上遠程訪問它了。

獲取Pod列表:

$ kubectl get pods

NAME                          READY     STATUS    RESTARTS   AGE

weave-scope-app-k8m4k         1/1       Running   0          23m

weave-scope-probe-b5jh7       1/1       Running   0          23m

weave-scope-probe-psit2       1/1       Running   0          23m

我們感興趣的是 weave-scope-app Pod。

我們能像這樣增加一個端口跳轉:

$ kubectl port-forward weave-scope-app-k8m4k 4040:4040

I0428 18:43:37.014631   14389 portforward.go:213] Forwarding from 127.0.0.1:4040 -> 4040

I0428 18:43:37.015420   14389 portforward.go:213] Forwarding from [::1]:4040 -> 4040

現在我們開以用瀏覽器打開 http://127.0.0.1:4040 。

你可以看到WeaveScope的界面:

太棒了。所有的東西都能正常工作了!

如果你過后想要卸載Weave Scope,你可以這樣做:

$ helm uninstall weavescope -n default

總結

在Kubernetes微教程的第二部分我們介紹了卷,secret,滾動更新和Helm。Helm是Kubernetes的包管理器,并使安裝和管理Kubernetes主文件更加容易。

===========================================譯者介紹,當當網架構師,開源數據庫分庫分表中間件作者。目前從事Docker相關調研工作。

 

來自: http://dockone.io/article/1281

 本文由用戶 bullet045 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!