[英]How to assign an external IP address to a container in Kubernetes?
I'm new to Kubernetes but was able to easily create my own Kubernetes cluster using Kubespray . 我是Kubernetes的新手,但能够使用Kubespray轻松创建自己的Kubernetes集群。 Furthermore I made myself familiar with Kubernete's terminology/concepts and therefore was able to create an example Pod/Deployment which runs as expected. 此外,我使自己熟悉Kubernete的术语/概念,因此能够创建示例Pod / Deployment,该示例可以按预期运行。 Unfortunately I'm not able to access my Containers from the external network which is within my company's intranet. 不幸的是,我无法从公司内部网内部的外部网络访问我的容器。 I'm not allowed to post the actual IP addresses so I'll use a fictive intranet of 47.11.xy 我不允许发布实际的IP地址,因此我将使用虚拟的47.11.xy内部网
My cluster consists of 3 bare metal nodes: 我的集群包含3个裸机节点:
Master (47.11.91.155)
Node1 (47.11.91.97)
Node2 (47.11.91.98)
Furthermore I own the additional intranet IP address 47.11.91.101
which I want to use in order to access my-example
application. 此外,我拥有要用于访问my-example
应用程序的其他Intranet IP地址47.11.91.101
。 I tried several commands in various combinations which I found in the official docs as well as other SO articles but was only able to forward the application's port 4711 to my local workstation using 我在官方文档以及其他SO文章中尝试了几种组合形式的命令,但是只能使用以下命令将应用程序的端口4711转发到我的本地工作站:
kubectl port-forward my-example-67795fd77d-mkrhw 4711:4711
This works fine if I do an nc localhost 4711
afterwards at least to prove that I fundamentally set up my stuff "correctly". 如果之后至少执行一次nc localhost 4711
来证明我从根本上正确设置了我的东西,则此方法很好。 The application successfully writes my input from nc's STDIN to the mounted file /my-data/my-data.txt
(/tmp/my-data.txt) on the node's filesystem and is able to pull my custom Docker image from my private Docker registry ( 47.11.91.42
) which therefore is located on the intranet as well. 该应用程序成功地将我来自nc的STDIN的输入写入节点文件系统上已挂载的文件/my-data/my-data.txt
),并能够从我的私有Docker中提取我的自定义Docker映像注册表( 47.11.91.42
),因此也位于Intranet上。
Could you please explain me what I would have to do in order to connect my-example
with the official external intranet IP address 47.11.91.101
so I can access my-example
using something like this: 您能否解释一下将my-example
连接到官方外部Intranet IP地址47.11.91.101
以便我可以使用以下方式访问my-example
:
nc 47.11.91.101 4711
My definition file looks like this: 我的定义文件如下所示:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: my-example
labels:
app: my-example
spec:
replicas: 1
template:
metadata:
name: my-example
labels:
app: my-example
spec:
containers:
- name: my-example
image: my.private.docker.registry:5002/my-example:latest
imagePullPolicy: IfNotPresent
command: ["echo", "The example is working correctly within Kubernetes."]
- name: my-example-port
image: my.private.docker.registry:5002/my-example:latest
imagePullPolicy: Always
ports:
- name: myport
containerPort: 4711
resources:
requests:
cpu: 512m
memory: 512Mi
command: ["/bin/bash","-c","nc -k -l 4711 > /my-data/my-data.txt"]
volumeMounts:
- mountPath: /my-data
name: data
volumes:
- name: data
hostPath:
path: /tmp
type: Directory
imagePullSecrets:
- name: my-priavte-docker-secrets
---
apiVersion: v1
kind: Service
metadata:
name: my-example-service
labels:
app: my-example
spec:
selector:
app: my-example
ports:
- port: 4711
targetPort: 4711
protocol: TCP
externalIPs:
- 47.11.91.101
It's created using kubectl create -f my-example-deployment.yml
. 它是使用kubectl create -f my-example-deployment.yml
。
Please let me know if you need further information. 如果您需要更多信息,请告诉我。 Thanks in advance! 提前致谢!
You can achieve what you want by using a Load Balancer and force it to use your fourth IP 47.11.91.101, if you ware using a supported cloud provider (not sure about your intranet in bare-metal though). 如果您使用受支持的云提供程序(虽然不确定您的Intranet是否为裸机),则可以通过使用负载均衡器来实现所需的功能,并强制其使用第四个IP 47.11.91.101。 This seems to be added by https://github.com/kubernetes/kubernetes/pull/13005 . 这似乎是由https://github.com/kubernetes/kubernetes/pull/13005添加的。
https://kubernetes.io/docs/concepts/services-networking/service/#type-loadbalancer https://kubernetes.io/docs/concepts/services-networking/service/#type-loadbalancer
Some other useful resources: 其他一些有用的资源:
https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/ https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/
https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/ https://kubernetes.io/docs/tutorials/stateless-application/expose-external-ip-address/
https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/ https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.