kubernetes 集群_如何在本地创建kubernetes集群简单教程

kubernetes 集群

如何在本地创建Kubernetes集群并部署与Kubernetes通信的简单前端应用 (How to create a Kubernetes cluster locally and deploy simple front-end apps that communicate with Kubernetes)

As a software engineer at Capital One, I get to explore cutting edge technologies every day in my work. I have worked with Docker and Docker Swarm and I always wanted to learn Kubernetes. However, I kept postponing it. Finally, I was able to dive into it and thought, “Why not create an application with Kubernetes and write about it while my mind is fresh!” That way others — and not just myself — can benefit from what I learned.

作为Capital One的软件工程师,我每天都在工作中探索最先进的技术。 我曾与Docker和Docker Swarm合作过,我一直想学习Kubernetes。 但是,我一直推迟。 最终,我得以深入研究,并思考:“为什么不使用Kubernetes创建应用程序并在新想法下编写它!” 这样,他人(而不仅仅是我自己)可以从我学到的知识中受益。

介绍 (Introduction)

Today we are going to create a Kubernetes cluster and deploy a simple React JS app which generates a random number by calling an Express JS app. We are going to orchestrate the whole process by using Kubernetes. We will first dockerize our front end/back end apps. Then using Kubernetes we will deploy the pods (React front end app/Express back end app) and access them via Kubernetes services.

今天,我们将创建一个Kubernetes集群,并部署一个简单的React JS应用,该应用通过调用Express JS应用生成一个随机数。 我们将使用Kubernetes协调整个过程。 我们将首先对前端/后端应用进行docker化。 然后使用Kubernetes,我们将部署Pod(React前端应用程序/ Express后端应用程序)并通过Kubernetes服务访问它们。

white button with black text next to a 90 written in black text, button is outlined in blue.

什么是Kubernetes集群? (What is a Kubernetes Cluster?)

Like any cluster you provide a set of nodes to Kubernetes. You tell kubernetes how to deploy containers in the cluster. How much memory or processing units each container gets and how they interact with each other.

像任何集群一样,您为Kubernetes提供了一组节点。 您告诉kubernetes如何在集群中部署容器。 每个容器获得多少内存或处理单元,以及它们如何相互影响。

Kubernetes集群图 (Kubernetes Cluster Diagram)

The below diagram depicts what we are going to achieve today, where we will create a Kubernetes cluster and services/deployments for our front and back ends.

下图描述了我们今天要实现的目标,我们将在其中创建一个Kubernetes集群以及前端和后端的服务/部署。

flowchart depicting kubernetes cluster starting with the user

先决条件 (Prerequisites)

  1. Docker for Desktop (latest version)

    桌面Docker (最新版本)

  2. Kubernetes or Docker Kubernetes

    KubernetesDocker Kubernetes

  3. Node & NPM (Only if you want to run the applications stand alone)

    节点和NPM(仅当您想独立运行应用程序时)
  4. YAML

    YAML

NOTE: This tutorial requires basic/working knowledge on Docker, Node, & NPM.

注意: 本教程需要有关Docker,Node和NPM的基础知识/工作知识。

Let’s dive in without any further ado.

事不宜迟,让我们潜入。

Kubernetes入门 (Getting Started With Kubernetes)

安装Kubernetes (Installing Kubernetes)

If you have Docker Desktop, go to preferences, go to the Kubernetes tab, and click Enable Kubernetes. It may take a while to spin up Kubernetes on to your machine, so go make a coffee while it does its magic. ☕

如果您具有 Docker Desktop ,请转到 首选项 ,转到 Kubernetes选项卡, 然后单击 Enable Kubernetes 将Kubernetes启动到您的机器上可能需要一段时间,所以在它发挥神奇作用的同时去煮咖啡。

Image for post

To verify if Kubernetes is running, type the below two commands:

要验证Kubernetes是否正在运行,请键入以下两个命令:

kubectl versionOutputs:Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.0", GitCommit:"e8462b5b5dc2584fdcd18e6bcfe9f1e4d970a529", GitTreeState:"clean", BuildDate:"2019-06-19T16:40:16Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"darwin/amd64"}Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.8", GitCommit:"211047e9a1922595eaa3a1127ed365e9299a6c23", GitTreeState:"clean", BuildDate:"2019-10-15T12:02:12Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}kubectl cluster-infoOutputs:
Kubernetes master is running at https://kubernetes.docker.internal:6443KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

将应用程序捆绑为Docker映像 (Bundling Application As Docker Images)

完整的代码可以在GitHub找到 (The full code for this can be found on GitHub)

Please download the project. The project has two sub folders in it.

请下载项目。 该项目中有两个子文件夹。

  • Client -> React based app

    客户端->基于React的应用
  • Server -> Express based app

    服务器->基于Express的应用

Please follow the below steps to setup the project and start the Kubernetes cluster:

请按照以下步骤设置项目并启动Kubernetes集群:

客户 (Client)

  • cd client

    cd客户端

  • npm install

    npm安装

  • npm run build

    npm运行构建

  • docker build -t frontend:1.0 .

    docker build -t前端:1.0。

  • kubectl apply -f frontend.deploy.yml

    kubectl套用-f frontend.deploy.yml

  • kubectl apply -f frontend.service.yml

    kubectl应用-f frontend.service.yml

后端 (Backend)

  • cd server

    CD服务器

  • npm install

    npm安装

  • docker build -t backend:1.0 .

    docker build -t后端:1.0。

  • kubectl apply -f backend.deploy.yml

    kubectl应用-f backend.deploy.yml

  • kubectl apply -f backend.service.yml

    kubectl应用-f backend.service.yml

Go to the browser, type localhost and hit enter, you should see the application loaded.

转到浏览器,输入localhost并按Enter,您应该看到已加载应用程序。

进一步降低 (Bringing It Down Further)

Kubernetes豆荚 (Kubernetes Pods)

Pods are the smallest deployable units of computing that can be created and managed in Kubernetes.

Pod是可以在Kubernetes中创建和管理的最小的可部署计算单元。

kubectl run nginx-frontend --image=frontend:1.0

The above creates a pod which hosts the front end container. You can’t access it yet since a host port isn’t exposed for the container. We will uncover those later.

上面创建了一个承载前端容器的容器。 您尚未访问它,因为该容器未暴露主机端口。 我们稍后会发现。

A pod can host multiple containers as well. Instead of creating via command line let’s create via a YAML file.

吊舱也可以容纳多个容器。 与其通过命令行创建,不如通过YAML文件创建。

# To create a pod with multiple containers
kubectl apply -f app.pod.yml# To see container status
kubectl get pod/mymulticontainerapp
Outputs:
NAME READY STATUS RESTARTS AGE
mymulticontainerapp 2/2 Running 0 9m# To see more details about the container
kubectl describe pod/mymulticontainerapp

To access the container from the host machine you have to do a port forwarding, i.e attaching host port to container port. You can do that via the below command:

要从主机访问容器,您必须执行端口转发,即将主机端口连接到容器端口。 您可以通过以下命令进行操作:

kubectl port-forward pod/mymulticontainerapp 9999:80 3000:3000Outputs:
Forwarding from 127.0.0.1:9999 -> 80
Forwarding from [::1]:9999 -> 80
Forwarding from 127.0.0.1:3000 -> 3000
Forwarding from [::1]:3000 -> 3000

The above command exposes the host port 9999 to container port 80 and host port 3000 to container port 3000 which is what our front end/back end containers are listening on. Any request to host 9999 will be forwarded to container on port 80. Any request to the host 3000 will be forwarded to container on port 3000.

上面的命令将主机端口9999暴露给容器端口80,并将主机端口3000暴露给容器端口3000,这是我们前端/后端容器正在侦听的内容。 对主机9999的任何请求将转发到端口80上的容器。对主机3000的任何请求将转发到端口3000上的容器。

Go to the browser and open 127.0.0.1:9999 or localhost:9999, it should load the front end app.

转到浏览器并打开127.0.0.1:9999或localhost:9999,它将加载前端应用程序。

To delete a pod you can do with below command:

要删除广告连播,您可以使用以下命令进行操作:

k delete pod/mymulticontainerapp

To inspect a pod you can use the below command:

要检查吊舱,可以使用以下命令:

Front End:
kubectl exec mymulticontainerapp -c myfrontendapp -it /bin/sh
ls /usr/share/nginx/html
exitBack End:
kubectl exec mymulticontainerapp -c mybackendapp -it /bin/sh
ls
exit

The above commands open a shell inside a container to interact.

上面的命令打开容器内的外壳进行交互。

Before we jump on to the next topic deployments — it’s crucial that you understand metadata in a YAML file. Metadata is data about the containers. You can add labels and key-values to the metadata. You can also use the labels or key-values as selectors to identify a pod which will be used by deployments/services later.

在我们进入下一个主题-部署之前,了解YAML文件中的元数据至关重要。 元数据是有关容器的数据。 您可以向元数据添加标签和键值。 您还可以将标签或键值用作选择器,以标识稍后将由部署/服务使用的Pod。

Let’s get all the pods labeled as mymulticontainerapp:

让我们将所有吊舱标记为mymulticontainerapp

kubectl get pods --selector=name=mymulticontainerapp

部署 (Deployments)

You describe a desired state in a deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define deployments to create new ReplicaSets, or to remove existing deployments and adopt all their resources with new deployments.

您在部署中描述所需的状态,Deployment Controller将实际状态以受控的速率更改为所需的状态。 您可以定义部署以创建新的副本集,或删除现有部署并在新部署中采用其所有资源。

It should look pretty similar to the previous YAML you saw. The salient point to note here is kind — it’s a deployment rather than pod.

它看起来应该与您之前看到的YAML非常相似。 这里要指出的重点是善良的 -它是部署而不是pod。

Selector: It is used to identify existing pods by labels, such as their metadata. If there is a pod already running with such a label, it will be part of this deployment.

选择器:用于通过标签(例如其元数据)识别现有的窗格。 如果已经有一个带有此类标签的吊舱正在运行,则它将成为此部署的一部分。

Replicas: It specifies how many pods within this container you wish to create. If you say two, it will have two pods running the back end.

副本:它指定您要在此容器中创建多少个吊舱。 如果您说两个,则后端将有两个吊舱。

Let’s say replicas is set to two and there are four pods already running with the label app: node-backend. It will terminate two of those pods to meet the requirement of two replicas. It will also scale up the pods by two if replicas is set to six.

假设副本设置为两个,并且标签app: node-backend已运行了四个pod app: node-backend 。 它将终止其中两个吊舱,以满足两个副本的要求。 如果将副本数设置为六个,则还会将Pod放大两倍。

Spec: It specifies the details about the container such as image name, container port, CPU/memory limits for the container inside the pod, etc.

规范:它指定有关容器的详细信息,例如映像名称,容器端口,容器内容器的CPU /内存限制等。

Template: It specifies the tags to be used for the newly created pods via the spec.

模板:通过规范指定用于新创建的广告连播的标签。

***

***

To create a deployment run the below command:

要创建部署,请运行以下命令:

kubectl apply -f backend.deploy.ymlOutput:
deployment.apps/node-backend created

To see all the deployments:

要查看所有部署:

k get deploymentsOutput:
NAME READY UP-TO-DATE AVAILABLE AGE
node-backend 2/2 2 2 46s

To access the containers created during deployment you can use port-forward.

要访问在部署期间创建的容器,可以使用端口转发。

kubectl port-forward deployment/node-backend 3000:3000You can access by going to localhost:3000/random

Kubernetes服务 (Kubernetes Services)

An abstract way to expose an application running on a set of pods is as a network service.

公开运行在一组Pod上的应用程序的抽象方法是作为网络服务。

Things to notice here — the kind is Service and the type is LoadBalancer. This creates a LoadBalancer on host port 3000 and proxies the request to container on 3000.

这里要注意的事情-类型是Service,类型是LoadBalancer。 这将在主机端口3000上创建LoadBalancer ,并将请求代理到3000上的容器。

What is LoadBalancer balancing here? If you look closely we specified a selector. The selector searches for pods with label app: node-background and any request sent to the host on 3000 will be load balances among those pods. Here we have two replicas and the request will be load balanced to these two pods.

这里的LoadBalancer均衡器是什么? 如果您仔细观察,我们指定了一个选择器。 选择器搜索标签为app: node-background Pod,并且在3000上发送给主机的任何请求将是这些Pod之间的负载平衡。 在这里,我们有两个副本,请求将负载平衡到这两个Pod。

To create a service run the below command:

要创建服务,请运行以下命令:

kubectl apply -f backend.service.yml.

To see all the services run the below command:

要查看所有服务,请运行以下命令:

k get service
black screen with lines of white code and red markings over it

This tells you the service name and what type of service it is. Here if you see there is a service called backend and of type LoadBalancer, which we just created using the service yml.

这会告诉您服务名称和服务类型。 在这里,如果您看到有一个名为backend且类型为LoadBalancer的服务,我们刚刚使用服务yml创建了该服务。

You can do the same steps for frontend as well:

您也可以对前端执行相同的步骤:

kubectl apply -f backend.deploy.yml
kubectl apply -f backend.service.yml

那不是那么容易吗? (Wasn’t That Easy?)

We have looked at how to create a Kubernetes cluster and deploy a simple front end app that communicates with a backend app in Kubernetes. Hope this helps your understanding of the basics of Kubernetes. Kubernetes is a vast ocean and we just touched a tiny drop of it. But I hope this helps pave the path for your future Kubernetes learning!

我们已经研究了如何创建Kubernetes集群并部署一个简单的前端应用程序,该应用程序与Kubernetes中的后端应用程序进行通信。 希望这有助于您了解Kubernetes的基础知识。 Kubernetes是一片广阔的海洋,我们只触摸了其中的一小滴。 但是我希望这可以为您将来的Kubernetes学习铺平道路!

Useful Links:

有用的链接:

https://kubernetes.io/docs/concepts/

https://kubernetes.io/docs/concepts/

https://docs.docker.com/

https://docs.docker.com/

DISCLOSURE STATEMENT: © 2020 Capital One. Opinions are those of the individual author. Unless noted otherwise in this post, Capital One is not affiliated with, nor endorsed by, any of the companies mentioned. All trademarks and other intellectual property used or displayed are property of their respective owners.

披露声明:©2020 Capital One。 观点是个别作者的观点。 除非本文中另有说明,否则Capital One不与任何提及的公司有附属关系或认可。 使用或显示的所有商标和其他知识产权均为其各自所有者的财产。

翻译自: https://medium.com/capital-one-tech/how-to-create-a-kubernetes-cluster-locally-simple-tutorial-63eab6c8c3f5

kubernetes 集群

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值