繁体   English   中英

如何在Kubernetes中为容器分配外部IP地址?

[英]How to assign an external IP address to a container in Kubernetes?

我是Kubernetes的新手,但能够使用Kubespray轻松创建自己的Kubernetes集群。 此外,我使自己熟悉Kubernete的术语/概念,因此能够创建示例Pod / Deployment,该示例可以按预期运行。 不幸的是,我无法从公司内部网内部的外部网络访问我的容器。 我不允许发布实际的IP地址,因此我将使用虚拟47.11.xy内部网

我的集群包含3个裸机节点:

Master (47.11.91.155)
Node1  (47.11.91.97)
Node2  (47.11.91.98)

此外,我拥有要用于访问my-example应用程序的其他Intranet IP地址47.11.91.101 我在官方文档以及其他SO文章中尝试了几种组合形式的命令,但是只能使用以下命令将应用程序的端口4711转发到我的本地工作站:

kubectl port-forward my-example-67795fd77d-mkrhw  4711:4711

如果之后至少执行一次nc localhost 4711来证明我从根本上正确设置了我的东西,则此方法很好。 该应用程序成功地将我来自nc的STDIN的输入写入节点文件系统上已挂载的文件/my-data/my-data.txt ),并能够从我的私有Docker中提取我的自定义Docker映像注册表( 47.11.91.42 ),因此也位于Intranet上。

您能否解释一下将my-example连接到官方外部Intranet IP地址47.11.91.101以便我可以使用以下方式访问my-example

nc 47.11.91.101 4711

我的定义文件如下所示:

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

它是使用kubectl create -f my-example-deployment.yml

如果您需要更多信息,请告诉我。 提前致谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM