You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kubernetes中如何让FastAPI通过<host>:<port>访问Redis Deployment?

问题描述

已在Kubernetes中部署Python FastAPI接口并运行正常,配置如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: mynamespace
  name: myapi-deployment
  labels:
    app: myapi-deployment
spec:
  revisionHistoryLimit: 0
  replicas: 1
  selector:
    matchLabels:
      app: myapi
      tier: backend
  template:
    metadata:
      labels:
        app: myapi
        tier: backend
    spec:
      containers:
        - name: myapi
          image: path_to_my_container_repo
          imagePullPolicy: Always
          ports:
            - containerPort: 80
      imagePullSecrets:
        - name: registry-secret

---
apiVersion: v1
kind: Service
metadata:
  name: myapi-service
  namespace: mynamespace
spec:
  selector:
    app: myapi
    tier: backend
  ports:
    - protocol: "TCP"
      port: 80
      targetPort: 80
  type: ClusterIP

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: mynamespace
  name: mynamespace-ingress-all
  annotations:
    ...
spec:
  ingressClassName: nginx
  rules:
  - host: somedomain
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: myapi-service
            port:
              number: 80

添加Redis Deployment后,两个服务无法互相访问,FastAPI连接Redis时报错:Error 111 connecting to localhost:6379. Connection refused。已创建Redis Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: mynamespace
  name: myapiredis-deployment
  labels:
    app: myapiredis-deployment
spec:
  revisionHistoryLimit: 0
  replicas: 1
  selector:
    matchLabels:
      app: myapiredis
  template:
    metadata:
      labels:
        app: myapiredis
    spec:
      containers:
        - name: myapiredis
          image: redis:7.0.6-alpine
          imagePullPolicy: Always
          ports:
            - containerPort: 6379

疑问:能否通过ClusterIP类型的Service实现FastAPI以<host>:<port>形式访问Redis?如何配置?


解决方案

完全可以用ClusterIP类型的Service实现Kubernetes内部服务间的访问,这也是K8s内部服务通信的标准方式,具体步骤如下:

1. 创建Redis的ClusterIP Service

ClusterIP是Kubernetes Service的默认类型,无需显式指定type: ClusterIP。创建以下Service配置文件:

apiVersion: v1
kind: Service
metadata:
  name: myapiredis-service
  namespace: mynamespace
spec:
  selector:
    app: myapiredis  # 匹配Redis Deployment的Pod标签
  ports:
    - protocol: TCP
      port: 6379       # Service对外暴露的端口
      targetPort: 6379 # 对应Redis容器的端口

执行命令应用配置:

kubectl apply -f redis-service.yaml -n mynamespace

2. 修改FastAPI的Redis连接配置

在FastAPI代码中,将Redis连接地址从localhost:6379改为myapiredis-service:6379。

原理说明

Kubernetes内部的DNS服务会自动为每个Service分配一个域名,格式为<service-name>.<namespace>.svc.cluster.local。当两个服务在同一个namespace下时,可以直接使用Service名称作为主机名访问(即myapiredis-service),K8s DNS会自动解析到对应的ClusterIP。

3. 验证连接

重新部署FastAPI应用后,可进入FastAPI容器内部测试Redis连通性:

kubectl exec -it <myapi-pod-name> -n mynamespace -- ping myapiredis-service
# 或直接测试Redis连接
kubectl exec -it <myapi-pod-name> -n mynamespace -- redis-cli -h myapiredis-service ping

如果返回PONG,说明连接正常。


内容的提问来源于stack exchange,提问作者Alfonso_MA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 04:55:15