代码之家  ›  专栏  ›  技术社区  ›  Ahmad.Masood

Kubernetes:使用容器作为代理

  •  0
  • Ahmad.Masood  · 技术社区  · 3 年前

    我有以下pod设置:

    apiVersion: v1
    kind: Pod
    metadata:
      name: proxy-test
      namespace: test
    spec:
      containers:
        - name: container-a
          image: <Image>
          imagePullPolicy: Always
          ports:
            - name: http-port
              containerPort: 8083
        - name: container-proxy
          image: <Image>
          ports:
            - name: server
              containerPort: 7487
              protocol: TCP
        - name: container-b
          image: <Image>
    

    exec 进入 container-b 并执行以下curl请求:

    curl --proxy localhost:7487 -X POST http://localhost:8083/
    

    由于某种原因, http://localhost:8083/ 直接被调用,代理被忽略。有人能解释为什么会这样吗?

    0 回复  |  直到 3 年前
        1
  •  1
  •   moonkotte    3 年前

    环境

    我在电脑上复制了这个场景 kubeadm GCP GKE kubernetes群集以查看是否有任何差异——不,它们的行为相同,因此我认为AWS EKS也应该有相同的行为。

    我创建了一个包含3个容器的吊舱:

    apiVersion: v1
    kind: Pod
    metadata:
      name: proxy-pod
    spec:
      containers:
      - image: ubuntu # client where connection will go from
        name: ubuntu
        command: ['bash', '-c', 'while true ; do sleep 60; done']
      - name: proxy-container # proxy - that's obvious
        image: ubuntu
        command: ['bash', '-c', 'while true ; do sleep 60; done']
      - name: server # regular nginx server which listens to port 80
        image: nginx
    

    我安装了这个试验台 squid 代理 proxy-container ( what is squid and how to install it ).默认情况下,它侦听端口 3128 .

    以及 curl 安装在 ubuntu -客户端容器。( net-tools 作为奖金,它有 netstat ).

    测验

    笔记

    • 我曾经 127.0.0.1 而不是 localhost 因为 鱿鱼 有一些解决问题,没有找到简单/快速的解决方案。
    • 卷曲 与…连用 -v 冗长的旗帜。

    我们有 proxy 在…上 3128 nginx 在…上 80 在吊舱内:

    # netstat -tulpn
    
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -
    tcp        0      0 0.0.0.0:3128            0.0.0.0:*               LISTEN      -
    tcp6       0      0 :::80                   :::*                    LISTEN      -
    

    卷曲 直接:

    # curl 127.0.0.1 -vI
    
    *   Trying 127.0.0.1:80... # connection goes directly to port 80 which is expected
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > HEAD / HTTP/1.1
    > Host: 127.0.0.1
    > User-Agent: curl/7.68.0
    > Accept: */*
    

    卷曲 通过代理:

    # curl --proxy 127.0.0.1:3128 127.0.0.1:80 -vI
    
    *   Trying 127.0.0.1:3128... # connecting to proxy!
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 3128 (#0) # connected to proxy
    > HEAD http://127.0.0.1:80/ HTTP/1.1 # going further to nginx on `80`
    > Host: 127.0.0.1
    > User-Agent: curl/7.68.0
    > Accept: */*
    

    鱿鱼 日志:

    # cat /var/log/squid/access.log
    
    1635161756.048      1 127.0.0.1 TCP_MISS/200 958 GET http://127.0.0.1/ - HIER_DIRECT/127.0.0.1 text/html
    1635163617.361      0 127.0.0.1 TCP_MEM_HIT/200 352 HEAD http://127.0.0.1/ - HIER_NONE/- text/html
    

    没有代理

    NO_PROXY 可以设置环境变量,但默认为空。

    我手动添加了它:

    # export NO_PROXY=127.0.0.1
    
    # printenv | grep -i proxy
    NO_PROXY=127.0.0.1
    

    现在 卷曲 通过代理的请求将如下所示:

    # curl --proxy 127.0.0.1:3128 127.0.0.1 -vI
    
    * Uses proxy env variable NO_PROXY == '127.0.0.1' # curl detects NO_PROXY envvar
    *   Trying 127.0.0.1:80... # and ignores the proxy, connection goes directly
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > HEAD / HTTP/1.1
    > Host: 127.0.0.1
    > User-Agent: curl/7.68.0
    > Accept: */*
    

    可以覆盖 没有代理 执行时使用envvar 卷曲 指挥 --noproxy 旗帜

    --没有代理列表

    不使用代理(如果指定了代理)的主机的逗号分隔列表。唯一的通配符是单个通配符* 字符,它匹配所有主机,并有效地禁用 代理此列表中的每个名称都匹配为 包含主机名或主机名本身。例如,当地的。通用域名格式 会匹配本地的。com,本地的。com:80,和 www.local.com ,但不是 www.notlocal.com (在7.19.4中添加)。

    例子:

    # curl --proxy 127.0.0.1:3128 --noproxy "" 127.0.0.1 -vI
    
    *   Trying 127.0.0.1:3128... # connecting to proxy as it was supposed to
    * TCP_NODELAY set
    * Connected to 127.0.0.1 (127.0.0.1) port 3128 (#0) # connection to proxy is established
    > HEAD http://127.0.0.1/ HTTP/1.1 # connection to nginx on port 80
    > Host: 127.0.0.1
    > User-Agent: curl/7.68.0
    > Accept: */*
    

    这证明了代理是有效的!使用localhost。

    另一个选择 中的某些配置不正确吗 代理 这是问题中使用的。你可以得到这个吊舱并安装 鱿鱼 卷曲 放入两个容器中,自己试试。