Kubernetes 实战教学,手把手教您如何在 K8s 平台上使用 Compose(二)

简介: 今天将展示如何在 Kubernetes 上运用 Compose 的实战演示。

screenshot

出品丨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

screenshot

]? > 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 状态

? > 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 集群上。

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
26天前
|
存储 运维 Kubernetes
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
飞轮科技推出了 Doris 的 Kubernetes Operator 开源项目(简称:Doris Operator),并捐赠给 Apache 基金会。该工具集成了原生 Kubernetes 资源的复杂管理能力,并融合了 Doris 组件间的分布式协同、用户集群形态的按需定制等经验,为用户提供了一个更简洁、高效、易用的容器化部署方案。
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
|
2月前
|
存储 Kubernetes Docker
Kubernetes(k8s)和Docker Compose本质区别
理解它们的区别和各自的优势,有助于选择合适的工具来满足特定的项目需求。
194 19
|
2月前
|
关系型数据库 MySQL Docker
《docker高级篇(大厂进阶):5.Docker-compose容器编排》包括是什么能干嘛去哪下、Compose核心概念、Compose使用三个步骤、Compose常用命令、Compose编排微服务
《docker高级篇(大厂进阶):5.Docker-compose容器编排》包括是什么能干嘛去哪下、Compose核心概念、Compose使用三个步骤、Compose常用命令、Compose编排微服务
137 24
|
2月前
|
Kubernetes 应用服务中间件 nginx
二进制安装Kubernetes(k8s)v1.32.0
本指南提供了一个详细的步骤,用于在Linux系统上通过二进制文件安装Kubernetes(k8s)v1.32.0,支持IPv4+IPv6双栈。具体步骤包括环境准备、系统配置、组件安装和配置等。
540 10
|
1月前
|
缓存 容灾 网络协议
ACK One多集群网关:实现高效容灾方案
ACK One多集群网关可以帮助您快速构建同城跨AZ多活容灾系统、混合云同城跨AZ多活容灾系统,以及异地容灾系统。
|
2月前
|
Kubernetes Ubuntu 网络安全
ubuntu使用kubeadm搭建k8s集群
通过以上步骤,您可以在 Ubuntu 系统上使用 kubeadm 成功搭建一个 Kubernetes 集群。本文详细介绍了从环境准备、安装 Kubernetes 组件、初始化集群到管理和使用集群的完整过程,希望对您有所帮助。在实际应用中,您可以根据具体需求调整配置,进一步优化集群性能和安全性。
131 12
|
2月前
|
Prometheus Kubernetes 监控
OpenAI故障复盘 - 阿里云容器服务与可观测产品如何保障大规模K8s集群稳定性
聚焦近日OpenAI的大规模K8s集群故障,介绍阿里云容器服务与可观测团队在大规模K8s场景下我们的建设与沉淀。以及分享对类似故障问题的应对方案:包括在K8s和Prometheus的高可用架构设计方面、事前事后的稳定性保障体系方面。
|
2月前
|
Kubernetes 网络协议 应用服务中间件
Kubernetes Ingress:灵活的集群外部网络访问的利器
《Kubernetes Ingress:集群外部访问的利器-打造灵活的集群网络》介绍了如何通过Ingress实现Kubernetes集群的外部访问。前提条件是已拥有Kubernetes集群并安装了kubectl工具。文章详细讲解了Ingress的基本组成(Ingress Controller和资源对象),选择合适的版本,以及具体的安装步骤,如下载配置文件、部署Nginx Ingress Controller等。此外,还提供了常见问题的解决方案,例如镜像下载失败的应对措施。最后,通过部署示例应用展示了Ingress的实际使用方法。
80 2
|
2月前
|
存储 Kubernetes 关系型数据库
阿里云ACK备份中心,K8s集群业务应用数据的一站式灾备方案
本文源自2024云栖大会苏雅诗的演讲,探讨了K8s集群业务为何需要灾备及其重要性。文中强调了集群与业务高可用配置对稳定性的重要性,并指出人为误操作等风险,建议实施周期性和特定情况下的灾备措施。针对容器化业务,提出了灾备的新特性与需求,包括工作负载为核心、云资源信息的备份,以及有状态应用的数据保护。介绍了ACK推出的备份中心解决方案,支持命名空间、标签、资源类型等维度的备份,并具备存储卷数据保护功能,能够满足GitOps流程企业的特定需求。此外,还详细描述了备份中心的使用流程、控制台展示、灾备难点及解决方案等内容,展示了备份中心如何有效应对K8s集群资源和存储卷数据的灾备挑战。
|
3月前
|
Kubernetes 监控 Cloud Native
Kubernetes集群的高可用性与伸缩性实践
Kubernetes集群的高可用性与伸缩性实践
96 1