基於Nginx的開牆方案


Kubernetes集群內部通過服務名能進行相互調用,但如果Kubernetes中的pod需要調用外部服務,而且這些外部服務是屬於不同的安全區域,就面臨開牆的問題,因為Kubernetes Pod能夠漂移道不同的宿主機中,因此很難做到只針對某幾台確定的宿主機進行防火牆的開通。參考傳統的架構中對外部服務的調用,在Kubernetes內部配置兩台宿主機專門部署Nginx Pod,進行反向代理進行實現。

  •  外部調用內部,Ingress所在節點開牆

 

  •  內部調用外部,Nginx所在節點開牆

 

 

  • 為什么不在ingress內做反向代理,因為Ingress中的配置會隨着pod的變化刷新掉,所以需要另外再啟動個Nginx pod,固定在2台機器上。
  • Nginx的配置不能直接固定在Pod中,因為有新的外部服務進行接入時,需要更新nginx.conf,因此需要采用configmap進行配置。
  • 每個服務掛接一個不同的location
apiVersion: v1
data:
  nginx.conf: |-
    worker_processes 1;

    events { worker_connections 1024; }

    http {
        sendfile on;

        server {
            listen 80;

            # a test endpoint that returns http 200s
            location /helloService {
                proxy_pass http://somehost:somenodeport/;
                proxy_set_header  X-Real-IP  $remote_addr;
            }
        }

    }
kind: ConfigMap
metadata:
  name: nginx-config
  namespace: default

nginx.yaml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  run: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: sz-pg-oam-docker-hub-001.tendcloud.com/library/nginx:1.9
        ports:
        - containerPort: 80
      volumeMounts:
      - name: config-volume
        mountPath: /etc/nginx
      volumes:
        - name: config-volume
          configMap:
            name: nginx-config

nginx啟動后會替換/etc/nginx中的內容,變成只有一個nginx.config(我們配置的內容)

為了其他pod訪問方面,還需要再建立一個services:nginxsvc

然后其他pod進去后通過

curl http://nginxsvc/helloService/hello

進行訪問。

其他得命名空間訪問

curl http://svcname:namespace/helloService/hello

 

這樣在pod進行飄移后不需要再度進行不同主機的開牆,只需要保持部署nginx的兩台機器開牆即可

 此方案問題在於當有新的服務接入時,需要更新configmap配置,更新完后發現nginx內的目錄文件也更新了,但是nginx沒有reload配置。

需要將nginx重啟后配置生效,可以通過kubectl delete pod方式進行逐個更新。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM