【译】kube-router-5 安装

307 阅读13分钟

使用集群安装程序试用 Kube-router

开始使用 Kube-router 部署 Kubernetes 的最佳方法是使用集群安装程序。

kops

请参阅kops与 Kube-router 一起部署 Kubernetes 群集的 步骤

kubeadm

请参阅kubeadm与 Kube-router 一起部署Kubernetes 集群的步骤

k0sproject

k0s 默认使用 Kube-router 作为 CNI 选项。 请参阅k0s与 Kube-router 一起部署 Kubernetes 群集步骤

k3sproject

k3s 默认使用 kube-router的网络策略控制器实现网络策略执行。

通用

请参阅在手动安装的集群上部署 kube-router的步骤

示例:

env:
- name: HTTP_PROXY
  value: "http://proxy.example.com:80"

部署

根据您想要使用的功能,kube-router 有多种部署选项。可以使用 --run-firewall, --run-router, --run-service-proxy, --run-loadbalancer等标志,选择性地启用您所需的功能。

此外,你还可以选择将 kube-router 作为代理运行在每个集群节点上。或者,也可以将 kube-router 作为pod 运行。

命令行选项

Usage of kube-router:
      --advertise-cluster-ip                          Add Cluster IP of the service to the RIB so that it gets advertises to the BGP peers.
      --advertise-external-ip                         Add External IP of service to the RIB so that it gets advertised to the BGP peers.
      --advertise-loadbalancer-ip                     Add LoadbBalancer IP of service status as set by the LB provider to the RIB so that it gets advertised to the BGP peers.
      --advertise-pod-cidr                            Add Node's POD cidr to the RIB so that it gets advertised to the BGP peers. (default true)
      --auto-mtu                                      Auto detect and set the largest possible MTU for kube-bridge and pod interfaces (also accounts for IPIP overlay network when enabled). (default true)
      --bgp-graceful-restart                          Enables the BGP Graceful Restart capability so that routes are preserved on unexpected restarts
      --bgp-graceful-restart-deferral-time duration   BGP Graceful restart deferral time according to RFC4724 4.1, maximum 18h. (default 6m0s)
      --bgp-graceful-restart-time duration            BGP Graceful restart time according to RFC4724 3, maximum 4095s. (default 1m30s)
      --bgp-holdtime duration                         This parameter is mainly used to modify the holdtime declared to BGP peer. When Kube-router goes down abnormally, the local saving time of BGP route will be affected. Holdtime must be in the range 3s to 18h12m16s. (default 1m30s)
      --bgp-port uint32                               The port open for incoming BGP connections and to use for connecting with other BGP peers. (default 179)
      --cache-sync-timeout duration                   The timeout for cache synchronization (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s)
      --cleanup-config                                Cleanup iptables rules, ipvs, ipset configuration and exit.
      --cluster-asn uint                              ASN number under which cluster nodes will run iBGP.
      --disable-source-dest-check                     Disable the source-dest-check attribute for AWS EC2 instances. When this option is false, it must be set some other way. (default true)
      --enable-cni                                    Enable CNI plugin. Disable if you want to use kube-router features alongside another CNI plugin. (default true)
      --enable-ibgp                                   Enables peering with nodes with the same ASN, if disabled will only peer with external BGP peers (default true)
      --enable-ipv4                                   Enables IPv4 support (default true)
      --enable-ipv6                                   Enables IPv6 support
      --enable-overlay                                When enable-overlay is set to true, IP-in-IP tunneling is used for pod-to-pod networking across nodes in different subnets. When set to false no tunneling is used and routing infrastructure is expected to route traffic for pod-to-pod networking across nodes in different subnets (default true)
      --enable-pod-egress                             SNAT traffic from Pods to destinations outside the cluster. (default true)
      --enable-pprof                                  Enables pprof for debugging performance and memory leak issues.
      --excluded-cidrs strings                        Excluded CIDRs are used to exclude IPVS rules from deletion.
      --hairpin-mode                                  Add iptables rules for every Service Endpoint to support hairpin traffic.
      --health-port uint16                            Health check port, 0 = Disabled (default 20244)
  -h, --help                                          Print usage information.
      --hostname-override string                      Overrides the NodeName of the node. Set this if kube-router is unable to determine your NodeName automatically.
      --injected-routes-sync-period duration          The delay between route table synchronizations  (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s)
      --iptables-sync-period duration                 The delay between iptables rule synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 5m0s)
      --ipvs-graceful-period duration                 The graceful period before removing destinations from IPVS services (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 30s)
      --ipvs-graceful-termination                     Enables the experimental IPVS graceful terminaton capability
      --ipvs-permit-all                               Enables rule to accept all incoming traffic to service VIP's on the node. (default true)
      --ipvs-sync-period duration                     The delay between ipvs config synchronizations (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 5m0s)
      --kubeconfig string                             Path to kubeconfig file with authorization information (the master location is set by the master flag).
      --loadbalancer-default-class                    Handle loadbalancer services without a class (default true)
      --loadbalancer-ip-range strings                 CIDR values from which loadbalancer services addresses are assigned (can be specified multiple times)
      --loadbalancer-sync-period duration             The delay between checking for missed services (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s)
      --masquerade-all                                SNAT all traffic to cluster IP/node port.
      --master string                                 The address of the Kubernetes API server (overrides any value in kubeconfig).
      --metrics-addr string                           Prometheus metrics address to listen on, (Default: all interfaces)
      --metrics-path string                           Prometheus metrics path (default "/metrics")
      --metrics-port uint16                           Prometheus metrics port, (Default 0, Disabled)
      --nodeport-bindon-all-ip                        For service of NodePort type create IPVS service that listens on all IP's of the node.
      --nodes-full-mesh                               Each node in the cluster will setup BGP peering with rest of the nodes. (default true)
      --overlay-encap string                          Valid encapsulation types are "ipip" or "fou" (if set to "fou", the udp port can be specified via "overlay-encap-port") (default "ipip")
      --overlay-encap-port uint16                     Overlay tunnel encapsulation port (only used for "fou" encapsulation) (default 5555)
      --overlay-type string                           Possible values: subnet,full - When set to "subnet", the default, default "--enable-overlay=true" behavior is used. When set to "full", it changes "--enable-overlay=true" default behavior so that IP-in-IP tunneling is used for pod-to-pod networking across nodes regardless of the subnet the nodes are in. (default "subnet")
      --override-nexthop                              Override the next-hop in bgp routes sent to peers with the local ip.
      --peer-router-asns uints                        ASN numbers of the BGP peer to which cluster nodes will advertise cluster ip and node's pod cidr. (default [])
      --peer-router-ips ipSlice                       The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's. (default [])
      --peer-router-multihop-ttl uint8                Enable eBGP multihop supports -- sets multihop-ttl. (Relevant only if ttl >= 2)
      --peer-router-passwords strings                 Password for authenticating against the BGP peer defined with "--peer-router-ips".
      --peer-router-passwords-file string             Path to file containing password for authenticating against the BGP peer defined with "--peer-router-ips". --peer-router-passwords will be preferred if both are set.
      --peer-router-ports uints                       The remote port of the external BGP to which all nodes will peer. If not set, default BGP port (179) will be used. (default [])
      --router-id string                              BGP router-id. Must be specified in a ipv6 only cluster, "generate" can be specified to generate the router id.
      --routes-sync-period duration                   The delay between route updates and advertisements (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 5m0s)
      --run-firewall                                  Enables Network Policy -- sets up iptables to provide ingress firewall for pods. (default true)
      --run-loadbalancer                              Enable loadbalancer address allocator
      --run-router                                    Enables Pod Networking -- Advertises and learns the routes to Pods via iBGP. (default true)
      --run-service-proxy                             Enables Service Proxy -- sets up IPVS for Kubernetes Services. (default true)
      --runtime-endpoint string                       Path to CRI compatible container runtime socket (used for DSR mode). Currently known working with containerd.
      --service-cluster-ip-range strings              CIDR values from which service cluster IPs are assigned (can be specified up to 2 times) (default [10.96.0.0/12])
      --service-external-ip-range strings             Specify external IP CIDRs that are used for inter-cluster communication (can be specified multiple times)
      --service-node-port-range string                NodePort range specified with either a hyphen or colon (default "30000-32767")
      --service-tcp-timeout duration                  Specify TCP timeout for IPVS services in standard duration syntax (e.g. '5s', '1m'), default 0s preserves default system value (default: 0s)
      --service-tcpfin-timeout duration               Specify TCP FIN timeout for IPVS services in standard duration syntax (e.g. '5s', '1m'), default 0s preserves default system value (default: 0s)
      --service-udp-timeout duration                  Specify UDP timeout for IPVS services in standard duration syntax (e.g. '5s', '1m'), default 0s preserves default system value (default: 0s)
  -v, --v string                                      log level for V logs (default "0")
  -V, --version                                       Print version information.

安装要求

  • Kube-router 需要访问 kubernetes API 服务器,以获取有关 pod、服务、端点、网络策略等信息。 它的基本需要是访问 kubernetes API 服务器的详细信息。可以通过以下方式传递参数获取
kube-router --master=http://192.168.1.99:8080/` or `kube-router --kubeconfig=<path to kubeconfig file>
  • 如果在节点上以代理身份运行 kube-router,则必须在每个节点上安装 ipset 软件包(以daemonset运行时,容器映像已预先与 ipset 打包在一起)。

  • 如果选择使用 kube-router 进行 pod 之间的网络连接,则需要对 Kubernetes 控制器管理器进行以下配置来分配 pod CIDR,方法是传递 --allocate-node-cidrs=true标记,并提供一个 cluster-cidr (例如,传递 --cluster-cidr=10.1.0.0/16)。

  • 如果你选择在低于 v1.15 的 Kubernetes 版本中将 kube-router 作为守护进程运行,那么 kube-apiserver 和 kubelet 都必须以--allow-privileged=true 选项运行。在以后的 Kubernetes 版本中,只有 kube-apiserver 必须使用--allow-privileged=true` 选项运行,如果启用了 PodSecurityPolicy 准入控制器,则应创建 PodSecurityPolicy,允许有特权的 kube-router pod 运行。

  • 此外,在守护进程模式下运行时,强烈建议保留与 netfilter 相关的用户空间主机上的"iptables"、"ipset "和 "ipvsadm "等工具并和 Alpine 在 kube-router 容器分发的版本保持同步。这将有助于避免在主机的用户空间和 kube-router 的用户空间工具修改 netfilter 内核定义时可能产生的冲突。请参见 更多信息,请参见:此 kube-router 问题

  • 如果选择使用 kube-router 进行 pod 到 pod 的网络连接,则必须为 Kubernetes 群集配置CNI 网络插件。每个节点上的 CNI 配置文件应为 /etc/cni/net.d/10-kuberouter.conf。应该使用 "bridge "CNI 插件和用于 IPAM 的 "host-local"。配置示例文件下载如下:

wget -O /etc/cni/net.d/10-kuberouter.conf https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/cni/10-kuberouter.conf`
  • 此外,如果您让 kube-router 管理 pod 到 pod 的网络,上述 bridgehost-local CNI 插件必须存在,以便容器运行时引用。 此外,如果您在任何 pod 上使用 "hostPort",则需要安装 "hostport "插件。从 kube-router v2.1.X 开始, 如果 kube-router 发现缺少这些插件,它会在 initContainer 阶段为你安装到 /opt/cni/bin。大多数容器运行时 默认情况下会在那里查找您的插件,但如果您的 pod 出现问题,您可能需要对它们进行配置。

作为守护进程运行

这是在 Kubernetes 中部署 kube-router 的最快方法(不要忘记确保满足上述要求)。 简单到只需运行下面脚本即可。

kubectl apply -f https://raw.githubusercontent.com/cloudnativelabs/kube-router/master/daemonset/kube-router-all-service-daemonset.yaml

以上操作会自动在每个节点上以 pod 的形式运行 kube-router。您可以根据需要更改 daemonset 定义中的参数,示例可参见守护进程配置示例.

作为代理运行

您可以将 kube-router 作为代理运行在每个节点上。如您只想让 kube-router 为 pod 提供入口防火墙,那么可以以下列方式启动 kube-router:

kube-router --master=http://192.168.1.99:8080/ --run-firewall=true --run-service-proxy=false --run-router=false

清理配置

请删除 kube-router 守护进程集,然后通过运行以下命令清理 kube-router 在节点上完成的所有配置(ipvs、iptables、ipset、ip routes 等),命令如下:

docker run --privileged --net=host cloudnativelabs/kube-router --cleanup-config

尝试用 Kube-router 替代 Kube-proxy

如果您正在使用 kube-proxy,并想尝试使用 kube-router 作为服务代理,您可以执行以下操作:

kube-proxy --cleanup-iptables

继续执行

kube-router --master=http://192.168.1.99:8080/ --run-service-proxy=true --run-firewall=false --run-router=false

如果您想退回到kube-proxy,就要删除现有的kube-router配置,则通过运行

 kube-router --cleanup-config

然后用您的kube-proxy的配置运行kube-proxy.

公示IP

kube-router 可以向 BGP 对等方公布集群、外部和负载平衡器 IP。 它的做法是

  • 本地将公示 IP 添加到节点的 kube-dummy-if 网络接口上
  • 向 BGP 对等体公布 IP

要为所有服务设置默认值,请使用 --advertise-cluster-ip--advertise-external-ip--advertise-loadbalance-ip 标记来设置所有服务的默认值。

要选择性地为每个服务启用或禁用该功能,请使用 kube-router.io/service.advertise.clusterip、和 kube-router.io/service.advertise.loadbalancerip 注解。

例如:

$ kubectl annotate service my-advertised-service "kube-router.io/service.advertise.clusterip=true" $ kubectl annotate service my-advertised-service "kube-router.io/service.advertise.externalip=true" $ kubectl annotate service my-advertised-service "kube-router.io/service.advertise.loadbalancerip=true"

$ kubectl annotate service my-non-advertised-service "kube-router.io/service.advertise.clusterip=false" $ kubectl annotate service my-non-advertised-service "kube-router.io/service.advertise.externalip=false" $ kubectl annotate service my-non-advertised-service "kube-router.io/service.advertise.loadbalancerip=false"

通过将标志与按服务注释相结合,您可以为IP选择 "opt-in"或 "opt-out"公示策略。

公告 LoadBalancer IP 的方法是监视外部设置的status.loadBalancer.ingress中的IP(如开源的k8sLB库MetalLb)。MetalLB在 ARP 模式下已被成功测试通过。

控制服务位置/流量策略

可通过 Kubernetes 标准流量策略和自定义的 kube-router 服务注解kube-router.io/service.local: true。来控制外部和集群背部的服务可用性。

有关 spec.internalTrafficPolicyspec.externalTrafficPolicy 的更多信息,请参阅之前链接的上游 Kubernetes 文档。 和 spec.externalTrafficPolicy 的更多信息,请参阅上面的 Kubernetes 文档链接。

为了保持向后兼容性,kube-router.io/service.local: true 注解有效地覆盖了 spec.internalTrafficPolicyspec.externalTrafficPolicy,并强制使kube-router行为和本地一致。

发夹模式

默认情况下,不支持从服务后面的 Pod 与自己的 ClusterIP:Port 通信。 不过 可通过添加 kube-router.io/service.hairpin= 注解来为单个服务启用, 或通过向 kube-router 传递标志 --hairpin-mode=true,为集群中的所有服务启用该功能。

此外,必须将每个节点上所有 veth 接口的 hairpin_mode sysctl 选项设置为 1。 这可以通过在 CNI 配置中添加 "hairpinMode": true 选项并重启所有集群节点(如果已运行 kubernetes)。

来源于服务pod的发夹流量将看做是来自集群IP,在记录源IP日志时也会被记录,

发夹模式示例

10-kuberouter.conf

{
    "name":"mynet",
    "type":"bridge",
    "bridge":"kube-bridge",
    "isDefaultGateway":true,
    "hairpinMode":true,
    "ipam": {
        "type":"host-local"
     }
}

为 "my-service"启用发夹式流量:

kubectl annotate service my-service "kube-router.io/service.hairpin="

如果您也想为服务 my-service 声明的外部 IP 设置发夹(注意,您还必须启用全局 发夹或服务发夹(见上文 ^^^)才能生效):

kubectl annotate service my-service "kube-router.io/service.hairpin.externalips="

SNATing 服务流量

默认情况下,当流量进入集群时,kube-router 会将流量源化,以确保路由对称 如果该流量需要被代理,且确保它能到达拥有服务 pod 并能为流量提供服务的节点。 这可能会在对该服务应用网络策略时制造问题,因为现在流量 将显示为来自群集中的任何一个节点,而不是流量发起方。

这是所有代理和所有 Kubernetes 服务代理普遍存在的问题。有关此问题的更多信息 有关此问题的更多信息: 服务源 IP

除了上面链接文档中提到的修复方法(使用 service.spec.externalTrafficPolicy)外、 kube-router 还提供了 DSR,其本质是保留源 IP,以解决这一问题。

负载平衡调度算法

Kube-router 使用 LVS 作为服务代理。LVS 支持丰富的 调度算法。您可以选择其中一种调度算法来注解服务。如果服务没有注解,则默认选择 "round-robin "调度器。

#最少连接调度算法:
$ kubectl annotate service my-service "kube-router.io/service.scheduler=lc"

#轮询调度算法:
$ kubectl annotate service my-service "kube-router.io/service.scheduler=rr"

#源哈希调度算法:
$ kubectl annotate service my-service "kube-router.io/service.scheduler=sh"

#目的地哈希调度算法:
$ kubectl annotate service my-service "kube-router.io/service.scheduler=dh"

支持hostPort

如果您想使用 "HostPort"功能,则需要对清单进行以下更改。

  • 默认情况下,kube-router 假定 CNI 配置文件为 /etc/cni/net.d/10-kuberouter.conf。 添加环境变量 KUBE_ROUTER_CNI_CONF_FILE 至 kube-router 清单,并将其设置为 /etc/cni/net.d/10-kuberouter.conflist

  • 使用支持将 portmap 作为附加插件的 CNI 配置修改 kube-router-cfg 配置表

   {
      "cniVersion":"0.3.0",
      "name":"mynet",
      "plugins":[
         {
            "name":"kubernetes",
            "type":"bridge",
            "bridge":"kube-bridge",
            "isDefaultGateway":true,
            "ipam":{
               "type":"host-local"
            }
         },
         {
            "type":"portmap",
            "capabilities":{
               "snat":true,
               "portMappings":true
            }
         }
      ]
   }
  • 更新初始化容器命令以创建 /etc/cni/net.d/10-kuberouter.conflist 文件
  • 重启容器运行时

如需清单,请查看 manifest,其中包含HostPort功能所需的更改。

支持 IPVS 优雅终止

从 0.2.6 开始,我们试验性地支持优雅终止 IPVS 目的地。在可能的情况下,pod 的TerminationGracePeriodSeconds 会被使用。可通过 --ipvs-graceful-period CLi-opt 进行调整。

优美终止的工作方式是,当 kube-router 收到服务的删除端点通知时 它的权重会被调整为 0,然后在终止宽限期已过或活动和非活动连接降至 0 后才会被删除。

MTU

最大传输单元 (MTU) 决定了可通过网络传输的最大数据包大小。 应适当设置 pod 接口的 MTU,以防止碎片和数据包丢弃,从而实现最高性能。如果将 "auto-mtu "设置为 true(从 kube-router 1.1 起,"auto-mtu "默认设置为 true),kube-router 将决定kube-bridge和pod接口的 MTU。如果将 auto-mtu 设置为 false,kube-router 将不会配置 MTU。 不过,您可以选择正确的 MTU,并在 10-kuberouter.conflist"(../daemonset/)中的 "cni-config.json "部分进行设置。例如:

  cni-conf.json: |
    {
       "cniVersion":"0.3.0",
       "name":"mynet",
       "plugins":[
          {
             "name":"kubernetes",
             "type":"bridge",
             "mtu": 1400,
             "bridge":"kube-bridge",
             "isDefaultGateway":true,
             "ipam":{
                "type":"host-local"
             }
          }
       ]
    }

如果您通过 CNI 配置自行设置 MTU,则还需要将 kube-bridge 的 MTU 手动设置为正确值 以避免在已创建 kube-bridge 的现有节点上出现数据包碎片。在节点重启或 新节点加入集群时,pod 的接口和 kube-bridge 都将使用指定的 MTU 值进行设置。

BGP 配置

配置 BGP 对等体

指标

配置指标收集