1 Star 1 Fork 1

Summmer / kubernetes-vagrant-centos-cluster

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

使用Vagrant和VirtualBox在本地搭建分布式的Kubernetes集群和Istio Service Mesh

Setting up a Kubernetes cluster along with Istio service mesh locally with Vagrant and VirtualBox - English

当我们需要在本地开发时,更希望能够有一个开箱即用又可以方便定制的分布式开发环境,这样才能对Kubernetes本身和应用进行更好的测试。现在我们使用VagrantVirtualBox来创建一个这样的环境。

注意:因为使用虚拟机创建分布式Kubernetes集群比较耗费资源,所以我又仅使用Docker创建Standalone的Kubernetes的轻量级Cloud Native Sandbox

Demo

点击下面的图片观看视频。

观看视频

准备环境

需要准备以下软件和环境:

  • 8G以上内存
  • Vagrant 2.0+
  • VirtualBox 5.0 +
  • 提前下载Kubernetes 1.9以上版本(支持最新的1.13.0)的release压缩包
  • Mac/Linux,Windows不完全支持,仅在windows10下通过

集群

我们使用Vagrant和Virtualbox安装包含3个节点的kubernetes集群,其中master节点同时作为node节点。

IP 主机名 组件
172.17.8.101 node1 kube-apiserver、kube-controller-manager、kube-scheduler、etcd、kubelet、docker、flannel、dashboard
172.17.8.102 node2 kubelet、docker、flannel、traefik
172.17.8.103 node3 kubelet、docker、flannel

注意:以上的IP、主机名和组件都是固定在这些节点的,即使销毁后下次使用vagrant重建依然保持不变。

容器IP范围:172.33.0.0/30

Kubernetes service IP范围:10.254.0.0/16

安装的组件

安装完成后的集群包含以下组件:

  • flannel(host-gw模式)
  • kubernetes dashboard
  • etcd(单节点)
  • kubectl
  • CoreDNS
  • kubernetes(版本根据下载的kubernetes安装包而定,支持Kubernetes1.9+)

可选插件

  • Heapster + InfluxDB + Grafana
  • ElasticSearch + Fluentd + Kibana
  • Istio service mesh
  • Helm
  • Vistio
  • Kiali

使用说明

将该repo克隆到本地,下载Kubernetes的到项目的根目录。

git clone https://github.com/rootsongjc/kubernetes-vagrant-centos-cluster.git
cd kubernetes-vagrant-centos-cluster

注意:如果您是第一次运行该部署程序,那么可以直接执行下面的命令,它将自动帮你下载 Kubernetes 安装包,下一次你就不需要自己下载了,另外您也可以在这里找到Kubernetes的发行版下载地址,下载 Kubernetes发行版后重命名为kubernetes-server-linux-amd64.tar.gz,并移动到该项目的根目录下。

因为该项目是使用 NFS 的方式挂载到虚拟机的 /vagrant 目录中的,所以在安装 NFS 的时候需要您输入密码授权。

使用vagrant启动集群。

vagrant up

如果是首次部署,会自动下载centos/7的box,这需要花费一些时间,另外每个节点还需要下载安装一系列软件包,整个过程大概需要10几分钟。

如果您在运行vagrant up的过程中发现无法下载centos/7的box,可以手动下载后将其添加到vagrant中。

手动添加centos/7 box

wget -c http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1801_02.VirtualBox.box
vagrant box add CentOS-7-x86_64-Vagrant-1801_02.VirtualBox.box --name centos/7

这样下次运行vagrant up的时候就会自动读取本地的centos/7 box而不会再到网上下载。

Windows 安装特别说明

执行vagrant up之后会有如下提示:

G:\code\kubernetes-vagrant-centos-cluster>vagrant up
Bringing machine 'node1' up with 'virtualbox' provider...
Bringing machine 'node2' up with 'virtualbox' provider...
Bringing machine 'node3' up with 'virtualbox' provider...
==> node1: Importing base box 'centos/7'...
==> node1: Matching MAC address for NAT networking...
==> node1: Setting the name of the VM: node1
==> node1: Clearing any previously set network interfaces...
==> node1: Specific bridge 'en0: Wi-Fi (AirPort)' not found. You may be asked to specify
==> node1: which network to bridge to.
==> node1: Available bridged network interfaces:
1) Realtek PCIe GBE Family Controller
2) TAP-Windows Adapter V9
==> node1: When choosing an interface, it is usually the one that is
==> node1: being used to connect to the internet.
    node1: Which interface should the network bridge to?
    node1: Which interface should the network bridge to?
    

输入1之后按回车继续。(根据自己真实网卡选择,node2、node3同样需要)

node3快要结束的时候可能会有如下错误:

node3: Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
    node3: Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
    node3: deploy coredns
    node3: /tmp/vagrant-shell: ./dns-deploy.sh: /bin/bash^M: bad interpreter: No such file or directory
    node3: error: no objects passed to apply
    node3: /home/vagrant

解决方法:

vagrant ssh node3
sudo -i
cd /vagrant/addon/dns
yum -y install dos2unix
dos2unix dns-deploy.sh
./dns-deploy.sh -r 10.254.0.0/16 -i 10.254.0.2 |kubectl apply -f -

访问kubernetes集群

访问Kubernetes集群的方式有三种:

  • 本地访问
  • 在VM内部访问
  • Kubernetes dashboard

通过本地访问

可以直接在你自己的本地环境中操作该kubernetes集群,而无需登录到虚拟机中。

要想在本地直接操作Kubernetes集群,需要在你的电脑里安装kubectl命令行工具,对于Mac用户执行以下步骤:

wget https://storage.googleapis.com/kubernetes-release/release/v1.11.0/kubernetes-client-darwin-amd64.tar.gz
tar xvf kubernetes-client-darwin-amd64.tar.gz && cp kubernetes/client/bin/kubectl /usr/local/bin

conf/admin.kubeconfig文件放到~/.kube/config目录下即可在本地使用kubectl命令操作集群。

mkdir -p ~/.kube
cp conf/admin.kubeconfig ~/.kube/config

我们推荐您使用这种方式。

在虚拟机内部访问

如果有任何问题可以登录到虚拟机内部调试:

vagrant ssh node1
sudo -i
kubectl get nodes

Kubernetes dashboard

还可以直接通过dashboard UI来访问:https://172.17.8.101:8443

可以在本地执行以下命令获取token的值(需要提前安装kubectl):

kubectl -n kube-system describe secret `kubectl -n kube-system get secret|grep admin-token|cut -d " " -f1`|grep "token:"|tr -s " "|cut -d " " -f2

注意:token的值也可以在vagrant up的日志的最后看到。

Kubernetes dashboard animation

只有当你安装了下面的heapster组件后才能看到上图中的监控metrics。

Windows下Chrome/Firefox访问

如果提示NET::ERR_CERT_INVALID,则需要下面的步骤

进入本项目目录

vagrant ssh node1
sudo -i
cd /vagrant/addon/dashboard/
mkdir certs
openssl req -nodes -newkey rsa:2048 -keyout certs/dashboard.key -out certs/dashboard.csr -subj "/C=/ST=/L=/O=/OU=/CN=kubernetes-dashboard"
openssl x509 -req -sha256 -days 365 -in certs/dashboard.csr -signkey certs/dashboard.key -out certs/dashboard.crt
kubectl delete secret kubernetes-dashboard-certs -n kube-system
kubectl create secret generic kubernetes-dashboard-certs --from-file=certs -n kube-system
kubectl delete pods $(kubectl get pods -n kube-system|grep kubernetes-dashboard|awk '{print $1}') -n kube-system #重新创建dashboard

刷新浏览器之后点击高级,选择跳过即可打开页面。

组件

Heapster监控

创建Heapster监控:

kubectl apply -f addon/heapster/

访问Grafana

使用Ingress方式暴露的服务,在本地/etc/hosts中增加一条配置:

172.17.8.102 grafana.jimmysong.io

访问Grafana:http://grafana.jimmysong.io

Grafana动画

Traefik

部署Traefik ingress controller和增加ingress配置:

kubectl apply -f addon/traefik-ingress

在本地/etc/hosts中增加一条配置:

172.17.8.102 traefik.jimmysong.io

访问Traefik UI:http://traefik.jimmysong.io

Traefik Ingress controller

EFK

使用EFK做日志收集。

kubectl apply -f addon/efk/

注意:运行EFK的每个节点需要消耗很大的CPU和内存,请保证每台虚拟机至少分配了4G内存。

Helm

用来部署helm。

hack/deploy-helm.sh

Service Mesh

我们使用 istio 作为 service mesh。

安装

Istio release 页面下载istio的安装包,安装istio命令行工具,将istioctl命令行工具放到你的$PATH目录下,对于Mac用户:

wget https://github.com/istio/istio/releases/download/1.0.0/istio-1.0.0-osx.tar.gz
tar xvf istio-1.0.0-osx.tar.gz
mv bin/istioctl /usr/local/bin/

在Kubernetes中部署istio:

kubectl apply -f addon/istio/istio-demo.yaml
kubectl apply -f addon/istio/istio-ingress.yaml

运行示例

我们开启了Sidecar自动注入。

kubectl label namespace default istio-injection=enabled
kubectl apply -n default -f yaml/istio-bookinfo/bookinfo.yaml
kubectl apply -n default -f yaml/istio-bookinfo/bookinfo-gateway.yaml
kubectl apply -n default -f yaml/istio-bookinfo/destination-rule-all.yaml

在您自己的本地主机的/etc/hosts文件中增加如下配置项。

172.17.8.102 grafana.istio.jimmysong.io
172.17.8.102 prometheus.istio.jimmysong.io
172.17.8.102 servicegraph.istio.jimmysong.io
172.17.8.102 jaeger-query.istio.jimmysong.io

我们可以通过下面的URL地址访问以上的服务。

Service URL
grafana http://grafana.istio.jimmysong.io
servicegraph http://servicegraph.istio.jimmysong.io/dotviz, http://servicegraph.istio.jimmysong.io/graph,http://servicegraph.istio.jimmysong.io/force/forcegraph.html
tracing http://jaeger-query.istio.jimmysong.io
productpage http://172.17.8.101:31380/productpage

详细信息请参阅:https://istio.io/zh/docs/examples/bookinfo/

Bookinfo Demo

Vistio

Vizceral是Netflix发布的一个开源项目,用于近乎实时地监控应用程序和集群之间的网络流量。Vistio是使用Vizceral对Istio和网格监控的改进。它利用Istio Mixer生成的指标,然后将其输入Prometheus。Vistio查询Prometheus并将数据存储在本地以允许重播流量。

# Deploy vistio via kubectl
kubectl -n default apply -f addon/vistio/

# Expose vistio-api
kubectl -n default port-forward $(kubectl -n default get pod -l app=vistio-api -o jsonpath='{.items[0].metadata.name}') 9091:9091 &

# Expose vistio in another terminal window
kubectl -n default port-forward $(kubectl -n default get pod -l app=vistio-web -o jsonpath='{.items[0].metadata.name}') 8080:8080 &

如果一切都已经启动并准备就绪,您就可以访问Vistio UI,开始探索服务网格网络,访问http://localhost:8080 您将会看到类似下图的输出。

vistio animation

更多详细内容请参考Vistio—使用Netflix的Vizceral可视化Istio service mesh

Kiali

Kiali是一个用于提供Istio service mesh观察性的项目,更多信息请查看https://kiali.io

在本地该项目的根路径下执行下面的命令:

kubectl apply -n istio-system -f addon/kiali

Kiali web地址:http://172.17.8.101:32439

用户名/密码:admin/admin

kiali

注意:Kilia使用Jaeger做追踪,请不用屏蔽kilia页面的弹出窗口。

Weave scope

Weave scope可用于监控、可视化和管理Docker&Kubernetes集群,详情见https://www.weave.works/oss/scope/

在本地该项目的根路径下执行下面的命令:

kubectl apply -f addon/weave-scope

在本地的/etc/hosts下增加一条记录。

172.17.8.102 scope.weave.jimmysong.io

现在打开浏览器,访问http://scope.weave.jimmysong.io/

Weave scope动画

管理

除了特别说明,以下命令都在当前的repo目录下操作。

挂起

将当前的虚拟机挂起,以便下次恢复。

vagrant suspend

恢复

恢复虚拟机的上次状态。

vagrant resume

注意:我们每次挂起虚拟机后再重新启动它们的时候,看到的虚拟机中的时间依然是挂载时候的时间,这样将导致监控查看起来比较麻烦。因此请考虑先停机再重新启动虚拟机。

重启

停机后重启启动。

vagrant halt
vagrant up
# login to node1
vagrant ssh node1
# run the prosivision scripts
/vagrant/hack/k8s-init.sh
exit
# login to node2
vagrant ssh node2
# run the prosivision scripts
/vagrant/hack/k8s-init.sh
exit
# login to node3
vagrant ssh node3
# run the prosivision scripts
/vagrant/hack/k8s-init.sh
sudo -i
cd /vagrant/hack
./deploy-base-services.sh
exit

现在你已经拥有一个完整的基础的kubernetes运行环境,在该repo的根目录下执行下面的命令可以获取kubernetes dahsboard的admin用户的token。

hack/get-dashboard-token.sh

根据提示登录即可。

清理

清理虚拟机。

vagrant destroy
rm -rf .vagrant

注意

仅做开发测试使用,不要在生产环境使用该项目。

参考

更多Istio和Service Mesh的资讯请访问ServiceMesher社区和关注社区的微信公众号。

ServiceMesher微信公众号二维码

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

Setting up a distributed Kubernetes cluster along with Istio service mesh locally with Vagrant and VirtualBox, only PoC or Demo use. 展开 收起
Shell
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Shell
1
https://gitee.com/Summmer/kubernetes-vagrant-centos-cluster.git
git@gitee.com:Summmer/kubernetes-vagrant-centos-cluster.git
Summmer
kubernetes-vagrant-centos-cluster
kubernetes-vagrant-centos-cluster
master

搜索帮助