![](/img/trans.png)
[英]How to use static External IP for Azure kubernetes LoadBalancer?
[英]Azure kubernetes service loadbalancer external IP not accessible
我是 Kubernetes 世界的新手,正在测试示例 Django“Hello world”应用程序部署。 使用 docker-compose 我能够在浏览器上访问地狱世界页面,但我需要使用 Kubernetes。 所以我测试了两个选项,但都没有奏效。 1) 我创建了一个 Azure CICD 管道来使用以下 Dockerfile 在 ACR 中构建和推送映像,
FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir /hello_world
WORKDIR /hello_world
COPY . /hello_world/
RUN pip install -r requirements.txt
CMD [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]
管道成功完成并将图像上传到存储库中。
现在我使用 kubectl 使用部署文件进行部署,
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-helloworld
spec:
replicas: 3
selector:
matchLabels:
app: django-helloworld
template:
metadata:
labels:
app: django-helloworld
spec:
containers:
- name: django-helloworld
image: acrshgpdev1.azurecr.io/django-helloworld:194
#imagePullPolicy: Always
ports:
- containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
name: django-helloworld-service
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: django-helloworld
部署和服务已创建,但是当我尝试通过浏览器访问 LB 服务的外部 IP 时,该页面无法访问。 我使用了外部 ip:port,但它不起作用。 任何想法为什么会发生这种情况?
2)我使用了相同的 Dockerfile 但使用了不同的部署文件(将映像更改为本地创建的映像并删除了 LB 服务)将应用程序部署到我的本地 Kubernetes。 部署文件如下,
apiVersion: v1
kind: Service
metadata:
name: django-helloworld-service
spec:
selector:
app: django-helloworld
ports:
- protocol: TCP
port: 80
targetPort: 30800
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-helloworld
spec:
replicas: 3
selector:
matchLabels:
app: django-helloworld
template:
metadata:
labels:
app: django-helloworld
spec:
containers:
- name: django-helloworld
image: django-helloworld:1.0
#imagePullPolicy: Always
ports:
- containerPort: 8000
它会创建部署和服务,但不会为 NodePort 服务分配外部 IP,因此我无法确定应该选择什么服务来测试应用程序是否成功。 我知道我不能选择 LB,因为它不在本地,我需要使用云服务进行部署。
只需将您的服务配置为LoadBalancer
类型并进行适当的端口映射:
apiVersion: v1
kind: Service
metadata:
name: django-helloworld-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8000
selector:
app: django-helloworld
https://kubernetes.io/docs/concepts/services-networking/service/
确保部署也关联了健康的 pod(它们显示为Running
并且名称旁边带有1/1
)。 如果没有,请确保您的集群可以成功从acrshgpdev1.azurecr.io
注册表中拉取; 您可以按照本文直接将 AKS 集群与 ACR 注册表集成:
az aks update -n myAKSCluster -g myResourceGroup --attach-acr acrshgpdev1.azurecr.io
或者通过手动将 AKS 群集的 SP 添加到 ACR 上的Reader
角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.