Mac 平台下使用orbstack 中的k8s 部署flask 应用

安装

本文使用orbstack 中的minikube

也可以是用在线的k8s playground

1
https://labs.play-with-k8s.com/

部署应用

kubectl 基础知识

kubectl 命令的常见格式是:kubectl 操作资源

这会对指定的资源(类似 node 或 deployment)执行指定的操作(类似 create、describe 或 delete)。 你可以在子命令之后使用 –help 获取可能参数相关的更多信息(例如:kubectl get nodes –help)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 kubectl version --output=json
{
"clientVersion": {
"major": "1",
"minor": "27",
"gitVersion": "v1.27.4",
"gitCommit": "fa3d7990104d7c1f16943a67f11b154b71f6a132",
"gitTreeState": "clean",
"buildDate": "2023-07-19T12:20:54Z",
"goVersion": "go1.20.6",
"compiler": "gc",
"platform": "darwin/arm64"
},
"kustomizeVersion": "v5.0.1",
"serverVersion": {
"major": "1",
"minor": "27",
"gitVersion": "v1.27.4+orb1",
"gitCommit": "40f54c2c6d383b1de832c2bc95f0966e047f7e7b",
"gitTreeState": "dirty",
"buildDate": "2023-08-29T06:23:17Z",
"goVersion": "go1.20.4",
"compiler": "gc",
"platform": "linux/arm64"
}
}

kubectl get nodes

查看Node 节点

1
2
3
kubectl get nodes
NAME STATUS ROLES AGE VERSION
orbstack Ready control-plane,master 58d v1.27.4+orb1

部署应用

1
2
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
# deployment.apps/kubernetes-bootcamp created

由于网络问题暂时使用hhitzhl/kubernetes-bootcamp 代替

1
2
3
docker pull hhitzhl/kubernetes-bootcamp
kubectl delete deployments kubernetes-bootcamp
kubectl create deployment kubernetes-bootcamp --image=hhitzhl/kubernetes-bootcamp

列出**deployments**

kubectl get deployments

1
2
3
❯ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 0/1 1 0 2m19s
1
2
3
❯ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 20s

K8s node top 报错error: Metrics API not available

原文

1
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

安装 kuboard

https://kuboard.cn/install/install-dashboard.html#%E5%AE%89%E8%A3%85

访问kuboard

安装Habor

1
2
3
4
5
6
7
docker tag goharbor/harbor-exporter:v2.9.4 127.0.0.1:80/library/goharbor/harbor-exporter:v2.9.4

docker push 127.0.0.1:80/library/goharbor/harbor-exporter:v2.9.4




简单项目部署

项目目录

1
2
3
4
5
├── app.py
├── dockerfile
├── harbor
├── k8s
└── requirements.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# app.py
from flask import Flask, jsonify, abort

app = Flask(__name__)


@app.route('/')
def get():
resp = jsonify({"message": "OK"})
resp.status_code = 200
return resp


if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)

requirements.txt

1
Flask==2.1.1

Dockerfile

1
2
3
4
5
6
7
8
9
FROM python:alpine3.19 as Builder

WORKDIR /app
ADD app.py /app
ADD ./requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

EXPOSE 8080
CMD ["python app.py"]
1
docker buildx build -t k8s_flask_demo:v1 .
1
2
3
4
5
6
7
docker tag k8s_flask_demo:v1 127.0.0.1:80/library/k8s_flask_demo:v1 # 打包docker 镜像
docker push 127.0.0.1:80/library/k8s_flask_demo:v1 # docker images



docker tag k8s_flask_demo:v3 127.0.0.1:80/library/k8s_flask_demo:v3 # 打包docker 镜像
docker push 127.0.0.1:80/library/k8s_flask_demo:v3 # docker images
1


Mac 平台下使用orbstack 中的k8s 部署flask 应用
https://kingjem.github.io/2024/10/14/Mac 平台下使用orbstack 中的k8s 部署flask 应用/
作者
Ruhai
发布于
2024年10月14日
许可协议