将外部IP分配给Kubernetes服务 [英] Assign External IP to a Kubernetes Service
问题描述
编辑:我的设置的重点是(如果可能的话)实现以下目标:
The whole point of my setup is to achieve (if possible) the following :
- 我有多个k8s节点
- 当我(通过公司网络)联系IP地址时,应该将其路由到我的容器/pod/服务/其他任何一个地址.
- 我应该能够轻松地设置该IP(就像在我的服务.yml定义中一样)
我正在运行一个小的Kubernetes集群(使用kubeadm构建),以评估是否可以将Docker(旧)Swarm设置移至k8s.我绝对需要的功能是能够为容器分配IP,就像我使用MacVlan一样.
I'm running a small Kubernetes cluster (built with kubeadm) in order to evaluate if I can move my Docker (old)Swarm setup to k8s. The feature I absolutely need is the ability to assign IP to containers, like I do with MacVlan.
在当前的docker设置中,我正在使用MacVlan将公司网络中的IP地址分配给某些容器,以便可以直接访问(而不使用反向代理),就像它是任何物理服务器一样.我正在尝试通过k8s实现类似的功能.
In my current docker setup, I'm using MacVlan to assign IP addresses from my company's network to some containers so I can reach directly (without reverse-proxy) like if it's any physical server. I'm trying to achieve something similar with k8s.
我发现:
- 我必须使用 Service
- 我不能使用 LoadBalancer 类型,因为它仅适用于兼容的云提供商(例如GCE或AWS).
- 我应该使用 ExternalIPs
- 入口资源是某种反向代理吗?
- I have to use Service
- I can't use the LoadBalancer type, as it's only for compatible cloud providers (like GCE or AWS).
- I should use ExternalIPs
- Ingress Resources are some kind of reverse proxy ?
我的Yaml文件是:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
nodeSelector:
kubernetes.io/hostname: k8s-slave-3
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service
spec:
type: ClusterIP
selector:
app: nginx
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
externalIPs:
- A.B.C.D
我希望我的服务获得IP A.B.C.D(这是我公司的网络之一).我的部署正在正常进行,因为我可以使用它的 ClusterIP 从k8s集群中访问我的nginx容器.
I was hopping that my service would get the IP A.B.C.D (which is one of my company's network). My deployment is working as I can reach my nginx container from inside the k8s cluster using it's ClusterIP.
我想念什么?或者至少,在哪里可以找到有关网络流量的信息以查看数据包是否到来?
What am I missing ? Or at least, where can I find informations on my network traffic in order to see if packets are coming ?
编辑:
$ kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443/TCP 6d
nginx-service 10.102.64.83 A.B.C.D 80/TCP 23h
谢谢.
推荐答案
如果这仅用于测试,请尝试
If this is just for testing, then try
kubectl port-forward service/nginx-service 80:80
那么你可以
curl http://localhost:80
这篇关于将外部IP分配给Kubernetes服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!