kubernetes 为什么不通过服务请求和入口请求向部署发出工作请求?

kkih6yb8  于 2023-02-15  发布在  Kubernetes
关注(0)|答案(1)|浏览(137)

安装minikube版本:版本1.29.0,适用于MacOs。
我在flask上创建API端点并构建Docker映像

FROM debian:latest

COPY . /app
WORKDIR /app
RUN pip3 install --no-cache-dir -r requirements.txt

CMD ["uwsgi", "--socket", "0.0.0.0:5001", "--protocol=http", "-w", "wsgi:app", "--ini", "wsgi.ini"]

将Docker映像加载到minikube后

minikube image load drnoreg/devops_blog:0.0.1

检查小型库贝

% minikube image ls 

docker.io/drnoreg/devops_blog:0.0.1

创建部署、服务和入口yaml

    • 应用程序yaml**
apiVersion: apps/v1
kind: Deployment
metadata:
  name: devops-blog
spec:
  selector:
    matchLabels:
      run: devops-blog
  replicas: 1
  template:
    metadata:
      labels:
        run: devops-blog
    spec:
      containers:
        - name: devops-blog
          image: docker.io/drnoreg/devops_blog:0.0.1
          ports:
            - name: pod-port
              containerPort: 5001
---
apiVersion: v1
kind: Service
metadata:
  name: devops-blog
  labels:
    run: devops-blog
spec:
  type: NodePort
  ports:
    - name: pod-port
      port: 5001
      targetPort: 5001
      protocol: TCP
      nodePort: 30001
  selector:
    run: devops-blog
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: devops-blog
  namespace: devops-blog
spec:
  rules:
    - host: devops-blog.cluster.local
      http:
        paths:
          - pathType: ImplementationSpecific
            backend:
              service:
                name: devops-blog
                port:
                  number: 5001

执行创建命名空间

kubectl create namespace devops-blog

设置当前名称空间

kubectl config set-context --current --namespace=devops-blog

并创建部署、服务和入口

kubectl create -f app.yaml

尝试后检查工作文件夹API的转发端口

kubectl port-forward devops-blog-f666d8cd7-njp95  5001:5001
Forwarding from 127.0.0.1:5001 -> 5001
Forwarding from [::1]:5001 -> 5001
Handling connection for 5001
Handling connection for 5001

minikube中的 flask API服务正在运行

% kubectl get service -n devops-blog -o wide
NAME          TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE   SELECTOR
devops-blog   NodePort   10.99.37.126   <none>        5001:30001/TCP   45s   run=devops-blog
% kubectl get pod -n devops-blog -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
devops-blog-f666d8cd7-b9n7j   1/1     Running   0          57s   10.244.0.34   minikube   <none>           <none>
% kubectl get node -n devops-blog -o wide
NAME       STATUS   ROLES           AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
minikube   Ready    control-plane   16h   v1.26.1   192.168.49.2   <none>        Ubuntu 20.04.5 LTS   5.10.47-linuxkit   docker://20.10.23

现在我尝试通过minikube服务检查API是否正常工作

% telnet 192.168.49.2 30001
Trying 192.168.49.2...

不工作
添加到/etc/hosts

127.0.0.1 devops-blog.cluster.local

尝试通过入口minikube检查工作API

% telnet devops-blog.cluster.local 80   
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

也不管用。
为什么不能通过服务请求和入口请求将工作请求发送到部署?如何解决这个问题?

rhfm7lfc

rhfm7lfc1#

如果您没有启用入口插件,请尝试通过执行以下命令启用它

$ minikube addons enable ingress

尝试使用clusterIP服务代替NodePort服务,在创建入口时,您可以给予此服务作为后端,如下所示

服务.yaml

apiVersion: v1
kind: Service
metadata:
  name: devops-blog
  labels:
    run: devops-blog
spec:
  type: ClusterIP
  ports:
    - name: pod-port
      port: 5001
      targetPort: 5001
      protocol: TCP
  selector:
    run: devops-blog

进口产品

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: devops-blog
  namespace: devops-blog
  annotations:
   kubernetes.io/ingress.class: "nginx"
   nginx.ingress.kubernetes.io/ssl-redirect: "false" #Since you are using localhost
spec:
  rules:
    - host: devops-blog.cluster.local
      http:
        paths:
          - pathType: ImplementationSpecific
            backend:
              service:
                name: devops-blog
                port:
                  number: 5001
            path: /

生成入口后,IP尝试使用http://devops-blog.cluster.local/在本地浏览器中打开它,或像curl $curl http://devops-blog.cluster.local/那样 curl 它。

注意:如果要在云中部署此应用程序,请尝试将LoadBalancer作为服务。

按照详细说明尝试使用tutorial

相关问题