kubelet_network_linux.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "time"
  18. "k8s.io/apimachinery/pkg/util/wait"
  19. "k8s.io/klog"
  20. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  21. )
  22. func (kl *Kubelet) initNetworkUtil() {
  23. kl.syncNetworkUtil()
  24. go kl.iptClient.Monitor(utiliptables.Chain("KUBE-KUBELET-CANARY"),
  25. []utiliptables.Table{utiliptables.TableMangle, utiliptables.TableNAT, utiliptables.TableFilter},
  26. kl.syncNetworkUtil, 1*time.Minute, wait.NeverStop)
  27. }
  28. // syncNetworkUtil ensures the network utility are present on host.
  29. // Network util includes:
  30. // 1. In nat table, KUBE-MARK-DROP rule to mark connections for dropping
  31. // Marked connection will be drop on INPUT/OUTPUT Chain in filter table
  32. // 2. In nat table, KUBE-MARK-MASQ rule to mark connections for SNAT
  33. // Marked connection will get SNAT on POSTROUTING Chain in nat table
  34. func (kl *Kubelet) syncNetworkUtil() {
  35. if kl.iptablesMasqueradeBit < 0 || kl.iptablesMasqueradeBit > 31 {
  36. klog.Errorf("invalid iptables-masquerade-bit %v not in [0, 31]", kl.iptablesMasqueradeBit)
  37. return
  38. }
  39. if kl.iptablesDropBit < 0 || kl.iptablesDropBit > 31 {
  40. klog.Errorf("invalid iptables-drop-bit %v not in [0, 31]", kl.iptablesDropBit)
  41. return
  42. }
  43. if kl.iptablesDropBit == kl.iptablesMasqueradeBit {
  44. klog.Errorf("iptables-masquerade-bit %v and iptables-drop-bit %v must be different", kl.iptablesMasqueradeBit, kl.iptablesDropBit)
  45. return
  46. }
  47. // Setup KUBE-MARK-DROP rules
  48. dropMark := getIPTablesMark(kl.iptablesDropBit)
  49. if _, err := kl.iptClient.EnsureChain(utiliptables.TableNAT, KubeMarkDropChain); err != nil {
  50. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, KubeMarkDropChain, err)
  51. return
  52. }
  53. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableNAT, KubeMarkDropChain, "-j", "MARK", "--set-xmark", dropMark); err != nil {
  54. klog.Errorf("Failed to ensure marking rule for %v: %v", KubeMarkDropChain, err)
  55. return
  56. }
  57. if _, err := kl.iptClient.EnsureChain(utiliptables.TableFilter, KubeFirewallChain); err != nil {
  58. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableFilter, KubeFirewallChain, err)
  59. return
  60. }
  61. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableFilter, KubeFirewallChain,
  62. "-m", "comment", "--comment", "kubernetes firewall for dropping marked packets",
  63. "-m", "mark", "--mark", dropMark,
  64. "-j", "DROP"); err != nil {
  65. klog.Errorf("Failed to ensure rule to drop packet marked by %v in %v chain %v: %v", KubeMarkDropChain, utiliptables.TableFilter, KubeFirewallChain, err)
  66. return
  67. }
  68. if _, err := kl.iptClient.EnsureRule(utiliptables.Prepend, utiliptables.TableFilter, utiliptables.ChainOutput, "-j", string(KubeFirewallChain)); err != nil {
  69. klog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableFilter, utiliptables.ChainOutput, KubeFirewallChain, err)
  70. return
  71. }
  72. if _, err := kl.iptClient.EnsureRule(utiliptables.Prepend, utiliptables.TableFilter, utiliptables.ChainInput, "-j", string(KubeFirewallChain)); err != nil {
  73. klog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableFilter, utiliptables.ChainInput, KubeFirewallChain, err)
  74. return
  75. }
  76. // Setup KUBE-MARK-MASQ rules
  77. masqueradeMark := getIPTablesMark(kl.iptablesMasqueradeBit)
  78. if _, err := kl.iptClient.EnsureChain(utiliptables.TableNAT, KubeMarkMasqChain); err != nil {
  79. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, KubeMarkMasqChain, err)
  80. return
  81. }
  82. if _, err := kl.iptClient.EnsureChain(utiliptables.TableNAT, KubePostroutingChain); err != nil {
  83. klog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, KubePostroutingChain, err)
  84. return
  85. }
  86. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableNAT, KubeMarkMasqChain, "-j", "MARK", "--set-xmark", masqueradeMark); err != nil {
  87. klog.Errorf("Failed to ensure marking rule for %v: %v", KubeMarkMasqChain, err)
  88. return
  89. }
  90. if _, err := kl.iptClient.EnsureRule(utiliptables.Prepend, utiliptables.TableNAT, utiliptables.ChainPostrouting,
  91. "-m", "comment", "--comment", "kubernetes postrouting rules", "-j", string(KubePostroutingChain)); err != nil {
  92. klog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableNAT, utiliptables.ChainPostrouting, KubePostroutingChain, err)
  93. return
  94. }
  95. // Establish the masquerading rule.
  96. // NB: THIS MUST MATCH the corresponding code in the iptables and ipvs
  97. // modes of kube-proxy
  98. masqRule := []string{
  99. "-m", "comment", "--comment", "kubernetes service traffic requiring SNAT",
  100. "-m", "mark", "--mark", masqueradeMark,
  101. "-j", "MASQUERADE",
  102. }
  103. if kl.iptClient.HasRandomFully() {
  104. masqRule = append(masqRule, "--random-fully")
  105. klog.V(3).Info("Using `--random-fully` in the MASQUERADE rule for iptables")
  106. } else {
  107. klog.V(2).Info("Not using `--random-fully` in the MASQUERADE rule for iptables because the local version of iptables does not support it")
  108. }
  109. if _, err := kl.iptClient.EnsureRule(utiliptables.Append, utiliptables.TableNAT, KubePostroutingChain, masqRule...); err != nil {
  110. klog.Errorf("Failed to ensure SNAT rule for packets marked by %v in %v chain %v: %v", KubeMarkMasqChain, utiliptables.TableNAT, KubePostroutingChain, err)
  111. return
  112. }
  113. }
  114. // getIPTablesMark returns the fwmark given the bit
  115. func getIPTablesMark(bit int) string {
  116. value := 1 << uint(bit)
  117. return fmt.Sprintf("%#08x/%#08x", value, value)
  118. }