天天看點

Kubernetes 實戰教學,手把手教您如何在 K8s 平台上使用 Compose(二)

Kubernetes 實戰教學,手把手教您如何在 K8s 平台上使用 Compose(二)

出品丨Docker公司(ID:docker-cn)

編譯丨小東

每周一、三、五,與您不見不散!

用過 Kubernetes 的使用者都知道 Kubernetes API 真的非常龐大。在最新的版本中,從 Pods 和 Deployments 到 Validating Webhook Configuration 和 ResourceQuota,超過 50 個一級對象。如果您是開發人員,我确信這會很容易導緻群集配置時出現紊亂。是以,需要一種簡化的方法(如 Swarm CLI / API)來部署和管理在 Kubernetes 叢集上運作的應用程式。在上一篇文章

《Kubernetes 實戰教學,手把手教您如何在 K8s 平台上使用 Compose(一)》

中,簡要的介紹了簡化 Kubernetes 部署以及管理的工具 —— Compose,今天将展示如何在 Kubernetes 上運用 Compose 的實戰示範。

測試所用的基礎設施

  • Docker 版本:Docker Desktop Community v2.0.1.0;
  • 系統:macOS High Sierra v10.13.6;
  • Docker Engine:v18.09.1;
  • Docker Compose:v1.23.2;
  • Kubernetes:v1.13.0;

先決條件

驗證 Docker Desktop 版本

[Captains-Bay]? > docker version

Client: Docker Engine - Community

 Version:          18.09.1

 API version:      1.39

 Go version:       go1.10.6

 Git commit:       4c52b90

 Built:            Wed Jan 9 19:33:12 2019

 OS/Arch:          darwin/amd64

 Experimental:     false

 

Server: Docker Engine - Community

 Engine:

 Version:         18.09.1

 API version:     1.39 (minimum version 1.12)

 Go version:      go1.10.6

 Git commit:      4c52b90

 Built:           Wed Jan 9 19:41:49 2019

 OS/Arch:         linux/amd64

 Experimental:    true

 Kubernetes:

 Version:         v1.12.4

 StackAPI:        v1beta2

[Captains-Bay]? >           

安裝 Minikube

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 \
 && chmod +x minikube
           

驗證 Minikube 版本

minikube version
minikube version: v0.32.0
           

檢查 Minikube 狀态

minikube status
host: Stopped
kubelet:
apiserver:
           

開始 Minikube

Kubernetes 實戰教學,手把手教您如何在 K8s 平台上使用 Compose(二)
]? > minikube start
Starting local Kubernetes v1.12.4 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Stopping extra container runtimes...
Machine exists, restarting cluster components...
Verifying kubelet health ...
Verifying apiserver health ....Kubectl is now configured to use the cluster.
Loading cached images from config file.
 
Everything looks great. Please enjoy minikube!
 
By now, you should be able to see context switching happening under UI windows under Kubernetes section as shown below:
           

? > minikube status

host: Running

kubelet: Running

apiserver: Running

kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100[Captains-Bay]? >           

列出 Minikube 叢集節點

kubectl get nodes
NAME      STATUS   ROLES    AGE      VERSION
minikube  Ready    master   12h      v1.12.4
           

建立“compose”命名空間

kubectl create namespace compose
namespace "compose" created
           

建立“tiller”服務帳戶

kubectl -n kube-system create serviceaccount tiller

serviceaccount "tiller" created
           

授予對叢集的通路權限

kubectl -n kube-system create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount kube-system:tiller
clusterrolebinding "tiller" created
           

初始化 Helm 元件

? > helm init --service-account tiller

$HELM_HOME has been configured at /Users/ajeetraina/.helm.

 

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.

 

Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.

For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation

Happy Helming!
           

驗證 Helm 版本

helm version
Client: &version.Version{SemVer:"v2.12.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.12.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
           

部署 etcd operator

? > helm install --name etcd-operator stable/etcd-operator --namespace compose
NAME:  etcd-operator
LAST DEPLOYED: Fri Jan 11 10:08:06 2019
NAMESPACE: compose
STATUS: DEPLOYED
 
RESOURCES:
==> v1/ServiceAccount
NAME                                              SECRETS AGE
etcd-operator-etcd-operator-etcd-backup-operator  1       1s
etcd-operator-etcd-operator-etcd-operator         1       1s
etcd-operator-etcd-operator-etcd-restore-operator 1       1s
 
==> v1beta1/ClusterRole
NAME                                      AGE
etcd-operator-etcd-operator-etcd-operator 1s
 
==> v1beta1/ClusterRoleBinding
NAME                                              AGE
etcd-operator-etcd-operator-etcd-backup-operator  1s
etcd-operator-etcd-operator-etcd-operator         1s
etcd-operator-etcd-operator-etcd-restore-operator 1s
 
==> v1/Service
NAME                  TYPE      CLUSTER-IP     EXTERNAL-IP PORT(S)   AGE
etcd-restore-operator ClusterIP 10.104.102.245       19999/TCP 1s
 
==> v1beta1/Deployment
NAME                                              DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
etcd-operator-etcd-operator-etcd-backup-operator  1       1       1          0         1s
etcd-operator-etcd-operator-etcd-operator         1       1       1          0         1s
etcd-operator-etcd-operator-etcd-restore-operator 1       1       1          0         1s
 
==> v1/Pod(related)
NAME                                                            READY STATUS            RESTARTS AGE
etcd-operator-etcd-operator-etcd-backup-operator-7978f8bc4r97s7 0/1   ContainerCreating 0        1s
etcd-operator-etcd-operator-etcd-operator-6c57fff9d5-kdd7d      0/1   ContainerCreating 0        1s
etcd-operator-etcd-operator-etcd-restore-operator-6d787599vg4rb 0/1   ContainerCreating 0        1s
 
NOTES:
1. etcd-operator deployed.
 If you would like to deploy an etcd-cluster set cluster.enabled to true in values.yaml
 Check the etcd-operator logs
   export POD=$(kubectl get pods -l app=etcd-operator-etcd-operator-etcd-operator --namespace compose --output name)
   kubectl logs $POD --namespace=compose
? >           

建立 etcd 叢集

$cat compose-etcd.yaml
apiVersion: "etcd.database.coreos.com/v1beta2"
kind: "EtcdCluster"
metadata:
 name: "compose-etcd"
 namespace: "compose"
spec:
 size: 3
 version: "3.2.13"
kubectl apply -f compose-etcd.yaml
etcdcluster "compose-etcd" created
           

這将在 compose 命名空間中引入一個 etcd 叢集。

下載下傳 Compose 安裝程式

wget https://github.com/docker/compose-on-kubernetes/releases/download/v0.4.18/installer-darwin
           

在 Kubernetes 上部署 Compose

./installer-darwin -namespace=compose -etcd-servers=http://compose-etcd-client:2379 -tag=v0.4.18
INFO[0000] Checking installation state
INFO[0000] Install image with tag "v0.4.18" in namespace "compose"
INFO[0000] Api server: image: "docker/kube-compose-api-server:v0.4.18", pullPolicy: "Always"
INFO[0000] Controller: image: "docker/kube-compose-controller:v0.4.18", pullPolicy: "Always"
           

確定已啟用 Compose Stack 控制器

[Captains-Bay]? > kubectl api-versions| grep compose
compose.docker.com/v1beta1
compose.docker.com/v1beta2
           

列出 Minikube 的服務

[Captains-Bay]? > minikube service list

|-------------|-------------------------------------|-----------------------------|

| NAMESPACE |               NAME                |            URL            |

|-------------|-------------------------------------|-----------------------------|

| compose    | compose-api                        | No node port               |

| compose    | compose-etcd-client                | No node port                             |

| compose    | etcd-restore-operator              | No node port               |

| default    | db1                                | No node port               |

| default    | example-etcd-cluster-client-service | http://192.168.99.100:32379 |

| default    | kubernetes                         | No node port               |

| default    | web1                               | No node port               |

| default    | web1-published                     | http://192.168.99.100:32511 |

| kube-system | kube-dns                           | No node port               |

| kube-system | kubernetes-dashboard               | No node port               |

| kube-system | tiller-deploy                      | No node port               |

|-------------|-------------------------------------|-----------------------------|

[Captains-Bay]? >           

驗證 StackAPI

[Captains-Bay]? > docker version

Client: Docker Engine - Community

 Version:          18.09.1

 API version:      1.39

 Go version:       go1.10.6

 Git commit:       4c52b90

 Built:            Wed Jan 9 19:33:12 2019

 OS/Arch:          darwin/amd64

 Experimental:     false

 

Server: Docker Engine - Community

 Engine:

 Version:         18.09.1

 API version:     1.39 (minimum version 1.12)

 Go version:      go1.10.6

 Git commit:      4c52b90

 Built:           Wed Jan 9 19:41:49 2019

 OS/Arch:         linux/amd64

 Experimental:    true

 Kubernetes:

 Version:         v1.12.4

 StackAPI:        v1beta2

[Captains-Bay]? >
           

直接使用 Docker Compose 部署 Web 應用程式堆棧

[Captains-Bay]? > docker stack deploy -c docker-compose2.yml myapp4

Waiting for the stack to be stable and running...

db1: Ready         [pod status: 1/2 ready, 1/2 pending, 0/2 failed]

web1: Ready       [pod status: 2/2 ready, 0/2 pending, 0/2 failed]

 

Stack myapp4 is stable and running

 

[Captains-Bay]? > docker stack ls

NAME               SERVICES           ORCHESTRATOR       NAMESPACE

myapp4             2                  Kubernetes         default

[Captains-Bay]? > kubectl get po

NAME                   READY    STATUS   RESTARTS  AGE

db1-55959c855d-jwh69   1/1      Running  0         57s

db1-55959c855d-kbcm4   1/1      Running  0         57s

web1-58cc9c58c7-sgsld  1/1      Running  0         57s

web1-58cc9c58c7-tvlhc  1/1      Running  0         57s
           

是以,我們使用 Docker Compose 檔案成功地将一個 Web 應用程式棧部署到運作在 Minikube 中的單節點 Kubernetes 叢集上。

繼續閱讀