linux
  • Introduction
  • Yum
    • 基础yum源的配置
    • 为yum源配置代理
    • Centos指定版本的yum源
    • 环境变量
    • 本地yum源
    • 制作yum源
      • 同步yum源
    • epel源
  • Iptables
    • 基本匹配条件
    • Match-Extensions
      • Addrtype
      • Set
      • TCP
    • Target-Extensions
      • DNAT
      • LOG
    • Iptables规则持久化
    • 连接追踪
  • LVS
    • Ipvsadm命令
  • 磁盘与分区
    • 创建分区
    • 格式化与挂载
    • fstab
    • LVM
      • LVM扩容
    • swap分区
    • tmpfs
  • 网络相关
    • 重命名网卡
    • resolv.conf
    • Tcpdump
    • dig与nslookup
  • Other
    • CPU与内存
    • 进程
      • 僵尸进程
    • SSH密钥登录
    • 用户管理
    • Crontab
  • Nofile
    • 原理
  • 常用软件安装篇
    • MYSQL
    • 系统与内核
      • 指定内核启动
    • NFS
    • Haproxy
    • Keepalived
    • Squid
    • Redsocks
    • Shadowsocks
    • 时钟同步
  • 内存
Powered by GitBook
On this page
  • --match set [options]
  • 示例
  • Reference

Was this helpful?

  1. Iptables
  2. Match-Extensions

Set

--match set [options]

--match可以简写为-m。常用的options

  • [!] --match-set <set> src

    源地址在<set>中,前面加!表示“源地址不在<set>中”

  • [!] --match-set <set> dst

    目的地址在<set>中,前面加!表示“目的地址不在<set>中”

<set>的值可以通过命令ipset list <set>查看

示例

在k8s集群中,我们有时希望Pod能够访问外网,而Pod本身的IP又是一个内部IP,此时我们就需要做SNAT。但是,我们又想Pod访问Pod时,不做SNAT。假设Pod的地址段为172.26.0.0/16。那么我们可以如下进行手动设置

首先创建一个名字为pod-cidr、类型为hash:net的set

$ ipset create pod-cidr hash:net

然后添加172.26.0.0/16这个net到pod-cidr中

$ ipset add pod-cidr 172.26.0.0/16

查看pod-cidr的详情,发现已经添加了一个条目

$ ipset list pod-cidr
Name: pod-cidr
Type: hash:net
Revision: 6
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 408
References: 0
Number of entries: 1
Members:
172.26.0.0/16

接下来我们在POSTROUTING中添加一条规则,使Pod到外网的包要做SNAT、Pod到Pod的包不做SNAT

$ iptables -t nat -I POSTROUTING -m set --match-set pod-cidr src -m set ! --match-set pod-cidr dst -j MASQUERADE

查看刚刚添加的规则

$ iptables -t nat -nL POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 0.0.0.0/0 0.0.0.0/0 match-set pod-cidr src ! match-set pod-cidr dst

Reference

PreviousAddrtypeNextTCP

Last updated 5 years ago

Was this helpful?

http://ipset.netfilter.org/iptables-extensions.man.html