我是Kubernetes Realm 的新手,并且正在测试示例Django“Hello world”应用程序部署。使用docker-compose我可以在浏览器上访问 hell 世界页面,但是我需要使用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

它创建了部署和服务,但没有将外部IP分配给NodePort服务,因此我无法确定应该选择哪种服务来测试应用程序是否成功。我知道我不能选择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/

10-07 19:15
查看更多