
내 루트 서버(netcup)에서 실행 중인 kubernetes 클러스터(현재 1개 노드)가 있고 트래픽을 해당 클러스터로 라우팅하려고 합니다. 나는 네트워킹에 꽤 능숙하지 않기 때문에 누군가가 나를 도울 수 있기를 바랍니다.
클러스터에서금속Calico는 CNI로 설치됩니다. Metallb는 내부적으로 잘 작동하는 K8s-LoadBalancer에 IP 주소를 할당하고 알리는 역할을 담당합니다. 따라서 서버 내에서 로드 밸런싱된 서비스에 접근할 수 있습니다. 고정된 내부 IP(역방향 프록시)를 사용하여 단일 서비스만 노출합니다.
이제 다음을 사용하여 metallb의 IP 주소로 직접 트래픽을 라우팅하여 외부에 노출시키고 싶습니다.iptables
iptables에는 다음 규칙이 적용되었습니다.
iptables -A PREROUTING -t nat -p tcp -m tcp -j DNAT --to-destination <metallb-ip> -i eth0 --destination-port 80 -m comment --comment Redirect web traffic to cluster (80)
iptables -A PREROUTING -t nat -p tcp -m tcp -j DNAT --to-destination <metallb-ip> -i eth0 --destination-port 443 -m comment --comment Redirect web traffic to cluster (443)
iptables -A POSTROUTING -t nat -p tcp -m tcp -j SNAT --to-source <public-server-ip> -o eth0 --destination-port 80 -m comment --comment Redirect web traffic from cluster (80)
iptables -A POSTROUTING -t nat -p tcp -m tcp -j SNAT --to-source <public-server-ip> -o eth0 --destination-port 443 -m comment --comment Redirect web traffic from cluster (443)
(어쩌면 명령을 내 ansible 스크립트에서 추출했기 때문에 명령이 100% 정확하지 않을 수도 있습니다. ansible 부분은 맨 아래에서 찾을 수 있습니다)
내가 이해하는 한 이것은 들어오는 모든 TCP 트래픽을 DNAT를 사용하여 내부 IP로 보내야 하며 모든 나가는 트래픽은 SNAT를 사용하는 공용 서버 주소를 갖게 됩니다. 그러면 연결 클라이언트의 경우 공용 IP/포트와 직접 통신하는 것처럼 보여야 합니까?
안타깝게도 인터넷에서 서비스에 액세스하려고 하면 다음과 같은 결과가 발생합니다.
$> curl <public-server-ip>
curl: (7) Failed to connect to <public-server-ip> port 80 after 647 ms: Network is down
서버에서의 액세스가 작동합니다.
$> curl <metallb-ip>
404 page not found # which the expected answer since its a blank Traefik reverse proxy
nmap 포트 스캔은 현재 다음을 발생시킵니다.
$> nmap -Pn -p 80,443 <public-server-ip>
Starting Nmap 7.93 ( https://nmap.org ) at 2022-12-15 15:57 CET
Strange SO_ERROR from connection to <public-server-ip> (50 - 'Network is down') -- bailing scan
QUITTING!
하지만 SSH를 통해 연결한 이후로 실행 중입니다.
현재 활성화된 항목이 없습니다 ufw
.
제가 말했듯이 저는 네트워킹에 능숙하지 않기 때문에 도움을 주시면 감사하겠습니다. ;)
앤서블 스크립트:
- name: DNAT port 80 to cluster
iptables:
table: nat
chain: PREROUTING
in_interface: eth0
protocol: tcp
match: tcp
jump: DNAT # REDIRECT
to_destination: <metallb-ip>
destination_port: 80
comment: Redirect web traffic to cluster (80)
become: yes
- name: DNAT port 443 to cluster
iptables:
table: nat
chain: PREROUTING
in_interface: eth0
protocol: tcp
match: tcp
destination_port: 443
jump: DNAT # REDIRECT
to_destination: <metallb-ip>
comment: Redirect web traffic to cluster (443)
become: yes
- name: SNAT port 80 from cluster
iptables:
table: nat
chain: POSTROUTING
out_interface: eth0
protocol: tcp
match: tcp
destination_port: 80
jump: SNAT # REDIRECT
to_source: <public-server-ip>
comment: Redirect web traffic from cluster (443)
become: yes
- name: SNAT port 443 from cluster
iptables:
table: nat
chain: POSTROUTING
out_interface: eth0
protocol: tcp
match: tcp
destination_port: 443
jump: SNAT # REDIRECT
to_source: <public-server-ip>
comment: Redirect web traffic from cluster (443)
become: yes
- name: Configure IP Masquerading
iptables:
table: nat
chain: POSTROUTING
out_interface: eth0
jump: MASQUERADE
업데이트: 의 도움으로이것기사 흐름을 이해하려고 노력했습니다. 어디에서 문제가 발생하는지 조사하기 위해 로깅 문이 추가되었습니다. 그것이 내가 지금까지 찾은 것입니다:
패킷이 손실된 위치를 확인하기 위해 다음 규칙을 추가했습니다.
iptables -t raw -A PREROUTING -p tcp --dport 80 -j LOG --log-prefix "[raw:PREROUTING]: "
iptables -t mangle -A PREROUTING -p tcp --dport 80 -j LOG --log-prefix "[mangle:PREROUTING]: "
iptables -t nat -A PREROUTING -p tcp --dport 80 -j LOG --log-prefix "[nat:PREROUTING]: "
iptables -t mangle -A POSTROUTING -p tcp --sport 80 -j LOG --log-prefix "[mangle:PREROUTING]: "
iptables -t nat -A POSTROUTING -p tcp --sport 80 -j LOG --log-prefix "[nat:PREROUTING]: "
및 로그 문 /var/log/kern.log
바로 아래에 나타납니다. 따라서 -step이 트리거되지 않은 것 같습니다 . 다음을 사용하여 맹글 규칙을 확인했지만 여기서는 어떤 문제도 식별할 수 없습니다.raw
mangle
nat
sudo iptables -t mangle -L
IP 전달이 활성화되었습니다:
$> sudo sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
그러면 왜 패킷이 nat
-step으로 오지 않습니까?
$> sudo iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
cali-PREROUTING all -- anywhere anywhere /* cali:6gwbT8clXdHdC1b1 */
LOG tcp -- anywhere anywhere tcp dpt:http LOG level warning prefix "[mangle:PREROUTING]: "
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
cali-POSTROUTING all -- anywhere anywhere /* cali:O3lYWMrLQYEMJtB5 */
Chain KUBE-IPTABLES-HINT (0 references)
target prot opt source destination
Chain KUBE-KUBELET-CANARY (0 references)
target prot opt source destination
Chain KUBE-PROXY-CANARY (0 references)
target prot opt source destination
Chain cali-from-host-endpoint (1 references)
target prot opt source destination
Chain cali-to-host-endpoint (1 references)
target prot opt source destination
Chain cali-PREROUTING (1 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere /* cali:6BJqBjBC7crtA-7- */ ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere /* cali:KX7AGNd6rMcDUai6 */ mark match 0x10000/0x10000
cali-from-host-endpoint all -- anywhere anywhere /* cali:wNH7KsA3ILKJBsY9 */
ACCEPT all -- anywhere anywhere /* cali:Cg96MgVuoPm7UMRo */ /* Host endpoint policy accepted packet. */ mark match 0x10000/0x10000
Chain cali-POSTROUTING (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere /* cali:NX-7roTexQ3fGRfU */ mark match 0x10000/0x10000
MARK all -- anywhere anywhere /* cali:nnqPh8lh2VOogSzX */ MARK and 0xfff0ffff
cali-to-host-endpoint all -- anywhere anywhere /* cali:nquN8Jw8Tz72pcBW */ ctstate DNAT
RETURN all -- anywhere anywhere /* cali:jWrgvDQ0xEZHmta3 */ /* Host endpoint policy accepted packet. */ mark match 0x10000/0x10000