OS名称 | Microsoft Windows 11 家庭中文版 |
---|---|
系统类型 | x64-based PC |
Docker版本 | Docker version 24.0.6, build ed223bc |
minikube版本 | v1.32.0 |
#基于fastapi快速创建一个项目
rkun1@LAPTOP-TUS5FU0D MINGW64 /
$ mkdir k8s-app
rkun1@LAPTOP-TUS5FU0D MINGW64 /
$ cd k8s-app/
#创建虚拟环境
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ python -m venv ./venv
#激活
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ source ./venv/Scripts/Activate
#安装库
(venv)
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ pip install fastapi
(venv)
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ pip install uvicorn #web服务器
#记录依赖库
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ pip freeze >> requirements.txt
https://fastapi.tiangolo.com/#create-it
参考官方文档:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ vim main.py
main.py
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
启动应用:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['C:\\Serve\\Git\\k8s-app']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [10408] using StatReload
INFO: Started server process [7256]
INFO: Waiting for application startup.
INFO: Application startup complete.
k8s可以运行容器,所以我们要将应用容器化
Dockerfile文件
# python基础镜像
FROM python:3.9
# 设置当前工作目录到/code 我们将requirements.txt和app目录放置在这
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
# 安装依赖
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# 复制
COPY ./app /code/app
# 运行
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
查看下目录结构:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ tree -L 1
.
|-- Dockerfile
|-- app
|-- requirements.txt
`-- venv
制作docker image:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ docker build -t k8s-fast-api .
测试运行一下:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ docker run -p 8000:80 k8s-fast-api
INFO: Started server process [1]
INFO: Waiting for application startup.
INFO: Application startup complete.
https://hub.docker.com/
创建一个仓库:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app #打标签
$ docker build -t rkun422/k8s-images-repo:0.0.1 .
#推送
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ docker push rkun422/k8s-images-repo:0.0.1
https://dashboard.civo.com/login
可以在云上部署Kubernetes集群,也可以在本地操作
这里我使用Windows本地搭建的minikube
https://minikube.sigs.k8s.io/docs/
#创建目录 定义k8s集群资源在这个目录下
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app
$ mkdir kubernetes
https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/deployment/
在部署之前首先要创建集群拉取镜像时的Secret
具体请参考:
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-secret-by-providing-credentials-on-the-command-line
deployment.yml
注意镜像仓库是你自己指定的
apiVersion: apps/v1
kind: Deployment
metadata:
name: fast-api
labels:
app: fast-api
spec:
replicas: 3
selector:
matchLabels:
app: fast-api
template:
metadata:
labels:
app: fast-api
spec:
containers:
- name: fast-api
image: rkun422/k8s-images-repo:0.0.1
ports:
- containerPort: 80
imagePullSecrets:
- name: repo-key #你创建的secret
Servcie可以提供一个固定的地址让我们去访问Pod
https://kubernetes.io/zh-cn/docs/concepts/services-networking/service/
service.yml
apiVersion: v1
kind: Service
metadata:
name: fast-api
spec:
selector:
app: fast-api
ports:
- protocol: TCP
port: 80
targetPort: 80
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app/kubernetes
$ pwd
/k8s-app/kubernetes
(venv)
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app/kubernetes
$ kubectl apply -f ./
查看Pod:
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
fast-api-5cbcbcfc56-b9whd 1/1 Running 0 6m19s
fast-api-5cbcbcfc56-bwf7v 1/1 Running 0 6m19s
fast-api-5cbcbcfc56-j9zw5 1/1 Running 0 6m19s
查看Service:
rkun1@LAPTOP-TUS5FU0D MINGW64 /k8s-app/kubernetes
$ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
fast-api ClusterIP 10.108.66.181 <none> 80/TCP 7m8s app=fast-api
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10d <none>
对一个Pod进行端口转发:
$ kubectl port-forward pods/fast-api-5cbcbcfc56-b9whd 80:80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
Handling connection for 80
Handling connection for 80
rkun1@LAPTOP-TUS5FU0D MINGW64 ~
$ minikube service fast-api --url
W1121 22:07:50.931444 22028 main.go:291] Unable to resolve the current Docker CLI context "default": context "default": context not found: open C:\Users\rkun1\.docker\contexts\meta\37a8eec1ce19687d132fe29051dca629d164e2c4958ba141d5f4133a33f0688f\meta.json: The system cannot find the path specified.
service default/fast-api 没有 NodePort
http://127.0.0.1:55001
❗ 因为你正在使用 windows 上的 Docker 驱动程序,所以需要打开终端才能运行它。