容器和Kubernetes入门:DigitalOcean工作坊套件
Getting Started with Containers and Kubernetes Workshop Kit Materials容器和Kubernetes Workshop Kit材料入门This meetup kit is designed to help a technical audience become familiar with core Kubernetes conc...
This meetup kit is designed to help a technical audience become familiar with core Kubernetes concepts and practices.
该见面工具包旨在帮助技术读者熟悉Kubernetes的核心概念和实践。
The aim is to provide a complete set of resources for a speaker to host an event and deliver an introductory talk on containers and Kubernetes. It includes:
目的是为演讲者提供一整套资源,以主持活动并就容器和Kubernetes进行介绍性演讲。 这包括:
Slides and speaker notes with commands for running a live demo. This talk runs for roughly 45 minutes.
幻灯片和演讲者备注以及用于运行实时演示的命令。 这场演讲进行了大约45分钟。
A GitHub repository containing the demo app code
包含演示应用程序代码的GitHub存储库
The Flask demo app Docker Hub repository
Flask演示应用程序Docker Hub存储库
This tutorial, which walks a user through rolling out the demo “Hello World” Flask app on a DigitalOcean Kubernetes cluster
本教程将引导用户在DigitalOcean Kubernetes集群上部署演示“ Hello World” 烧瓶应用程序
This tutorial is intended to supplement the talk demo with additional detail and elucidation. It also serves as a reference for readers seeking to get a minimal containerized Flask app up and running on DigitalOcean Kubernetes.
本教程旨在通过其他细节和说明来补充演讲演示。 它也为寻求在DigitalOcean Kubernetes上启动并运行最小的容器化Flask应用程序的读者提供参考。
介绍 (Introduction)
In the past decade, containerized applications and container clusters have rapidly replaced the old paradigm of scaling applications using virtual machines. Containers offer the same process isolation, but are generally more lightweight, portable, and performant than full virtualization. Container clusters, which can be used to manage thousands of running containers across a set of physical machines, abstract away much of the work of rolling out new versions of applications, scaling them, and efficiently scheduling workloads. Out of these, Kubernetes has emerged as a mature, production-ready system. It provides a rich set of features like rolling deployments, health checking, self-monitoring, workload autoscaling, and much, much more.
在过去的十年中,容器化应用程序和容器集群已Swift取代了使用虚拟机扩展应用程序的旧范例。 容器提供了相同的过程隔离,但与完全虚拟化相比,容器通常更轻便,可移植且性能更高。 容器集群可用于管理一组物理机上成千上万个运行中的容器,从而消除了推出新版本的应用程序,扩展它们并有效地调度工作负载的许多工作。 其中, Kubernetes已成为成熟的,可投入生产的系统。 它提供了丰富的功能,例如滚动部署,运行状况检查,自我监控,工作负载自动扩展等等。
This tutorial, designed to accompany the Slides and speaker notes for the Getting Started with Kubernetes Meetup Kit, will show you how to harness these technologies and deploy the “Hello World” Flask app onto a DigitalOcean Kubernetes cluster.
本教程旨在与Kubernetes Meetup入门指南的幻灯片和演讲者注释一起使用,向您展示如何利用这些技术并将“ Hello World” Flask应用程序部署到DigitalOcean Kubernetes集群上。
先决条件 (Prerequisites)
To follow this tutorial, you will need:
要遵循本教程,您将需要:
A Kubernetes 1.10+ cluster with role-based access control (RBAC) enabled. This setup will use a DigitalOcean Kubernetes cluster
启用了基于角色的访问控制(RBAC)的Kubernetes 1.10+集群。 此设置将使用DigitalOcean Kubernetes集群
The
kubectl
command-line tool installed on your local machine or development server and configured to connect to your cluster. You can read more about installingkubectl
in the official documentation.kubectl
命令行工具安装在本地计算机或开发服务器上,并配置为连接到集群。 您可以在官方文档中阅读有关安装kubectl
更多信息。Docker installed on your local machine or development server. If you are working with Ubuntu 18.04, follow Steps 1 and 2 of How To Install and Use Docker on Ubuntu 18.04; otherwise, follow the official documentation for information about installing on other operating systems. Be sure to add your non-root user to the
docker
group, as described in Step 2 of the linked tutorial.将Docker安装在本地计算机或开发服务器上。 如果您正在使用Ubuntu 18.04,请遵循如何在Ubuntu 18.04上安装和使用Docker的步骤1和2; 否则,请遵循官方文档以获取有关在其他操作系统上安装的信息。 确保按照链接教程的第2步中所述将非root用户添加到
docker
组。A Docker Hub account (optional). For an overview of how to set this up, refer to this introduction to Docker Hub. You’ll only need a Docker Hub account if you plan on modifying the Flask Docker image described in this tutorial.
Docker Hub帐户(可选)。 有关如何进行此设置的概述,请参阅此介绍给泊坞枢纽。 如果您打算修改本教程中描述的Flask Docker映像,则仅需要Docker Hub帐户。
第1步-克隆应用程序存储库并构建Flask映像 (Step 1 — Cloning the App Repository and Building the Flask Image)
To begin, clone the demo Flask app repo onto your machine, navigate into the directory, and list the directory contents:
首先,将演示烧瓶应用程序仓库克隆到您的计算机上,导航到目录并列出目录内容:
- git clone https://github.com/do-community/k8s-intro-meetup-kit.git git clone https://github.com/do-community/k8s-intro-meetup-kit.git
- cd k8s-intro-meetup-kit cd k8s-meetup-kit套件
- ls ls
Output
LICENSE README.md app k8s
The app
directory contains the Flask demo app code, as well as the Dockerfile for building its container image. The k8s
directory contains Kubernetes manifest files for a Pod, Deployment, and Service. To learn more about these Kubernetes objects, consult the slide deck or An Introduction to Kubernetes.
app
目录包含Flask演示应用程序代码以及用于构建其容器映像的Dockerfile 。 k8s
目录包含用于Pod,Deployment和Service的Kubernetes清单文件。 要了解有关这些Kubernetes对象的更多信息,请查阅幻灯片或Kubernetes简介 。
Navigate into the app
directory and print out the contents of the app.py
file:
导航到app
目录并打印出app.py
文件的内容:
- cat app.py 猫app.py
Output
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
This code defines a single default route that will print “Hello World.” Additionally, the apps runs in debug mode to enable verbose output.
此代码定义了一个默认路由,该默认路由将显示“ Hello World”。 此外,这些应用程序以调试模式运行以启用详细输出。
In a similar fashion, cat
out the contents of the app’s Dockerfile
:
以类似的方式, cat
出来的应用程序的内容是Dockerfile
:
- cat Dockerfile 猫Dockerfile
Output
FROM python:3-alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
This Dockerfile first sources a lightweight Alpine Linux Python parent image. It then copies in the Python requirements file, installs Flask, copies the app code into the container image, defines port 5000
as the container port, and finally sets the default command to python app.py
.
该Dockerfile首先提供轻量级的Alpine Linux Python父映像。 然后将其复制到Python需求文件中,安装Flask,将应用程序代码复制到容器映像中,将端口5000
定义为容器端口,最后将默认命令设置为python app.py
Next, build the app image:
接下来,构建应用程序图像:
- cd app 光盘应用
- docker build -t flask_demo:v0 . docker build -t flask_demo:v0。
We give the image a name, flask_demo
, and tag, v0
using the -t
option.
我们使用-t
选项将图像命名为flask_demo
,并将标签命名为v0
。
After Docker finishes the build, run the container using run
:
Docker完成构建后,使用run
运行容器:
- docker run -p 5000:5000 flask_demo:v0 泊坞窗运行-p 5000:5000 flask_demo:v0
This command runs a container using the flask:v0
image, and forwards local port 5000
to container port 5000
.
此命令使用flask:v0
映像运行容器,并将本地端口5000
转发到容器端口5000
。
If you’re running Docker on your local machine, navigate to http://localhost:5000
in your web browser. You should see “Hello World,” generated by the dockerized Flask app.
如果您在本地计算机上运行Docker,请在Web浏览器中导航至http://localhost:5000
。 您应该看到由docker化的Flask应用生成的“ Hello World”。
If you’re running Docker on a dev server, navigate instead to http://dev_server_external_IP:5000
. If you’re running a firewall like UFW, be sure to allow external access on port 5000
. To learn more about doing this with UFW, consult UFW Essentials: Common Firewall Rules and Commands.
如果您在开发服务器上运行Docker,请改用http:// dev_server_external_IP :5000
。 如果您正在运行UFW之类的防火墙,请确保允许在端口5000
上进行外部访问。 要了解有关使用UFW进行此操作的更多信息,请查阅UFW Essentials:通用防火墙规则和命令 。
At this point you can experiment with Docker commands like docker ps
, docker top
, and docker images
to practice working with images and containers on your system.
此时,您可以尝试使用诸如docker ps
, docker top
和docker images
类的Docker命令,以练习在系统上使用图像和容器。
In the next step, we’ll deploy this demo app to your Kubernetes cluster. We’ll use a prebuilt image shared publicly on Docker Hub. If you’d like to customize the Flask app and use your own image, you should create a Docker Hub account and follow the steps in this introduction to push your image to a public repository. From there, Kubernetes will be able to pull and deploy the container image into your cluster.
下一步,我们将将此演示应用程序部署到您的Kubernetes集群。 我们将使用在Docker Hub上公开共享的预构建映像。 如果您想自定义Flask应用并使用自己的映像,则应创建一个Docker Hub帐户,并按照本简介中的步骤将映像推送到公共存储库。 从那里,Kubernetes将能够将容器映像拉出并部署到您的集群中。
步骤2 —在Kubernetes上部署Flask应用程序 (Step 2 — Deploying the Flask App on Kubernetes)
The app and Docker image described in the previous step have already been built and made publicly available in the flask-helloworld Docker Hub repository. You can optionally create your own repository for the app and substitute it for flask-helloworld
throughout this step.
上一步中描述的应用程序和Docker镜像已经构建,并在flask-helloworld Docker Hub存储库中公开可用。 您可以选择为应用创建自己的存储库,并在整个步骤中将其替换为flask-helloworld
。
We’ll first deploy this demo “Hello World” app into our cluster as a standalone Pod, then as a multi-pod Deployment, which we’ll finally expose as a LoadBalancer Service. At the end of this tutorial, the “Hello World” app will be publicly accessible from outside of the Kubernetes cluster.
我们首先将这个演示的“ Hello World”应用程序作为独立的Pod部署到我们的集群中,然后作为多容器部署,最后将其作为LoadBalancer Service公开。 在本教程结束时,可以从Kubernetes集群外部公开访问“ Hello World”应用程序。
Before we launch any workloads into the cluster, we’ll create a Namespace in which the objects will run. Namespaces allow you to segment your cluster and limit scope for running workloads.
在将任何工作负载启动到集群之前,我们将创建一个将在其中运行对象的命名空间 。 命名空间使您可以对集群进行分段,并限制正在运行的工作负载的范围。
Create a Namespace called flask
:
创建一个名为flask
的命名空间:
kubectl create namespace flask
kubectl创建名称空间flask
Now, list all the Namespaces in your cluster:
现在,列出集群中的所有命名空间:
- kubectl get namespace kubectl获取名称空间
You should see your new Namespace as well as some default Namespaces like kube-system
and default
. In this tutorial, we are going to exclusively work within the flask
Namespace.
您应该看到新的命名空间以及一些默认的命名空间,例如kube-system
和default
。 在本教程中,我们将仅在flask
命名空间内工作。
Navigate back out to the k8s
directory in the demo repo:
导航回到演示k8s
中的k8s
目录:
- cd ../k8s cd ../k8s
In this directory, you’ll see three Kubernetes manifest files:
在此目录中,您将看到三个Kubernetes清单文件:
flask-pod.yaml
: The app Pod manifestflask-pod.yaml
:应用程序Pod清单flask-deployment.yaml
: The app Deployment manifestflask-deployment.yaml
:应用程序部署清单flask-service.yaml
: The app LoadBalancer Service manifestflask-service.yaml
:应用程序LoadBalancer服务清单
Let’s take a look at the Pod manifest:
让我们看一下Pod清单:
- cat flask-pod.yaml 猫烧瓶-pod.yaml
Output
apiVersion: v1
kind: Pod
metadata:
name: flask-pod
labels:
app: flask-helloworld
spec:
containers:
- name: flask
image: hjdo/flask-helloworld:latest
ports:
- containerPort: 5000
Here, we define a minimal Pod called flask-pod
and label it with the app: flask-helloworld
key-value pair.
在这里,我们定义了一个最小的Pod,称为flask-pod
,并使用以下app: flask-helloworld
其标记为app: flask-helloworld
键值对。
We then name the single container flask
and set the image to flask-helloworld:latest
from the hjdo/flask-helloworld
Docker Hub repository. If you’re using an image stored in a different Docker Hub repo, you can reference it using the image
field here. Finally, we open up port 5000
to accept incoming connections.
然后,我们命名单个容器flask
,并将hjdo/flask-helloworld
Docker Hub存储库hjdo/flask-helloworld
的映像设置为flask-helloworld:latest
。 如果您正在使用存储在其他Docker Hub存储库中的映像,则可以在此处使用image
字段引用它。 最后,我们打开端口5000
以接受传入的连接。
Deploy this Pod into the flask
Namespace using kubectl apply -f
and the -n
Namespace flag:
使用kubectl apply -f
和-n
Namespace标志将此Pod部署到flask
命名空间中:
- kubectl apply -f flask-pod.yaml -n flask kubectl apply -f flask-pod.yaml -n flask
After ten or so seconds, the Pod should be up and running in your cluster:
大约十秒钟后,Pod应该已启动并在您的集群中运行:
- kubectl get pod -n flask kubectl get pod -n烧瓶
Output
NAME READY STATUS RESTARTS AGE
flask-pod 1/1 Running 0 4s
Since this Pod is running inside of the Kubernetes cluster, we need to forward a local port to the Pod’s containerPort
to access the running app locally:
由于此Pod在Kubernetes集群内部运行,因此我们需要将本地端口转发到Pod的containerPort
以本地访问正在运行的应用程序:
- kubectl port-forward pods/flask-pod -n flask 5000:5000 kubectl前移式豆荚/烧瓶-n烧瓶5000:5000
Here we use port-forward
to forward local port 5000
to the Pod’s containerPort
5000
.
在这里,我们使用port-forward
将本地端口5000
port-forward
到Pod的containerPort
5000
。
Navigate to http://localhost:5000
, where you should once again see the “Hello World” text generated by the Flask app. If you’re running kubectl
on a remote dev server, replace localhost
with your dev server’s external IP address.
导航到http://localhost:5000
,您将在此处再次看到Flask应用程序生成的“ Hello World”文本。 如果您在远程开发服务器上运行kubectl
localhost
替换为开发服务器的外部IP地址。
Feel free to play around with kubectl
commands like kubectl describe
to explore the Pod resource. When you’re done, delete the Pod using delete
:
可以随意使用kubectl
命令(例如kubectl describe
来探索Pod资源。 完成后,使用delete
删除Pod:
- kubectl delete pod flask-pod -n flask kubectl删除豆荚烧瓶-pod -n flask
Next, we’ll roll out this Pod in a scalable fashion using the Deployment resource. Print out the contents of the flask-deployment.yaml
manifest file:
接下来,我们将使用Deployment资源以可扩展的方式推出此Pod。 打印出flask-deployment.yaml
清单文件的内容:
- cat flask-deployment.yaml 猫瓶部署
Output
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-dep
labels:
app: flask-helloworld
spec:
replicas: 2
selector:
matchLabels:
app: flask-helloworld
template:
metadata:
labels:
app: flask-helloworld
spec:
containers:
- name: flask
image: hjdo/flask-helloworld:latest
ports:
- containerPort: 5000
Here, we define a Deployment called flask-dep
with an app: flask-helloworld
Label. Next, we request 2 replicas of a Pod template identical to the template we previously used to deploy the Flask app Pod. The selector
field matches the app: flask-helloworld
Pod template to the Deployment.
在这里,我们定义了一个名为flask-dep
的部署,其中包含一个app: flask-helloworld
标签。 接下来,我们请求Pod模板的2个副本,这些副本与我们之前用于部署Flask应用Pod的模板相同。 selector
字段将app: flask-helloworld
Pod模板与Deployment匹配。
Roll out the Deployment using kubectl apply -f
:
使用kubectl apply -f
展开部署:
- kubectl apply -f flask-deployment.yaml -n flask kubectl apply -f flask-deployment.yaml -n烧瓶
After a brief moment, the Deployment should be up and running in your cluster:
片刻之后,应在群集中启动并运行部署:
- kubectl get deploy -n flask kubectl获取部署-n烧瓶
Output
NAME READY UP-TO-DATE AVAILABLE AGE
flask-dep 2/2 2 2 5s
You can also pull up the individual Pods that are managed by the Deployment controller:
您还可以拉出由Deployment控制器管理的各个Pod:
- kubectl get pod -n flask kubectl get pod -n烧瓶
Output
NAME READY STATUS RESTARTS AGE
flask-dep-876bd7677-bl4lg 1/1 Running 0 76s
flask-dep-876bd7677-jbfpb 1/1 Running 0 76s
To access the app, we have to forward a port inside of the cluster:
要访问该应用程序,我们必须转发集群内部的端口:
- kubectl port-forward deployment/flask-dep -n flask 5000:5000 kubectl port-forward部署/ flask-dep -n flask 5000:5000
This will forward local port 5000
to containerPort 5000
on one of the running Pods.
这会将本地端口5000
转发到正在运行的Pod之一上的containerPort 5000
。
You should be able to access the app at http://localhost:5000
. If you’re running kubectl
on a remote dev server, replace localhost
with your dev server’s external IP address.
您应该可以通过http://localhost:5000
访问该应用程序。 如果您在远程开发服务器上运行kubectl
localhost
替换为开发服务器的外部IP地址。
At this point you can play around with commands like kubectl rollout
and kubectl scale
to experiment with rolling back Deployments and scaling them. To learn more about these and other kubectl commands, consult a kubectl Cheat Sheet.
在这一点上,您可以使用诸如kubectl rollout
和kubectl scale
类的命令来尝试回滚Deployment和对其进行缩放。 要了解有关这些和其他kubectl命令的更多信息,请查阅kubectl备忘单 。
In the final step, we’ll expose this app to outside users using the LoadBalancer Service type, which will automatically provision a DigitalOcean cloud Load Balancer for the Flask app Service.
在最后一步中,我们将使用LoadBalancer服务类型将此应用程序暴露给外部用户,该类型将自动为Flask应用程序服务配置DigitalOcean云负载均衡器 。
第3步-创建App服务 (Step 3 — Creating the App Service)
A Kubernetes Deployment allows the operator to flexibly scale a Pod template up or down, as well as manage rollouts and template updates. To create a stable network endpoint for this set of running Pod replicas, you can create a Kubernetes Service, which we’ll do here.
Kubernetes部署使操作员可以灵活地向上或向下缩放Pod模板,以及管理部署和模板更新。 要为这组正在运行的Pod副本创建稳定的网络端点,您可以创建Kubernetes服务,我们将在此处进行操作。
Begin by inspecting the Service manifest file:
首先检查服务清单文件:
- cat flask-service.yaml 猫瓶服务.yaml
Output
apiVersion: v1
kind: Service
metadata:
name: flask-svc
labels:
app: flask-helloworld
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 5000
protocol: TCP
selector:
app: flask-helloworld
This manifest defines a Service called flask-svc
. We set the type to LoadBalancer
to provision a DigitalOcean cloud Load Balancer that will route traffic to the Deployment Pods. To select the already running Deployment, the selector
field is set to the Deployment’s app: flask-helloworld
Label. Finally, we open up port 80
on the Load Balancer and instruct it to route traffic to the Pods’ containerPort 5000
.
此清单定义了一个称为flask-svc
的服务。 我们将类型设置为LoadBalancer
以配置一个DigitalOcean云负载均衡器,它将流量路由到Deployment Pod。 要选择已运行的Deployment,请将selector
字段设置为Deployment的app: flask-helloworld
标签。 最后,我们在负载均衡器上打开端口80
,并指示其将流量路由到Pods的containerPort 5000
。
To create the Service, use kubectl apply -f
:
要创建服务,请使用kubectl apply -f
:
- kubectl apply -f flask-service.yaml -n flask kubectl apply -f flask-service.yaml -n烧瓶
It may take a bit of time for Kubernetes to provision the cloud Load Balancer. You can track progress using the -w
watch flag:
Kubernetes可能需要一些时间来配置云负载均衡器。 您可以使用-w
watch标志跟踪进度:
- kubectl -n flask get svc -w kubectl -n flask get svc -w
Once you see an external IP for the flask-svc
Service, navigate to it using your web browser. You should see the “Hello World” Flask app page.
一旦看到flask-svc
服务的外部IP,请使用Web浏览器导航至该IP。 您应该会看到“ Hello World”烧瓶应用程序页面。
结论 (Conclusion)
This brief tutorial demonstrates how to containerize a minimal Flask app and deploy it to a Kubernetes cluster. It accompanies the meetup kit’s slides and speaker notes and GitHub repository.
这个简短的教程演示了如何容器化一个最小的Flask应用程序并将其部署到Kubernetes集群。 它与metup套件的幻灯片和演讲者注释以及GitHub存储库一起提供 。
更多推荐
所有评论(0)