Dynamic proxy_pass in nginx to another pod in Kubernetes

后端 未结 2 1458
难免孤独
难免孤独 2021-02-08 06:26

I\'m trying to create an nginx proxy that forwards requests to / to http://. I first tried the following:



        
相关标签:
2条回答
  • 2021-02-08 06:43

    I resolved this by coredns docker : my nginx and coredns are all deploy on host

    step1: config Corefile in Corefile maybe you should change k8s master config refer: https://coredns.io/plugins/kubernetes/

    sudo mkdir /etc/coredns; sudo tee /etc/coredns/Corefile   <<-'EOF' .:53 {
        log
        errors
        health
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           endpoint http://172.31.88.71:8080
           pods insecure
           upstream
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }
        forward . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance } EOF
    

    step2:config docker and then start it

    tee coreos.sh   <<-'EOF'
     docker run --restart=always  -idt --name coredns \
     -v /etc/coredns/Corefile:/etc/coredns/Corefile \
     -v /home/ec2-user/.kube/config:/etc/coredns/kubeconfig \
     -p 53:53/udp \
     coredns/coredns:1.6.9 \
     -conf /etc/coredns/Corefile
    EOF
    

    step3: config nginx and then reload

    resolver 127.0.0.1 valid=60s ipv6=off;
    
    0 讨论(0)
  • 2021-02-08 06:59

    After much research and trial and error I managed to solve this. First I changed the pod specification to:

    spec:
      containers:
        - name: nginx
          image: "nginx:1.10.0"
          ports:
            - containerPort: 8080
              name: "external"
              protocol: "TCP"
        - name: dnsmasq
          image: "janeczku/go-dnsmasq:release-1.0.5"
          args:
            - --listen
            - "127.0.0.1:53"
            - --default-resolver
            - --append-search-domains
            - --hostsfile=/etc/hosts
            - --verbose
    

    then I also had to disable the ipv6 for the resolver in nginx:

    location ~ ^/(.+)$ {
            resolver 127.0.0.1:53 ipv6=off;
            set $backend "http://$1:80";
            proxy_pass $backend;
    }
    

    Then it works as expected!

    0 讨论(0)
提交回复
热议问题