
在 k8s 中访问无外部 ip 的 loadbalancer service
在 k8s 集群中,当使用 loadbalancer 类型的 service 且没有指定外部 ip 时,您将无法通过该 service 直接从外部访问后端 pod。以下是一些解决方案:
1. 使用 nodeport service
您可以创建另一个类型的 service,即 nodeport service。此类型会自动分配每个节点上的特定端口,您可以通过节点 ip 和 nodeport 来访问后端 pod。
示例:
apiversion: v1
kind: service
metadata:
name: my-nginx-svc
labels:
app: nginx
spec:
type: nodeport
ports:
- port: 80
nodeport: 30080
selector:
app: nginx您可以通过节点 ip 和端口 30080 访问该 service。
2. 使用 metallb 负载均衡器
metallb 是一个为 k8s 集群提供 layer-2 负载均衡解决方案。它可以自动为 loadbalancer service 分配外部 ip。
安装 metallb:
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.10.2/config/manifests/metallb-native.yaml
创建 loadbalancer pool:
apiversion: metallb.io/v1beta1
kind: ippool
metadata:
name: metallb-pool
spec:
addresses:
- 10.123.0.100-10.123.0.199创建 loadbalancer service:
apiVersion: v1
kind: Service
metadata:
name: my-nginx-svc
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx
loadBalancerIP: 10.123.0.1003. 检查解析错误
如果您在使用 curl 从容器中访问外部域名时遇到解析错误,请检查 coredns 配置。c0redns 负责在集群中解析域名。确保上游 dns 服务器配置正确,允许 pod 解析外部域名。
以上就是在 K8s 中如何访问没有外部 IP 的 LoadBalancer Service?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号