kubelet_network_linux.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // +build linux
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kubelet
  15. import (
  16. "fmt"
  17. "k8s.io/klog"
  18. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  19. )
  20. // syncNetworkUtil ensures the network utility are present on host.
  21. // Network util includes:
  22. // 1. In nat table, KUBE-MARK-DROP rule to mark connections for dropping
  23. // Marked connection will be drop on INPUT/OUTPUT Chain in filter table
  24. // 2. In nat table, KUBE-MARK-MASQ rule to mark connections for SNAT
  25. // Marked connection will get SNAT on POSTROUTING Chain in nat table
  26. func (kl *Kubelet) syncNetworkUtil() {
  27. if kl.iptablesMasqueradeBit < 0 || kl.iptablesMasqueradeBit > 31 {
  28. klog.Errorf("invalid iptables-masquerade-bit %v not in [0, 31]", kl.iptablesMasqueradeBit)
  29. return
  30. }
  31. if kl.iptablesDropBit < 0 || kl.iptablesDropBit > 31 {
  32. klog.Errorf("invalid iptables-drop-bit %v not in [0, 31]", kl.iptablesDropBit)
  33. return
  34. }
  35. if kl.iptablesDropBit == kl.iptablesMasqueradeBit {
  36. klog.Errorf("iptables-masquerade-bit %v and iptables-drop-bit %v must be different", kl.iptablesMasqueradeBit, kl.iptablesDropBit)
  37. return
  38. }
  39. // Setup KUBE-MARK-DROP rules
  40. dropMark := getIPTablesMark(kl.iptablesDropBit)
  41. if _, err := kl.iptClient.EnsureChain(utiliptables.TableNAT, KubeMarkDropChain); err != nil {
  42. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, KubeMarkDropChain, err)
  43. return
  44. }
  45. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableNAT, KubeMarkDropChain, "-j", "MARK", "--set-xmark", dropMark); err != nil {
  46. klog.Errorf("Failed to ensure marking rule for %v: %v", KubeMarkDropChain, err)
  47. return
  48. }
  49. if _, err := kl.iptClient.EnsureChain(utiliptables.TableFilter, KubeFirewallChain); err != nil {
  50. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableFilter, KubeFirewallChain, err)
  51. return
  52. }
  53. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableFilter, KubeFirewallChain,
  54. "-m", "comment", "--comment", "kubernetes firewall for dropping marked packets",
  55. "-m", "mark", "--mark", dropMark,
  56. "-j", "DROP"); err != nil {
  57. klog.Errorf("Failed to ensure rule to drop packet marked by %v in %v chain %v: %v", KubeMarkDropChain, utiliptables.TableFilter, KubeFirewallChain, err)
  58. return
  59. }
  60. if _, err := kl.iptClient.EnsureRule(utiliptables.Prepend, utiliptables.TableFilter, utiliptables.ChainOutput, "-j", string(KubeFirewallChain)); err != nil {
  61. klog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableFilter, utiliptables.ChainOutput, KubeFirewallChain, err)
  62. return
  63. }
  64. if _, err := kl.iptClient.EnsureRule(utiliptables.Prepend, utiliptables.TableFilter, utiliptables.ChainInput, "-j", string(KubeFirewallChain)); err != nil {
  65. klog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableFilter, utiliptables.ChainInput, KubeFirewallChain, err)
  66. return
  67. }
  68. // Setup KUBE-MARK-MASQ rules
  69. masqueradeMark := getIPTablesMark(kl.iptablesMasqueradeBit)
  70. if _, err := kl.iptClient.EnsureChain(utiliptables.TableNAT, KubeMarkMasqChain); err != nil {
  71. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, KubeMarkMasqChain, err)
  72. return
  73. }
  74. if _, err := kl.iptClient.EnsureChain(utiliptables.TableNAT, KubePostroutingChain); err != nil {
  75. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, KubePostroutingChain, err)
  76. return
  77. }
  78. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableNAT, KubeMarkMasqChain, "-j", "MARK", "--set-xmark", masqueradeMark); err != nil {
  79. klog.Errorf("Failed to ensure marking rule for %v: %v", KubeMarkMasqChain, err)
  80. return
  81. }
  82. if _, err := kl.iptClient.EnsureRule(utiliptables.Prepend, utiliptables.TableNAT, utiliptables.ChainPostrouting,
  83. "-m", "comment", "--comment", "kubernetes postrouting rules", "-j", string(KubePostroutingChain)); err != nil {
  84. klog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableNAT, utiliptables.ChainPostrouting, KubePostroutingChain, err)
  85. return
  86. }
  87. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableNAT, KubePostroutingChain,
  88. "-m", "comment", "--comment", "kubernetes service traffic requiring SNAT",
  89. "-m", "mark", "--mark", masqueradeMark, "-j", "MASQUERADE"); err != nil {
  90. klog.Errorf("Failed to ensure SNAT rule for packets marked by %v in %v chain %v: %v", KubeMarkMasqChain, utiliptables.TableNAT, KubePostroutingChain, err)
  91. return
  92. }
  93. }
  94. // getIPTablesMark returns the fwmark given the bit
  95. func getIPTablesMark(bit int) string {
  96. value := 1 << uint(bit)
  97. return fmt.Sprintf("%#08x/%#08x", value, value)
  98. }