Kubernetes 实战教学,手把手教您用2个脚本在 PWK 上启用 Compose

简介: 在 Dockercon 的第二天,Docker 在 Kubernetes 项目上开源了 Compose。这个工具无疑可以简化 Kubernetes。

screenshot

出品丨Docker公司(ID:docker-cn)
编译丨小东
每周一、三、五,与您不见不散!


在 Dockercon 的第二天,Docker 在 Kubernetes 项目上开源了 Compose。这个工具无疑可以简化 Kubernetes。如果您不知道,Docker 企业版已经在 Compose File 3.3 版本中启用了这个功能,它可以让您使用相同的 docker-compose.yml 文件进行 Swarm 部署,也可以在部署应用栈时指定 Kubernetes 工作负载。


screenshot

两周前,我注意到社区中有人要求在 Kubernetes Playground 上运行 Compose on Kubernetes。出于我的兴趣,我开始研究如何简化解决方案,以便任何人都能在短时间内轻松地设置它。我创建了镜像仓库,并开始构建一个简单的脚本和 Makefile 来启动它并在 PWK 上运行。点击下列文章标题,查看详情:

在这篇博文中,我将向您演示如何使用2个脚本就可以在 Play with Kubernetes 平台上启用 Compose on Kubernetes。接下来,让我们开始吧!


首先,浏览 https://labs.play-with-k8s.com/ 访问Kubernetes Playground。

screenshot

单击“Login”按钮,并使用 Docker Hub 或 GitHub ID 进行身份验证。

screenshot

一旦开始会话,您将拥有一个自己的实验室环境。


添加第一个 Kubernetes 节点

单击左侧的“Add New Instance” 来建您的第一个 Kubernetes 集群节点。它会自动将其命名为“node1”。每个实例都预装了Docker Community Edition(CE)和Kubeadm。该节点将被视为我们群集的主节点。

screenshot


引导主节点

克隆镜像仓库并在第一个实例上运行此脚本:

git clone https://github.com/collabnix/compose-on-kubernetes
cd compose-on-kubernetes/scripts/pwk/
sh bootstrap-pwk.sh

当您执行此脚本时,作为初始化的一部分,kubeadm 会编写所需的几个配置文件,设置 RBAC 并部署 Kubernetes 控制平面组件(如 kube-apiserver、kube-dns、kube-proxy 和 etcd 等)。控制平面组件部署为 Docker 容器。

screenshot

复制上面的 kubeadm 连接令牌命令并将其保存,以供下一步使用。此命令将用于将其他节点加入群集。


添加工作节点

screenshot

点击“Add New Node”来添加一个新的工作节点。


检查集群状态

[node1 ~]$ kubectl get nodes
NAME     STATUS    ROLES    AGE      VERSION
node1    Ready     master   18m      v1.11.3
node2    Ready        4m       v1.11.3
node3    Ready        39s      v1.11.3
node4    NotReady     22s      v1.11.3
node5    NotReady     4s       v1.11.3
[node1 ]$ kubectl get po
No resources found.
[node1 ]$ kubectl get svc
NAME        TYPE       CLUSTER-IP  EXTERNAL-IP  PORT(S)  AGE
kubernetes  ClusterIP  10.96.0.1          443/TCP  1h

执行以下脚本,一次性设置好 Compose Namespace、etcd cluster 和 Compose controller:

chmod +x prepare-pwk.sh
sh prepare-pwk.sh
[node1 pwk]$ sh prepare-pwk.sh
Creating Compose Namespace...
namespace/compose created
Installing Helm...
 % Total   % Received % Xferd Average Speed  Time   Time    Time Current
                                Dload Upload  Total  Spent   Left Speed
100 21.6M 100 21.6M   0    0 25.3M     0 --:--:-- --:--:-- --:--:-- 25.4M
Preparing Helm
linux-amd64/
linux-amd64/tiller
linux-amd64/helm
linux-amd64/LICENSE
linux-amd64/README.md
Creating tiller under kube-system namespace...
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller-cluster-rule created
Creating /root/.helm
Creating /root/.helm/repository
Creating /root/.helm/repository/cache
Creating /root/.helm/repository/local
Creating /root/.helm/plugins
Creating /root/.helm/starters
Creating /root/.helm/cache/archive
Creating /root/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /root/.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.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
Happy Helming!
 Tiller is still coming up...Please Wait
NAME                            READY    STATUS   RESTARTS  AGE
coredns-78fcdf6894-699fx        1/1      Running  0         5m
coredns-78fcdf6894-trslx        1/1      Running  0         5m
etcd-node1                      1/1      Running  0         4m
kube-apiserver-node1            1/1      Running  0         4m
kube-controller-manager-node1   1/1      Running  0         4m
kube-proxy-5gskp                1/1      Running  0         4m
kube-proxy-5hbkb                1/1      Running  0         5m
kube-proxy-lcsnz                1/1      Running  0         4m
kube-scheduler-node1            1/1      Running  0         4m
tiller-deploy-85744d9bfb-bjw2f  0/1      Running  0         15s
weave-net-9vt2s                 2/2      Running  1         4m
weave-net-k87d7                 2/2      Running  0         5m
weave-net-nmmt5                 2/2      Running  0         4m
NAME:  etcd-operator
LAST DEPLOYED: Mon Jan 21 14:27:50 2019
NAMESPACE: compose
STATUS: DEPLOYED
 
RESOURCES:
==> v1/ServiceAccount
NAME                                              SECRETS AGE
etcd-operator-etcd-operator-etcd-backup-operator  1       4s
etcd-operator-etcd-operator-etcd-operator         1       4s
etcd-operator-etcd-operator-etcd-restore-operator 1       4s
 
==> v1beta1/ClusterRole
NAME                                      AGE
etcd-operator-etcd-operator-etcd-operator 4s
 
==> v1beta1/ClusterRoleBinding
NAME                                              AGE
etcd-operator-etcd-operator-etcd-backup-operator  4s
etcd-operator-etcd-operator-etcd-operator         3s
etcd-operator-etcd-operator-etcd-restore-operator 3s
 
==> v1/Service
NAME                  TYPE      CLUSTER-IP   EXTERNAL-IP PORT(S)   AGE
etcd-restore-operator ClusterIP 10.108.89.92       19999/TCP 3s
 
==> v1beta2/Deployment
NAME                                              DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
etcd-operator-etcd-operator-etcd-backup-operator  1       1       1          0         3s
etcd-operator-etcd-operator-etcd-operator         1       1       1          0         3s
etcd-operator-etcd-operator-etcd-restore-operator 1       1       1          0         3s
 
==> v1/Pod(related)
NAME                                                            READY STATUS            RESTARTS AGE
etcd-operator-etcd-operator-etcd-backup-operator-56fd448cd897mk 0/1   ContainerCreating 0     2s
etcd-operator-etcd-operator-etcd-operator-c5b8b8f74-pttr2       0/1   ContainerCreating 0     2s
etcd-operator-etcd-operator-etcd-restore-operator-58587cdc9g4br 0/1   ContainerCreating 0     2s
 
 
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 --namespacecompose --output name)
   kubectl logs $POD --namespace=compose
Loaded plugins: fastestmirror, ovl
base                                                                   | 3.6 kB 00:00:00
docker-ce-stable                                                       | 3.5 kB 00:00:00
extras                                                                 | 3.4 kB 00:00:00
kubernetes/signature                                                   | 454 B 00:00:00
kubernetes/signature                                                   | 1.4 kB 00:00:10 !!!
updates                                                                | 3.4 kB 00:00:00
(1/7): base/7/x86_64/group_gz                                          | 166 kB 00:00:00
(2/7): extras/7/x86_64/primary_db                                      | 156 kB 00:00:00
(3/7): base/7/x86_64/primary_db                                        | 6.0 MB 00:00:00
(4/7): updates/7/x86_64/primary_db                                     | 1.3 MB 00:00:00
(5/7): docker-ce-stable/x86_64/primary_db                              | 20 kB 00:00:00
(6/7): docker-ce-stable/x86_64/updateinfo                              |  55 B 00:00:01
(7/7): kubernetes/primary                                              | 42 kB 00:00:01
Determining fastest mirrors
 * base: mirror.nl.datapacket.com
 * extras: mirror.nl.datapacket.com
 * updates: mirror.denit.net
kubernetes                                                                            305/305
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
===============================================================================================
 Package           Arch                Version                     Repository         Size
===============================================================================================
Installing:
 wget              x86_64              1.14-18.el7                 base              547 k
 
Transaction Summary
===============================================================================================
Install 1 Package
 
Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
wget-1.14-18.el7.x86_64.rpm                                            | 547 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
 Installing : wget-1.14-18.el7.x86_64                                                    1/1
install-info: No such file or directory for /usr/share/info/wget.info.gz
 Verifying : wget-1.14-18.el7.x86_64                                                    1/1
 
Installed:
 wget.x86_64 0:1.14-18.el7
 
Complete!
$HELM_HOME has been configured at /root/.helm.
 
Tiller (the Helm server-side component) has been upgraded to the current version.
Happy Helming!
etcdcluster.etcd.database.coreos.com/compose-etcd created
NAME                            READY    STATUS   RESTARTS  AGE
coredns-78fcdf6894-699fx        1/1      Running  0         6m
coredns-78fcdf6894-trslx        1/1      Running  0         6m
etcd-node1                      1/1      Running  0         5m
kube-apiserver-node1            1/1      Running  0         6m
kube-controller-manager-node1   1/1      Running  0         5m
kube-proxy-5gskp                1/1      Running  0         6m
kube-proxy-5hbkb                1/1      Running  0         6m
kube-proxy-lcsnz                1/1      Running  0         5m
kube-scheduler-node1            1/1      Running  0         5m
tiller-deploy-85744d9bfb-bjw2f  1/1      Running  0         1m
weave-net-9vt2s                 2/2      Running  1         6m
weave-net-k87d7                 2/2      Running  0         6m
weave-net-nmmt5                 2/2      Running  0         5m
--2019-01-21 14:28:49-- https://github.com/docker/compose-on-kubernetes/releases/download/v0.4.18/installer-linux
Resolving github.com (github.com)... 140.82.118.3, 140.82.118.4
Connecting to github.com (github.com)|140.82.118.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/158560458/e9a86500-15b2-11e9-8620-1eec5bf160e3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190121%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190121T142850Z&X-Amz-Expires=300&X-Amz-Signature=bd4020beb0f68210e2a3cfa8ca8166dddcf1d1e4868737eb9ad83363cd39c660&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dinstaller-linux&response-content-type=application%2Foctet-stream [following]
--2019-01-21 14:28:50-- https://github-production-release-asset-2e65be.s3.amazonaws.com/158560458/e9a86500-15b2-11e9-8620-1eec5bf160e3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190121%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190121T142850Z&X-Amz-Expires=300&X-Amz-Signature=bd4020beb0f68210e2a3cfa8ca8166dddcf1d1e4868737eb9ad83363cd39c660&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dinstaller-linux&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.161.163
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.161.163|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28376064 (27M) [application/octet-stream]
Saving to: 'installer-linux'
 
100%[=====================================================>] 28,376,064 15.9MB/s  in 1.7s
 
2019-01-21 14:28:52 (15.9 MB/s) - 'installer-linux' saved [28376064/28376064]
 
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[0001] Controller: image: "docker/kube-compose-controller:v0.4.18", pullPolicy: "Always"
failed to find a Stack API version
error: the server doesn't have a resource type "stacks"
[node1 pwk]$ sh prepare-pwk.sh
Creating Compose Namespace...
Installing Helm...
 % Total   % Received % Xferd Average Speed  Time   Time    Time Current
                                Dload Upload  Total  Spent   Left Speed
100 21.6M 100 21.6M   0    0 14.9M     0 0:00:01 0:00:01 --:--:-- 14.9M
Preparing Helm
linux-amd64/
linux-amd64/tiller
linux-amd64/helm
linux-amd64/LICENSE
linux-amd64/README.md
Creating tiller under kube-system namespace...
clusterrolebindings.rbac.authorization.k8s.io "tiller-cluster-rule" already exists
$HELM_HOME has been configured at /root/.helm.
 
Tiller (the Helm server-side component) has been upgraded to the current version.
Happy Helming!
 Tiller is still coming up...Please Wait
NAME                            READY    STATUS   RESTARTS  AGE
coredns-78fcdf6894-699fx        1/1      Running  0         7m
coredns-78fcdf6894-trslx        1/1      Running  0         7m
etcd-node1                      1/1      Running  0         6m
kube-apiserver-node1            1/1      Running  0         6m
kube-controller-manager-node1   1/1      Running  0         6m
kube-proxy-5gskp                1/1      Running  0         6m
kube-proxy-5hbkb                1/1      Running  0         7m
kube-proxy-lcsnz                1/1      Running  0         6m
kube-scheduler-node1            1/1      Running  0         6m
tiller-deploy-85744d9bfb-bjw2f  1/1      Running  0         2m
weave-net-9vt2s                 2/2      Running  1         6m
weave-net-k87d7                 2/2      Running  0         7m
weave-net-nmmt5                 2/2      Running  0         6m
NAME           REVISION       UPDATED                        STATUS         CHART       APP VERSION    NAMESPACE
etcd-operator  1              Mon Jan 21 14:27:50 2019       DEPLOYED       etcd-operator-0.8.3   0.9.3          compose
Run: helm ls --all etcd-operator; to check the status of the release
Or run: helm del --purge etcd-operator; to delete it
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirror.nl.datapacket.com
 * extras: mirror.nl.datapacket.com
 * updates: mirror.denit.net
Package wget-1.14-18.el7.x86_64 already installed and latest version
Nothing to do
$HELM_HOME has been configured at /root/.helm.
 
Tiller (the Helm server-side component) has been upgraded to the current version.
Happy Helming!
etcdcluster.etcd.database.coreos.com/compose-etcd unchanged
NAME                            READY    STATUS   RESTARTS  AGE
coredns-78fcdf6894-699fx        1/1      Running  0         7m
coredns-78fcdf6894-trslx        1/1      Running  0         7m
etcd-node1                      1/1      Running  0         6m
kube-apiserver-node1            1/1      Running  0         7m
kube-controller-manager-node1   1/1      Running  0         7m
kube-proxy-5gskp                1/1      Running  0         7m
kube-proxy-5hbkb                1/1      Running  0         7m
kube-proxy-lcsnz                1/1      Running  0         6m
kube-scheduler-node1            1/1      Running  0         6m
tiller-deploy-85744d9bfb-bjw2f  1/1      Running  0         2m
weave-net-9vt2s                 2/2      Running  1         7m
weave-net-k87d7                 2/2      Running  0         7m
weave-net-nmmt5                 2/2      Running  0         6m
--2019-01-21 14:30:05-- https://github.com/docker/compose-on-kubernetes/releases/download/v0.4.18/installer-linux
Resolving github.com (github.com)... 140.82.118.3, 140.82.118.4
Connecting to github.com (github.com)|140.82.118.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/158560458/e9a86500-15b2-11e9-8620-1eec5bf160e3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190121%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190121T143006Z&X-Amz-Expires=300&X-Amz-Signature=53d5f390f91b968a53219512c18b696e1a085cbbd59cdb953ca95bea1aca4d60&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dinstaller-linux&response-content-type=application%2Foctet-stream [following]
--2019-01-21 14:30:06-- https://github-production-release-asset-2e65be.s3.amazonaws.com/158560458/e9a86500-15b2-11e9-8620-1eec5bf160e3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20190121%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190121T143006Z&X-Amz-Expires=300&X-Amz-Signature=53d5f390f91b968a53219512c18b696e1a085cbbd59cdb953ca95bea1aca4d60&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3Dinstaller-linux&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.233.59
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.233.59|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 28376064 (27M) [application/octet-stream]
Saving to: 'installer-linux.1'
 
100%[=====================================================>] 28,376,064 16.3MB/s  in 1.7s
 
2019-01-21 14:30:08 (16.3 MB/s) - 'installer-linux.1' saved [28376064/28376064]
 
INFO[0000] Checking installation state
INFO[0001] Compose version v0.4.18 is already installed in namespace "compose" with the same settings
compose.docker.com/v1beta1
compose.docker.com/v1beta2
Waiting for the stack to be stable and running...
db1: Pending           [pod status: 0/2 ready, 2/2 pending, 0/2 failed]
web1: Pending          [pod status: 0/3 ready, 3/3 pending, 0/3 failed]
db1: Ready             [pod status: 1/2 ready, 1/2 pending, 0/2 failed]
web1: Ready            [pod status: 1/3 ready, 2/3 pending, 0/3 failed]
 
Stack hellostack is stable and running
 
NAME        SERVICES  PORTS       STATUS                           CREATED AT
hellostack  2         web1: 8082  Progressing (Stack is starting)  2019-01-21T14:30:10Z

验证 Stack

[node1 pwk]$ kubectl get stack
NAME        SERVICES  PORTS       STATUS                        CREATED AT
hellostack  2         web1: 8082  Available (Stack is started)  2019-01-21T14:30:10Z
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
4月前
|
Kubernetes 监控 调度
Kubernetes Pod调度:从基础到高级实战技巧
Kubernetes Pod调度:从基础到高级实战技巧
199 0
|
5月前
|
Kubernetes Shell Linux
linux|shell脚本|有趣的知识---格式化输出日志和脚本调试方法以及kubernetes集群核心服务重启和集群证书备份脚本
linux|shell脚本|有趣的知识---格式化输出日志和脚本调试方法以及kubernetes集群核心服务重启和集群证书备份脚本
72 0
|
5月前
|
Kubernetes Cloud Native 应用服务中间件
云原生|kubernetes 你真的学废了吗---实战k8s 一(jsonpath实战)
云原生|kubernetes 你真的学废了吗---实战k8s 一(jsonpath实战)
68 0
|
4月前
|
存储 Kubernetes 监控
Kubernetes快速进阶与实战:构建可靠的容器化应用平台
Kubernetes快速进阶与实战:构建可靠的容器化应用平台
101 0
|
2月前
|
Kubernetes Shell Docker
容器服务ACK常见问题之容器服务ACK kubectl命令写到shell脚本失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
4月前
|
存储 Kubernetes 安全
Kubernetes Pod配置:从基础到高级实战技巧
Kubernetes Pod配置:从基础到高级实战技巧
157 0
|
4月前
|
关系型数据库 MySQL 开发者
Docker Compose:轻松实现容器编排的利器
Docker Compose 是一款由 Docker 官方提供的容器编排工具,它通过简化容器应用的部署和管理,使得开发者们能够更加便捷地进行应用开发。本文将对 Docker Compose 的概念、功能及使用方法进行详细介绍,并结合实际案例进行演示,帮助读者更好地掌握 Docker Compose 的使用。
|
4月前
|
Docker 容器
docker容器编排利器Docker Compose(二)
docker容器编排利器Docker Compose(二)
77 1
|
4月前
|
应用服务中间件 Linux 数据库
docker容器编排利器Docker Compose(一)
docker容器编排利器Docker Compose(一)
78 0
|
5月前
|
Kubernetes 监控 Cloud Native
云原生|kubernetes|kubernetes集群巡检脚本
云原生|kubernetes|kubernetes集群巡检脚本
85 0

推荐镜像

更多