kubernetes - 如何在 BareMetal -KinD 中使用 LoadBalancer?
问题描述
我的问题非常简单,它与名为 KinD 的 kubernetes 很棒的工具有关。我正在为我的烧瓶应用程序使用 KindD:
import os
import requests
from flask import Flask
from jaeger_client import Config
from flask_opentracing import FlaskTracing
app = Flask(__name__)
config = Config(
config={
'sampler':
{'type': 'const',
'param': 1},
'logging': True,
'reporter_batch_size': 1,},
service_name="service")
jaeger_tracer = config.initialize_tracer()
tracing = FlaskTracing(jaeger_tracer, True, app)
def get_counter(counter_endpoint):
counter_response = requests.get(counter_endpoint)
return counter_response.text
def increase_counter(counter_endpoint):
counter_response = requests.post(counter_endpoint)
return counter_response.text
@app.route('/')
def hello_world():
counter_service = os.environ.get('COUNTER_ENDPOINT', default="https://localhost:5000")
counter_endpoint = f'{counter_service}/api/counter'
counter = get_counter(counter_endpoint)
increase_counter(counter_endpoint)
return f"""Hello, World!
You're visitor number {counter} in here!\n\n"""
FROM python:3.7-alpine
RUN mkdir /app
RUN apk add --no-cache py3-pip python3 && \
pip3 install flask Flask-Opentracing jaeger-client
WORKDIR /app
ADD ./app /app/
ADD ./requirements.txt /app/
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "/app/main.py"]
部署.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-flask-deployment
spec:
selector:
matchLabels:
app: my-flask-pod
replicas: 2
template:
metadata:
labels:
app: my-flask-pod
spec:
containers:
- name: my-flask-container
image: yusufkaratoprak/awsflaskeks:latest
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 5000
服务.yaml
apiVersion: v1
kind: Service
metadata:
name: my-flask-service
spec:
selector:
app: my-flask-pod
ports:
- port: 6000
targetPort: 5000
type: LoadBalancer
status:
loadBalancer:
ingress:
- ip: 203.0.113.10
配置映射.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 172.42.42.100-172.42.42.105 #Update this with your Nodes IP range
结果:
一切看起来都很好。当我编写浏览器时:http: //172.42.42.101 :6000/
结果:
并且我还想添加@Kaan Mersin 建议:我添加了 80 作为默认端口。
解决方案
实际上,对于裸机服务器,您应该使用 LoadBalancer 以外的其他解决方案来公开您的应用程序,例如 NodePort、Ingress 等。因为 LoadBalancer 服务类型仅适用于 Google、Azure、AWS 等云提供商。
遵循以下方式,认为您使用的是 NodePort 而不是 LoadBalancer,NodePort 服务类型也会在 Pod 之间进行负载平衡流量,如果您只期待平衡功能;
您需要从运行容器的主机外部使用http://172.42.42.101:30160 。端口 6000 只能在集群内部使用内部 IP 访问(在您的情况下为 10.96.244.204)。每当您公开您的部署时,会自动(也可以手动定义)其中一个 NodePort(默认情况下在 30000 - 32767 之间)分配给服务以进行外部请求。
有关服务详细信息,您需要运行以下命令。命令输出将为您提供 NodePort 和其他详细信息。
kubectl describe services my-service
请查看相关的 Kubernetes文档
推荐阅读
- mysql - 在表中创建一个数据类型,该数据类型是对其他表的引用列表
- python - 可能的 django 竞争条件
- django - Django 表单 - 引发 ValidationError 时我可以返回任何变量吗
- javascript - 我的页面如何在外部站点上填写用户名和密码?
- c++ - KNN字符识别读取数字的对角线
- google-cloud-platform - 谷歌是否存储通过谷歌 DLP API 发送的请求
- python - 如何使用 Gmail API 创建只允许发送电子邮件的 client_secret.json?
- android - 如何在 WhatsApp 中同时分享图片和文字
- python - 如何使 form.py 中的电子邮件字段成为必填字段?
- hyperledger-fabric - Allow participants to view transactions they submitted