【问题标题】:kubernetes redirecting outgoing http traffic from the service to localhost:portkubernetes 将来自服务的传出 http 流量重定向到 localhost:port
【发布时间】:2018-05-23 06:46:58
【问题描述】:

我在两个容器中有一个图表:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: catalog
  labels:
    app: catalog
    chart: catalog-0.1.0
    heritage: Tiller
spec:
  replicas: 1
  selector:
    matchLabels:
      app: catalog
  template:
    metadata:
      labels:
        app: catalog
    spec:
      containers:
        - name: catalog
          image: catalog:v1
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
        - name: myproxy
          image: myproxy:v1
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8008
              protocol: TCP
          env:
            - name: PROXY_PORT
              value: '8080'
---
apiVersion: v1
kind: Service
metadata:
  name: catalog
  labels:
    app: catalog
    chart: catalog-0.1.0
    heritage: Tiller
spec:
  type: NodePort
  ports:
    - port: 8008
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app: catalog

我需要通过 localhost 将目录容器中的所有出站流量重定向到 myproxy 容器。

并且已经在容器中判断目录是否可以发送请求,记录它们等

请提示是否可以使用kubernetes实现。

谢谢。


更新:

问题是我无法更改 catalg 容器中的代码并将查询发送到 localhost

容器也没有 iptables 来做这样的事情

containers:
    - name: catalog
      image: catalog:v1
      imagePullPolicy: IfNotPresent
      command:
        - 'iptables -t nat -A OUTPUT -p tcp --dport 8080 -j DNAT --to-destination 127.0.0.1:8008'
      ports:
        - name: http
          containerPort: 8080
          protocol: TCP

理想情况下使用 Kubernetes 完成

【问题讨论】:

    标签: docker kubernetes devops google-kubernetes-engine


    【解决方案1】:

    如果目录应用程序尊重http_proxy 环境变量,这很容易。只需将环境变量添加到目录容器即可。

        - name: catalog
          image: catalog:v1
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          env:
          - name: HTTP_PROXY
            value: localhost:8008
    

    【讨论】:

      【解决方案2】:

      对于你的更新,如果你需要操作iptables,你可以再添加一个initContainer,例如:

        initContainers:
        - image: centos
          imagePullPolicy: Always
          name: run-iptables
          securityContext:
            privileged: true
          command:
          - "sh"
          - "-c"
          - 'yum -y install iptables; iptables -t nat -A OUTPUT -p tcp --dport 8080 -j DNAT --to-destination 127.0.0.1:8008'
      

      由于一个 pod 中的所有容器共享同一个网络命名空间,它也会影响到目录容器。

      【讨论】:

      猜你喜欢
      • 2018-01-02
      • 1970-01-01
      • 2012-10-19
      • 2021-05-04
      • 2013-07-11
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 2019-12-28
      相关资源
      最近更新 更多