如何使用skaffold和minikube在本地为kubernetes开发

This is a very practical tutorial. I am not going to dwell on why microservices are here to stay and why Kubernetes is a beast to set up. I am going to cut it straight to the core. You have come here for a solution to your problem, and I am going to give you one.

这是一个非常实用的教程。 我将不讨论为什么微服务仍然存在,以及Kubernetes是如何建立的野兽。 我将直接切入核心。 您来这里是为了解决您的问题,我将给您一个。

You are probably reading this article because your organisation is using or decided to use Kubernetes.

您可能正在阅读本文,因为您的组织正在使用或决定使用Kubernetes。

But you or your development team is complaining because it is hard to develop locally without wasting time with deployments(docker build, docker push, kubectl rollout, etc). What is meant to be a one-liner code change, takes several minutes to test??? That is not acceptable.

但是您或您的开发团队在抱怨,因为很难在不浪费时间进行部署的情况下在本地进行开发(docker构建,docker push,kubectl推出等)。 什么是单行代码更改,需要几分钟的时间进行测试??? 那是不可接受的。

Thankfully there is a fix that will be like heaven on hell, for any developer looking to develop fast.

值得庆幸的是有一个修复程序,这将是像地狱天堂,任何开发人员,想要快速发展。

I introduce to you…. Minikube and Skaffold.

我向您介绍...。 MinikubeSkaffold

For this tutorial, you will be downloading a GitHub repository that I have created:

对于本教程,您将下载我创建的GitHub存储库:

In this repository, you will find a CMS component that uses Strapi and a front end written in NodeJS. Before you get too excited about the front end… don’t.

在这个库中,你会发现,使用Strapi和前端写在一个的NodeJS CMS组件。 在您对前端太兴奋之前……不要。

It’s just a few lines of javascript code that retrieves data from a Strapi backend using a REST API, to prove that we are able to connect and retrieve data.

仅有几行JavaScript代码使用REST API从Strapi后端检索数据,以证明我们能够连接和检索数据。

What you should be excited about, is that you should be able to start developing on Kubernetes after a few seconds by just executing a skaffold command. Of course, I am assuming you’ve already installed Minikube, Skaffold and Kustomize.

您应该兴奋的是,您只需几秒钟就能通过执行skaffold命令就能在Kubernetes上开始开发。 当然,我假设您已经安装了Minikube,Skaffold和Kustomize。

Let’s get started.

让我们开始吧。

Step 1: Install Docker

步骤1:安装Docker

Use this link for detailed instructions on how to install docker:

使用此链接获取有关如何安装docker的详细说明:

https://docs.docker.com/engine/install/

https://docs.docker.com/engine/install/

Step 2: Install Minikube

步骤2:安装Minikube

Use this link for detailed instructions: https://kubernetes.io/docs/tasks/tools/install-minikube/

使用此链接可获得详细说明: https : //kubernetes.io/docs/tasks/tools/install-minikube/

If you are on a MacOS you will want to start Minikube in the following way:

如果您使用的是MacOS,则需要通过以下方式启动Minikube:

$ minikube start --driver hyperkit --mount --mount-string /Users:/Users

$ minikube start --driver hyperkit --mount --mount-string /Users:/Users

By default Minikube only allocates 1GB of memory for its VM. You will need to give it more memory and possibly more CPUs. Otherwise, you will see memory errors when skaffold tries to redeploy certain containers.

默认情况下,Minikube仅为其VM分配1GB内存。 您将需要为其提供更多的内存,并可能需要更多的CPU。 否则,当skaffold尝试重新部署某些容器时,您将看到内存错误。

Example of starting Minikube with 8GB of memory and 4 cpus:

使用8GB内存和4 cpus启动Minikube的示例:

minikube start --driver hyperkit --mount --mount-string /Users:/Users --cpus 4 --memory 8192

The reason for this customised start script is so that we have a way to share files between our host and the Minikube VM. This could be useful for instance if you have a database backup with which you want to initialise Mysql. Or if you want to build a docker image directly on the docker registry in Minikube.

使用此自定义的启动脚本的原因是,我们可以在主机和Minikube VM之间共享文件。 例如,如果您有要初始化Mysql的数据库备份,这可能会很有用。 或者,如果您想直接在Minikube中的Docker注册表上构建Docker映像。

If you are trying to build a docker container directly from the command line then you will also want to run:

如果您尝试直接从命令行构建docker容器,则还需要运行:

eval $(docker-machine env -u)

But because we are using Skaffold to do the heavy lifting for us, this will not be necessary for us. Anyways, I have created a 5 min youtube video to explain how you would do that if you need to. Click the link below to watch.

但是,由于我们正在使用Skaffold为我们完成繁重的工作,因此对我们而言这不是必需的。 无论如何,我已经制作了一个5分钟的youtube视频,以解释您在需要时会如何做。 点击下面的链接观看。

Step 3: Install Skaffold

步骤3:安装Skaffold

To install Skaffold, follow these instructions: https://skaffold.dev/docs/install/

要安装Skaffold,请按照以下说明进行操作: https ://skaffold.dev/docs/install/

Step 4: Install standalone Kustomize

步骤4:安装独立的Kustomize

Minikube ships with Kustomize, which is integrated into the kubectl command.Unfortunately, Skaffold only works with the standalone version of Kustomize. So you will need to download and install Kustomize.

Minikube随Kustomize一起提供,后者集成在kubectl命令中。不幸的是, Skaffold仅适用于独立版本的Kustomize 。 因此,您将需要下载并安装Kustomize。

https://kubernetes-sigs.github.io/kustomize/installation/

https://kubernetes-sigs.github.io/kustomize/installation/

Step 5: Download the example strapi project

步骤5:下载示例stradi项目

git clone https://github.com/armindocachada/strapi-kubernetes-skaffold-example

git clone https://github.com/armindocachada/strapi-kubernetes-skaffold-example

Step 6: Modify overlays/dev/mysqlbackup-volumes.yml

步骤6:修改overlays / dev / mysqlbackup-volumes.yml

apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-backups-volume
labels:
type: local
spec:
storageClassName: mysql-backups-sc
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/Users/<YOUR USERNAME>/projects/strapi-kubernetes-skaffold-example/cms/db"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-sc-mysqlbackups
spec:
storageClassName: mysql-backups-sc
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi

Provide a full path to the db folder, which contains the DB dump.

提供db文件夹的完整路径,该文件夹包含数据库转储。

Step 7: Modify overlays/dev/strapi.yml

步骤7:修改overlays / dev / strapi.yml

---
apiVersion: v1
kind: PersistentVolume
metadata:
name: strapi-volume
labels:
type: local
spec:
storageClassName: strapi-sc
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/Users/<your username>/projects/strapi-kubernetes-skaffold-example/cms/persistent"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-sc-strapi
spec:
storageClassName: strapi-sc
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi

Provide a full path to the cms/persistent folder, which contains a copy of an example strapi schema.

提供cms / persistent文件夹的完整路径,该路径包含示例stradi架构的副本。

Do you have a suggestion on how to make this better? I would love to hear your feedback.

您对如何使它更好有建议吗? 我希望听到您的反馈。

Step 8: Start Skaffold

步骤8:启动Skaffold

$ cd strapi-kubernetes-skaffold-example
$ skaffold dev
Listing files to watch...
- example/strapi
Generating tags...
- example/strapi -> example/strapi:12ca63
Checking cache...
- example/strapi: Not found. Building
Found [minikube] context, using local docker daemon.
Building [example/strapi]...
Sending build context to Docker daemon 450kB
Step 1/9 : FROM strapi/strapi
---> dbaecb7cc54f
Step 2/9 : RUN mkdir /app
---> Using cache
---> 1e7cfae17eda
Step 3/9 : WORKDIR /app
---> Using cache
---> 69535ccbc52c
Step 4/9 : COPY ./cms/package.json .
---> Using cache
---> 3c8f79bd78fc
Step 5/9 : RUN yarn install
---> Using cache
---> e9aef355b99b
Step 6/9 : COPY ./cms .
---> 156f41f3ae4b
Step 7/9 : RUN strapi build
---> Running in 1b7bc12bbfff
Building your admin UI with development configuration ...
ℹ Compiling Webpack
✔ Webpack: Compiled successfully in 56.36s
---> 2fcb77c30ee1
Step 8/9 : CMD ["strapi", "start"]
...[cms] ┌─────────────────────────────┐
[cms] │ http://localhost:1337/admin
[cms] [2020-09-11T10:49:24.766Z] debug GET
/admin/33d5f0d956f3fc30bc51f81047a2c47d.woff2 (1 ms) 200

Note that Skaffold will continue running until you do Ctrl-C to terminate. At that point, it cleans up any pods, volumes or any resources that were created to deploy the app to Minikube.

请注意,Skaffold将继续运行,直到您执行Ctrl-C终止。 到那时,它会清理为将应用程序部署到Minikube而创建的所有pod,卷或任何资源。

You will see some Mysql connection errors at the start of skaffold. This is because the MySQL pod takes a bit longer to be available then the CMS strapi component. The CMS pod tries to connect to the MySQL database and since it is unable to, it crashes. Kubernetes is smart enough to restart the pod. Eventually, the errors will disappear and all the pods will be in the running state.

您会在skaffold的开头看到一些Mysql连接错误。 这是因为MySQL窗格需要的时间要比CMS bandi组件更长。 CMS pod尝试连接到MySQL数据库,由于无法连接,它崩溃了。 Kubernetes足够聪明,可以重新启动Pod。 最终,错误将消失并且所有吊舱都将处于运行状态。

In a production environment, this crash behaviour is not ideal as it generates large amounts of unsightly errors. But hey, we are not looking to use this for production!

在生产环境中,这种崩溃行为并不理想,因为它会产生大量难看的错误。 但是,我们不希望将其用于生产!

Step 9: Retrieve the URL for the CMS and the Front end

步骤9:检索CMS和前端的URL

If all goes well, you will have 3 pods running, and 3 persistence volumes.

如果一切顺利,您将运行3个Pod和3个持久性卷。

$ kubectl get podsNAME                         READY   STATUS    RESTARTS   AGEcms-7f69cb495b-4j5zd         1/1     Running   2          22m
cms-mysql-769ddb9469-n6jsc 1/1 Running 0 22m
front-end-769ddb9469-n6jsc 1/1 Running 0 22m$ kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-293db68b-ef2b-464a-a32d-15aaaee14d0e 1Gi RWO Delete Bound default/pvc-sc-strapi standard 24m
pvc-5c26a70b-dc5d-4432-b9dc-7ede3eed7e6b 1Gi RWO Delete Bound default/mysql-pv-claim standard 24m

To access the CMS and the front end you will need to call:

要访问CMS和前端,您需要致电:

$ minikube service cms --url
http://192.168.64.2:30391$ minikube service front-end --url
http://192.168.64.2:303222

The URLs above are unique to your environment.

上面的URL对您的环境是唯一的。

You are ready to develop now. Access the front end URL and you should be able to see the following screen:

您现在就准备好进行开发。 访问前端URL,您应该能够看到以下屏幕:

Image for post
Screenshot of front end screen
前端屏幕截图

It is not an amazing front end but you could improve it. For example, you could a title to the page in server.js.

它不是一个了不起的前端,但是您可以改进它。 例如,您可以在server.js中找到页面的标题。

You will notice that once you change the file, it will be automatically copied to the pod and Nodejs will restart automatically(using nodemon). This, without having to rebuild a new docker image.

您会注意到,一旦更改了文件,它将被自动复制到pod上,Nodejs将自动重新启动(使用nodemon)。 这样,无需重建新的Docker映像。

So literally you can develop and see your changes instantaneously appear. This can save months of development potentially.

因此,从字面上看,您可以开发并立即看到您的更改。 这样可以节省数月的开发时间。

Let us access the Strapi CMS and add an extra restaurant.

让我们访问Strapi CMS并添加一个额外的餐厅。

The username and password you will need to use are:

您需要使用的用户名和密码为:

username: admin@example.compassword: aZrnkuyQRbt9zgP

用户名: admin@example.com密码: aZrnkuyQRbt9zgP

This is a generated password and doesn’t match any of my existing passwords. Just saying it… I trust you all ;)

这是生成的密码,与我现有的任何密码都不匹配。 只是说...我相信你们所有人;)

Image for post

Click on Restaurants at the top left.

点击左上方的餐厅

Image for post

Add a new Restaurant, and assign it to an existing category. You can be as creative as you wish with your restaurant and description.

添加一个新的餐厅,并将其分配给现有的类别。 您可以根据自己的餐厅和描述来发挥创意。

You should have now two restaurants:

您现在应该有两家餐厅:

Image for post

Lets now refresh the front end.

现在让我们刷新前端。

Image for post

You can see now that there are two restaurants being displayed in our rough front end.

现在您可以看到,在我们粗糙的前端中显示了两个餐厅。

如何将它们放在一起 (How it was all put together)

The github repository, which by now, I hope, is bookmarked, has some important configuration files:

我希望现在将github存储库添加为书签,其中包含一些重要的配置文件:

skaffold.yaml (skaffold.yaml)

This file configures Skaffold so that it understands how to build our Strapi application. Here I tell it where it can find the Dockerfiles and where to look for environment configuration.

此文件配置Skaffold,以便它了解如何构建Strapi应用程序。 在这里,我告诉它可以在何处找到Dockerfile,以及在何处查找环境配置。

Note the sync section on each docker image section. This sync section does all the magic that we need to copy our changes to the docker containers without having to rebuild and redeploy a docker image. That would take too much precious time as redeploying even a simple container takes at least a couple of minutes.

注意每个docker映像部分上的sync部分。 这个同步部分完成了将更改复制到Docker容器所需的所有魔术,而无需重建和重新部署Docker映像。 这将花费太多宝贵的时间,因为重新部署一个简单的容器至少需要花费几分钟。

base/kustomize.yaml

base / kustomize.yaml

In the base folder, you will find the kubernetes deployment templates for our application. These deployment templates are the starting point for deployments in all environments. The kustomize.yaml allows us to bundle up all the deployment yaml files together as a bundle and deploy it as a unit to kubernetes.

基本文件夹中,您将找到我们应用程序的kubernetes部署模板。 这些部署模板是在所有环境中进行部署的起点。 kustomize.yaml允许我们将所有部署yaml文件捆绑在一起,然后作为一个单元部署到kubernetes。

Each environment can have a separate folder inside overlay and that allows us to make specific changes for the Strapi application to run in specific types of environments. For example, in a local development environment, we need to use HostPath volumes to share files from our local file system with the pods. We only want this to apply to the local development environment and nowhere else.

每个环境都可以在覆盖图内有一个单独的文件夹,这使我们可以对Strapi应用程序进行特定的更改,以使其在特定类型的环境中运行。 例如,在本地开发环境中,我们需要使用HostPath卷与Pod共享本地文件系统中的文件。 我们只希望将其应用于本地开发环境,而没有其他地方。

overlays/dev/kustomization.yaml

overlays / dev / kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patches:
- patch.ymlresources:
- ../../base
- strapi-volumes.yml
- mysqlbackups-volumes.yml

You can see that in the dev environment we include all the configuration from the base templates and we add extra templates, for Mysql and Strapi itself to have some initial data. This is key for a developer to be quickly up and running with a local Kubernetes environment.

您可以看到,在开发环境中,我们包括了基本模板的所有配置,并添加了额外的模板,以便Mysql和Strapi本身具有一些初始数据。 这是开发人员在本地Kubernetes环境中快速启动并运行的关键。

There is probably a lot more to be said about the code in the Git repository. But then I think it is better to let the source code to do all the talking!

关于Git存储库中的代码,可能还有很多要说的。 但是,我认为最好让源代码进行所有讨论!

If you have any questions I will try to resolve any queries!

如果您有任何疑问,我将尝试解决任何疑问!

翻译自: https://medium.com/swlh/how-to-develop-locally-for-kubernetes-using-skaffold-and-minikube-b0072e71abd0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值