server.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package app does all of the work necessary to configure and run a
  14. // Kubernetes app process.
  15. package app
  16. import (
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. goruntime "runtime"
  23. "strings"
  24. "time"
  25. v1 "k8s.io/api/core/v1"
  26. v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
  27. "k8s.io/apimachinery/pkg/runtime"
  28. "k8s.io/apimachinery/pkg/runtime/serializer"
  29. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  30. "k8s.io/apimachinery/pkg/util/wait"
  31. "k8s.io/apiserver/pkg/server/healthz"
  32. "k8s.io/apiserver/pkg/server/mux"
  33. "k8s.io/apiserver/pkg/server/routes"
  34. utilfeature "k8s.io/apiserver/pkg/util/feature"
  35. "k8s.io/client-go/informers"
  36. clientset "k8s.io/client-go/kubernetes"
  37. v1core "k8s.io/client-go/kubernetes/typed/core/v1"
  38. "k8s.io/client-go/rest"
  39. "k8s.io/client-go/tools/clientcmd"
  40. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  41. "k8s.io/client-go/tools/record"
  42. cliflag "k8s.io/component-base/cli/flag"
  43. componentbaseconfig "k8s.io/component-base/config"
  44. "k8s.io/kube-proxy/config/v1alpha1"
  45. api "k8s.io/kubernetes/pkg/apis/core"
  46. "k8s.io/kubernetes/pkg/kubelet/qos"
  47. "k8s.io/kubernetes/pkg/master/ports"
  48. "k8s.io/kubernetes/pkg/proxy"
  49. "k8s.io/kubernetes/pkg/proxy/apis"
  50. kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
  51. "k8s.io/kubernetes/pkg/proxy/apis/config/scheme"
  52. "k8s.io/kubernetes/pkg/proxy/apis/config/validation"
  53. "k8s.io/kubernetes/pkg/proxy/config"
  54. "k8s.io/kubernetes/pkg/proxy/healthcheck"
  55. "k8s.io/kubernetes/pkg/proxy/iptables"
  56. "k8s.io/kubernetes/pkg/proxy/ipvs"
  57. "k8s.io/kubernetes/pkg/proxy/userspace"
  58. proxyutil "k8s.io/kubernetes/pkg/proxy/util"
  59. "k8s.io/kubernetes/pkg/util/configz"
  60. "k8s.io/kubernetes/pkg/util/filesystem"
  61. utilflag "k8s.io/kubernetes/pkg/util/flag"
  62. utilipset "k8s.io/kubernetes/pkg/util/ipset"
  63. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  64. utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
  65. "k8s.io/kubernetes/pkg/util/oom"
  66. "k8s.io/kubernetes/pkg/util/resourcecontainer"
  67. "k8s.io/kubernetes/pkg/version"
  68. "k8s.io/kubernetes/pkg/version/verflag"
  69. "k8s.io/utils/exec"
  70. utilpointer "k8s.io/utils/pointer"
  71. "github.com/fsnotify/fsnotify"
  72. "github.com/prometheus/client_golang/prometheus"
  73. "github.com/spf13/cobra"
  74. "github.com/spf13/pflag"
  75. "k8s.io/klog"
  76. )
  77. const (
  78. proxyModeUserspace = "userspace"
  79. proxyModeIPTables = "iptables"
  80. proxyModeIPVS = "ipvs"
  81. proxyModeKernelspace = "kernelspace"
  82. )
  83. // proxyRun defines the interface to run a specified ProxyServer
  84. type proxyRun interface {
  85. Run() error
  86. CleanupAndExit() error
  87. }
  88. // Options contains everything necessary to create and run a proxy server.
  89. type Options struct {
  90. // ConfigFile is the location of the proxy server's configuration file.
  91. ConfigFile string
  92. // WriteConfigTo is the path where the default configuration will be written.
  93. WriteConfigTo string
  94. // CleanupAndExit, when true, makes the proxy server clean up iptables rules, then exit.
  95. CleanupAndExit bool
  96. // CleanupIPVS, when true, makes the proxy server clean up ipvs rules before running.
  97. CleanupIPVS bool
  98. // WindowsService should be set to true if kube-proxy is running as a service on Windows.
  99. // Its corresponding flag only gets registered in Windows builds
  100. WindowsService bool
  101. // config is the proxy server's configuration object.
  102. config *kubeproxyconfig.KubeProxyConfiguration
  103. // watcher is used to watch on the update change of ConfigFile
  104. watcher filesystem.FSWatcher
  105. // proxyServer is the interface to run the proxy server
  106. proxyServer proxyRun
  107. // errCh is the channel that errors will be sent
  108. errCh chan error
  109. // The fields below here are placeholders for flags that can't be directly mapped into
  110. // config.KubeProxyConfiguration.
  111. //
  112. // TODO remove these fields once the deprecated flags are removed.
  113. // master is used to override the kubeconfig's URL to the apiserver.
  114. master string
  115. // healthzPort is the port to be used by the healthz server.
  116. healthzPort int32
  117. // metricsPort is the port to be used by the metrics server.
  118. metricsPort int32
  119. scheme *runtime.Scheme
  120. codecs serializer.CodecFactory
  121. // hostnameOverride, if set from the command line flag, takes precedence over the `HostnameOverride` value from the config file
  122. hostnameOverride string
  123. }
  124. // AddFlags adds flags to fs and binds them to options.
  125. func (o *Options) AddFlags(fs *pflag.FlagSet) {
  126. o.addOSFlags(fs)
  127. fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
  128. fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the default configuration values to this file and exit.")
  129. fs.StringVar(&o.config.ClientConnection.Kubeconfig, "kubeconfig", o.config.ClientConnection.Kubeconfig, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
  130. fs.StringVar(&o.config.ClusterCIDR, "cluster-cidr", o.config.ClusterCIDR, "The CIDR range of pods in the cluster. When configured, traffic sent to a Service cluster IP from outside this range will be masqueraded and traffic sent from pods to an external LoadBalancer IP will be directed to the respective cluster IP instead")
  131. fs.StringVar(&o.config.ClientConnection.ContentType, "kube-api-content-type", o.config.ClientConnection.ContentType, "Content type of requests sent to apiserver.")
  132. fs.StringVar(&o.master, "master", o.master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
  133. fs.StringVar(&o.hostnameOverride, "hostname-override", o.hostnameOverride, "If non-empty, will use this string as identification instead of the actual hostname.")
  134. fs.StringVar(&o.config.IPVS.Scheduler, "ipvs-scheduler", o.config.IPVS.Scheduler, "The ipvs scheduler type when proxy mode is ipvs")
  135. fs.StringSliceVar(&o.config.IPVS.ExcludeCIDRs, "ipvs-exclude-cidrs", o.config.IPVS.ExcludeCIDRs, "A comma-separated list of CIDR's which the ipvs proxier should not touch when cleaning up IPVS rules.")
  136. fs.StringSliceVar(&o.config.NodePortAddresses, "nodeport-addresses", o.config.NodePortAddresses,
  137. "A string slice of values which specify the addresses to use for NodePorts. Values may be valid IP blocks (e.g. 1.2.3.0/24, 1.2.3.4/32). The default empty string slice ([]) means to use all local addresses.")
  138. fs.BoolVar(&o.CleanupAndExit, "cleanup", o.CleanupAndExit, "If true cleanup iptables and ipvs rules and exit.")
  139. fs.BoolVar(&o.CleanupIPVS, "cleanup-ipvs", o.CleanupIPVS, "If true and --cleanup is specified, kube-proxy will also flush IPVS rules, in addition to normal cleanup.")
  140. fs.Var(utilflag.IPVar{Val: &o.config.BindAddress}, "bind-address", "The IP address for the proxy server to serve on (set to `0.0.0.0` for all IPv4 interfaces and `::` for all IPv6 interfaces)")
  141. fs.Var(utilflag.IPVar{Val: &o.config.HealthzBindAddress}, "healthz-bind-address", "The IP address for the health check server to serve on (set to `0.0.0.0` for all IPv4 interfaces and `::` for all IPv6 interfaces)")
  142. fs.Var(utilflag.IPVar{Val: &o.config.MetricsBindAddress}, "metrics-bind-address", "The IP address for the metrics server to serve on (set to `0.0.0.0` for all IPv4 interfaces and `::` for all IPv6 interfaces)")
  143. fs.Var(utilflag.PortRangeVar{Val: &o.config.PortRange}, "proxy-port-range", "Range of host ports (beginPort-endPort, single port or beginPort+offset, inclusive) that may be consumed in order to proxy service traffic. If (unspecified, 0, or 0-0) then ports will be randomly chosen.")
  144. fs.Var(&o.config.Mode, "proxy-mode", "Which proxy mode to use: 'userspace' (older) or 'iptables' (faster) or 'ipvs' (experimental). If blank, use the best-available proxy (currently iptables). If the iptables proxy is selected, regardless of how, but the system's kernel or iptables versions are insufficient, this always falls back to the userspace proxy.")
  145. fs.Var(cliflag.NewMapStringBool(&o.config.FeatureGates), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
  146. "Options are:\n"+strings.Join(utilfeature.DefaultFeatureGate.KnownFeatures(), "\n"))
  147. fs.Int32Var(&o.healthzPort, "healthz-port", o.healthzPort, "The port to bind the health check server. Use 0 to disable.")
  148. fs.Int32Var(&o.metricsPort, "metrics-port", o.metricsPort, "The port to bind the metrics server. Use 0 to disable.")
  149. fs.Int32Var(o.config.OOMScoreAdj, "oom-score-adj", utilpointer.Int32PtrDerefOr(o.config.OOMScoreAdj, int32(qos.KubeProxyOOMScoreAdj)), "The oom-score-adj value for kube-proxy process. Values must be within the range [-1000, 1000]")
  150. fs.Int32Var(o.config.IPTables.MasqueradeBit, "iptables-masquerade-bit", utilpointer.Int32PtrDerefOr(o.config.IPTables.MasqueradeBit, 14), "If using the pure iptables proxy, the bit of the fwmark space to mark packets requiring SNAT with. Must be within the range [0, 31].")
  151. fs.Int32Var(o.config.Conntrack.MaxPerCore, "conntrack-max-per-core", *o.config.Conntrack.MaxPerCore,
  152. "Maximum number of NAT connections to track per CPU core (0 to leave the limit as-is and ignore conntrack-min).")
  153. fs.Int32Var(o.config.Conntrack.Min, "conntrack-min", *o.config.Conntrack.Min,
  154. "Minimum number of conntrack entries to allocate, regardless of conntrack-max-per-core (set conntrack-max-per-core=0 to leave the limit as-is).")
  155. fs.Int32Var(&o.config.ClientConnection.Burst, "kube-api-burst", o.config.ClientConnection.Burst, "Burst to use while talking with kubernetes apiserver")
  156. fs.DurationVar(&o.config.IPTables.SyncPeriod.Duration, "iptables-sync-period", o.config.IPTables.SyncPeriod.Duration, "The maximum interval of how often iptables rules are refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")
  157. fs.DurationVar(&o.config.IPTables.MinSyncPeriod.Duration, "iptables-min-sync-period", o.config.IPTables.MinSyncPeriod.Duration, "The minimum interval of how often the iptables rules can be refreshed as endpoints and services change (e.g. '5s', '1m', '2h22m').")
  158. fs.DurationVar(&o.config.IPVS.SyncPeriod.Duration, "ipvs-sync-period", o.config.IPVS.SyncPeriod.Duration, "The maximum interval of how often ipvs rules are refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")
  159. fs.DurationVar(&o.config.IPVS.MinSyncPeriod.Duration, "ipvs-min-sync-period", o.config.IPVS.MinSyncPeriod.Duration, "The minimum interval of how often the ipvs rules can be refreshed as endpoints and services change (e.g. '5s', '1m', '2h22m').")
  160. fs.DurationVar(&o.config.Conntrack.TCPEstablishedTimeout.Duration, "conntrack-tcp-timeout-established", o.config.Conntrack.TCPEstablishedTimeout.Duration, "Idle timeout for established TCP connections (0 to leave as-is)")
  161. fs.DurationVar(
  162. &o.config.Conntrack.TCPCloseWaitTimeout.Duration, "conntrack-tcp-timeout-close-wait",
  163. o.config.Conntrack.TCPCloseWaitTimeout.Duration,
  164. "NAT timeout for TCP connections in the CLOSE_WAIT state")
  165. fs.DurationVar(&o.config.ConfigSyncPeriod.Duration, "config-sync-period", o.config.ConfigSyncPeriod.Duration, "How often configuration from the apiserver is refreshed. Must be greater than 0.")
  166. fs.DurationVar(&o.config.UDPIdleTimeout.Duration, "udp-timeout", o.config.UDPIdleTimeout.Duration, "How long an idle UDP connection will be kept open (e.g. '250ms', '2s'). Must be greater than 0. Only applicable for proxy-mode=userspace")
  167. fs.BoolVar(&o.config.IPVS.StrictARP, "ipvs-strict-arp", o.config.IPVS.StrictARP, "Enable strict ARP by setting arp_ignore to 1 and arp_announce to 2")
  168. fs.BoolVar(&o.config.IPTables.MasqueradeAll, "masquerade-all", o.config.IPTables.MasqueradeAll, "If using the pure iptables proxy, SNAT all traffic sent via Service cluster IPs (this not commonly needed)")
  169. fs.BoolVar(&o.config.EnableProfiling, "profiling", o.config.EnableProfiling, "If true enables profiling via web interface on /debug/pprof handler.")
  170. fs.Float32Var(&o.config.ClientConnection.QPS, "kube-api-qps", o.config.ClientConnection.QPS, "QPS to use while talking with kubernetes apiserver")
  171. // All flags below here are deprecated and will eventually be removed.
  172. fs.StringVar(&o.config.ResourceContainer, "resource-container", o.config.ResourceContainer, "Absolute name of the resource-only container to create and run the Kube-proxy in (Default: /kube-proxy).")
  173. fs.MarkDeprecated("resource-container", "This feature will be removed in a later release.")
  174. }
  175. // NewOptions returns initialized Options
  176. func NewOptions() *Options {
  177. return &Options{
  178. config: new(kubeproxyconfig.KubeProxyConfiguration),
  179. healthzPort: ports.ProxyHealthzPort,
  180. metricsPort: ports.ProxyStatusPort,
  181. scheme: scheme.Scheme,
  182. codecs: scheme.Codecs,
  183. CleanupIPVS: true,
  184. errCh: make(chan error),
  185. }
  186. }
  187. // Complete completes all the required options.
  188. func (o *Options) Complete() error {
  189. if len(o.ConfigFile) == 0 && len(o.WriteConfigTo) == 0 {
  190. klog.Warning("WARNING: all flags other than --config, --write-config-to, and --cleanup are deprecated. Please begin using a config file ASAP.")
  191. o.config.HealthzBindAddress = addressFromDeprecatedFlags(o.config.HealthzBindAddress, o.healthzPort)
  192. o.config.MetricsBindAddress = addressFromDeprecatedFlags(o.config.MetricsBindAddress, o.metricsPort)
  193. }
  194. // Load the config file here in Complete, so that Validate validates the fully-resolved config.
  195. if len(o.ConfigFile) > 0 {
  196. c, err := o.loadConfigFromFile(o.ConfigFile)
  197. if err != nil {
  198. return err
  199. }
  200. o.config = c
  201. if err := o.initWatcher(); err != nil {
  202. return err
  203. }
  204. }
  205. if err := o.processHostnameOverrideFlag(); err != nil {
  206. return err
  207. }
  208. return utilfeature.DefaultMutableFeatureGate.SetFromMap(o.config.FeatureGates)
  209. }
  210. // Creates a new filesystem watcher and adds watches for the config file.
  211. func (o *Options) initWatcher() error {
  212. fswatcher := filesystem.NewFsnotifyWatcher()
  213. err := fswatcher.Init(o.eventHandler, o.errorHandler)
  214. if err != nil {
  215. return err
  216. }
  217. err = fswatcher.AddWatch(o.ConfigFile)
  218. if err != nil {
  219. return err
  220. }
  221. o.watcher = fswatcher
  222. return nil
  223. }
  224. func (o *Options) eventHandler(ent fsnotify.Event) {
  225. eventOpIs := func(Op fsnotify.Op) bool {
  226. return ent.Op&Op == Op
  227. }
  228. if eventOpIs(fsnotify.Write) || eventOpIs(fsnotify.Rename) {
  229. // error out when ConfigFile is updated
  230. o.errCh <- fmt.Errorf("content of the proxy server's configuration file was updated")
  231. }
  232. o.errCh <- nil
  233. }
  234. func (o *Options) errorHandler(err error) {
  235. o.errCh <- err
  236. }
  237. // processHostnameOverrideFlag processes hostname-override flag
  238. func (o *Options) processHostnameOverrideFlag() error {
  239. // Check if hostname-override flag is set and use value since configFile always overrides
  240. if len(o.hostnameOverride) > 0 {
  241. hostName := strings.TrimSpace(o.hostnameOverride)
  242. if len(hostName) == 0 {
  243. return fmt.Errorf("empty hostname-override is invalid")
  244. }
  245. o.config.HostnameOverride = strings.ToLower(hostName)
  246. }
  247. return nil
  248. }
  249. // Validate validates all the required options.
  250. func (o *Options) Validate(args []string) error {
  251. if len(args) != 0 {
  252. return errors.New("no arguments are supported")
  253. }
  254. if errs := validation.Validate(o.config); len(errs) != 0 {
  255. return errs.ToAggregate()
  256. }
  257. return nil
  258. }
  259. // Run runs the specified ProxyServer.
  260. func (o *Options) Run() error {
  261. defer close(o.errCh)
  262. if len(o.WriteConfigTo) > 0 {
  263. return o.writeConfigFile()
  264. }
  265. proxyServer, err := NewProxyServer(o)
  266. if err != nil {
  267. return err
  268. }
  269. if o.CleanupAndExit {
  270. return proxyServer.CleanupAndExit()
  271. }
  272. o.proxyServer = proxyServer
  273. return o.runLoop()
  274. }
  275. // runLoop will watch on the update change of the proxy server's configuration file.
  276. // Return an error when updated
  277. func (o *Options) runLoop() error {
  278. if o.watcher != nil {
  279. o.watcher.Run()
  280. }
  281. // run the proxy in goroutine
  282. go func() {
  283. err := o.proxyServer.Run()
  284. o.errCh <- err
  285. }()
  286. for {
  287. err := <-o.errCh
  288. if err != nil {
  289. return err
  290. }
  291. }
  292. }
  293. func (o *Options) writeConfigFile() (err error) {
  294. const mediaType = runtime.ContentTypeYAML
  295. info, ok := runtime.SerializerInfoForMediaType(o.codecs.SupportedMediaTypes(), mediaType)
  296. if !ok {
  297. return fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
  298. }
  299. encoder := o.codecs.EncoderForVersion(info.Serializer, v1alpha1.SchemeGroupVersion)
  300. configFile, err := os.Create(o.WriteConfigTo)
  301. if err != nil {
  302. return err
  303. }
  304. defer func() {
  305. ferr := configFile.Close()
  306. if ferr != nil && err == nil {
  307. err = ferr
  308. }
  309. }()
  310. if err = encoder.Encode(o.config, configFile); err != nil {
  311. return err
  312. }
  313. klog.Infof("Wrote configuration to: %s\n", o.WriteConfigTo)
  314. return nil
  315. }
  316. // addressFromDeprecatedFlags returns server address from flags
  317. // passed on the command line based on the following rules:
  318. // 1. If port is 0, disable the server (e.g. set address to empty).
  319. // 2. Otherwise, set the port portion of the config accordingly.
  320. func addressFromDeprecatedFlags(addr string, port int32) string {
  321. if port == 0 {
  322. return ""
  323. }
  324. return proxyutil.AppendPortIfNeeded(addr, port)
  325. }
  326. // loadConfigFromFile loads the contents of file and decodes it as a
  327. // KubeProxyConfiguration object.
  328. func (o *Options) loadConfigFromFile(file string) (*kubeproxyconfig.KubeProxyConfiguration, error) {
  329. data, err := ioutil.ReadFile(file)
  330. if err != nil {
  331. return nil, err
  332. }
  333. return o.loadConfig(data)
  334. }
  335. // loadConfig decodes data as a KubeProxyConfiguration object.
  336. func (o *Options) loadConfig(data []byte) (*kubeproxyconfig.KubeProxyConfiguration, error) {
  337. configObj, gvk, err := o.codecs.UniversalDecoder().Decode(data, nil, nil)
  338. if err != nil {
  339. return nil, err
  340. }
  341. proxyConfig, ok := configObj.(*kubeproxyconfig.KubeProxyConfiguration)
  342. if !ok {
  343. return nil, fmt.Errorf("got unexpected config type: %v", gvk)
  344. }
  345. return proxyConfig, nil
  346. }
  347. // ApplyDefaults applies the default values to Options.
  348. func (o *Options) ApplyDefaults(in *kubeproxyconfig.KubeProxyConfiguration) (*kubeproxyconfig.KubeProxyConfiguration, error) {
  349. external, err := o.scheme.ConvertToVersion(in, v1alpha1.SchemeGroupVersion)
  350. if err != nil {
  351. return nil, err
  352. }
  353. o.scheme.Default(external)
  354. internal, err := o.scheme.ConvertToVersion(external, kubeproxyconfig.SchemeGroupVersion)
  355. if err != nil {
  356. return nil, err
  357. }
  358. out := internal.(*kubeproxyconfig.KubeProxyConfiguration)
  359. return out, nil
  360. }
  361. // NewProxyCommand creates a *cobra.Command object with default parameters
  362. func NewProxyCommand() *cobra.Command {
  363. opts := NewOptions()
  364. cmd := &cobra.Command{
  365. Use: "kube-proxy",
  366. Long: `The Kubernetes network proxy runs on each node. This
  367. reflects services as defined in the Kubernetes API on each node and can do simple
  368. TCP, UDP, and SCTP stream forwarding or round robin TCP, UDP, and SCTP forwarding across a set of backends.
  369. Service cluster IPs and ports are currently found through Docker-links-compatible
  370. environment variables specifying ports opened by the service proxy. There is an optional
  371. addon that provides cluster DNS for these cluster IPs. The user must create a service
  372. with the apiserver API to configure the proxy.`,
  373. Run: func(cmd *cobra.Command, args []string) {
  374. verflag.PrintAndExitIfRequested()
  375. utilflag.PrintFlags(cmd.Flags())
  376. if err := initForOS(opts.WindowsService); err != nil {
  377. klog.Fatalf("failed OS init: %v", err)
  378. }
  379. if err := opts.Complete(); err != nil {
  380. klog.Fatalf("failed complete: %v", err)
  381. }
  382. if err := opts.Validate(args); err != nil {
  383. klog.Fatalf("failed validate: %v", err)
  384. }
  385. klog.Fatal(opts.Run())
  386. },
  387. }
  388. var err error
  389. opts.config, err = opts.ApplyDefaults(opts.config)
  390. if err != nil {
  391. klog.Fatalf("unable to create flag defaults: %v", err)
  392. }
  393. opts.AddFlags(cmd.Flags())
  394. // TODO handle error
  395. cmd.MarkFlagFilename("config", "yaml", "yml", "json")
  396. return cmd
  397. }
  398. // ProxyServer represents all the parameters required to start the Kubernetes proxy server. All
  399. // fields are required.
  400. type ProxyServer struct {
  401. Client clientset.Interface
  402. EventClient v1core.EventsGetter
  403. IptInterface utiliptables.Interface
  404. IpvsInterface utilipvs.Interface
  405. IpsetInterface utilipset.Interface
  406. execer exec.Interface
  407. Proxier proxy.ProxyProvider
  408. Broadcaster record.EventBroadcaster
  409. Recorder record.EventRecorder
  410. ConntrackConfiguration kubeproxyconfig.KubeProxyConntrackConfiguration
  411. Conntracker Conntracker // if nil, ignored
  412. ProxyMode string
  413. NodeRef *v1.ObjectReference
  414. CleanupIPVS bool
  415. MetricsBindAddress string
  416. EnableProfiling bool
  417. OOMScoreAdj *int32
  418. ResourceContainer string
  419. ConfigSyncPeriod time.Duration
  420. HealthzServer *healthcheck.HealthzServer
  421. }
  422. // createClients creates a kube client and an event client from the given config and masterOverride.
  423. // TODO remove masterOverride when CLI flags are removed.
  424. func createClients(config componentbaseconfig.ClientConnectionConfiguration, masterOverride string) (clientset.Interface, v1core.EventsGetter, error) {
  425. var kubeConfig *rest.Config
  426. var err error
  427. if len(config.Kubeconfig) == 0 && len(masterOverride) == 0 {
  428. klog.Info("Neither kubeconfig file nor master URL was specified. Falling back to in-cluster config.")
  429. kubeConfig, err = rest.InClusterConfig()
  430. } else {
  431. // This creates a client, first loading any specified kubeconfig
  432. // file, and then overriding the Master flag, if non-empty.
  433. kubeConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
  434. &clientcmd.ClientConfigLoadingRules{ExplicitPath: config.Kubeconfig},
  435. &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterOverride}}).ClientConfig()
  436. }
  437. if err != nil {
  438. return nil, nil, err
  439. }
  440. kubeConfig.AcceptContentTypes = config.AcceptContentTypes
  441. kubeConfig.ContentType = config.ContentType
  442. kubeConfig.QPS = config.QPS
  443. kubeConfig.Burst = int(config.Burst)
  444. client, err := clientset.NewForConfig(kubeConfig)
  445. if err != nil {
  446. return nil, nil, err
  447. }
  448. eventClient, err := clientset.NewForConfig(kubeConfig)
  449. if err != nil {
  450. return nil, nil, err
  451. }
  452. return client, eventClient.CoreV1(), nil
  453. }
  454. // Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
  455. // TODO: At the moment, Run() cannot return a nil error, otherwise it's caller will never exit. Update callers of Run to handle nil errors.
  456. func (s *ProxyServer) Run() error {
  457. // To help debugging, immediately log version
  458. klog.Infof("Version: %+v", version.Get())
  459. // TODO(vmarmol): Use container config for this.
  460. var oomAdjuster *oom.OOMAdjuster
  461. if s.OOMScoreAdj != nil {
  462. oomAdjuster = oom.NewOOMAdjuster()
  463. if err := oomAdjuster.ApplyOOMScoreAdj(0, int(*s.OOMScoreAdj)); err != nil {
  464. klog.V(2).Info(err)
  465. }
  466. }
  467. if len(s.ResourceContainer) != 0 {
  468. // Run in its own container.
  469. if err := resourcecontainer.RunInResourceContainer(s.ResourceContainer); err != nil {
  470. klog.Warningf("Failed to start in resource-only container %q: %v", s.ResourceContainer, err)
  471. } else {
  472. klog.V(2).Infof("Running in resource-only container %q", s.ResourceContainer)
  473. }
  474. }
  475. if s.Broadcaster != nil && s.EventClient != nil {
  476. s.Broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: s.EventClient.Events("")})
  477. }
  478. // Start up a healthz server if requested
  479. if s.HealthzServer != nil {
  480. s.HealthzServer.Run()
  481. }
  482. // Start up a metrics server if requested
  483. if len(s.MetricsBindAddress) > 0 {
  484. proxyMux := mux.NewPathRecorderMux("kube-proxy")
  485. healthz.InstallHandler(proxyMux)
  486. proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
  487. fmt.Fprintf(w, "%s", s.ProxyMode)
  488. })
  489. proxyMux.Handle("/metrics", prometheus.Handler())
  490. if s.EnableProfiling {
  491. routes.Profiling{}.Install(proxyMux)
  492. }
  493. configz.InstallHandler(proxyMux)
  494. go wait.Until(func() {
  495. err := http.ListenAndServe(s.MetricsBindAddress, proxyMux)
  496. if err != nil {
  497. utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
  498. }
  499. }, 5*time.Second, wait.NeverStop)
  500. }
  501. // Tune conntrack, if requested
  502. // Conntracker is always nil for windows
  503. if s.Conntracker != nil {
  504. max, err := getConntrackMax(s.ConntrackConfiguration)
  505. if err != nil {
  506. return err
  507. }
  508. if max > 0 {
  509. err := s.Conntracker.SetMax(max)
  510. if err != nil {
  511. if err != errReadOnlySysFS {
  512. return err
  513. }
  514. // errReadOnlySysFS is caused by a known docker issue (https://github.com/docker/docker/issues/24000),
  515. // the only remediation we know is to restart the docker daemon.
  516. // Here we'll send an node event with specific reason and message, the
  517. // administrator should decide whether and how to handle this issue,
  518. // whether to drain the node and restart docker. Occurs in other container runtimes
  519. // as well.
  520. // TODO(random-liu): Remove this when the docker bug is fixed.
  521. const message = "CRI error: /sys is read-only: " +
  522. "cannot modify conntrack limits, problems may arise later (If running Docker, see docker issue #24000)"
  523. s.Recorder.Eventf(s.NodeRef, api.EventTypeWarning, err.Error(), message)
  524. }
  525. }
  526. if s.ConntrackConfiguration.TCPEstablishedTimeout != nil && s.ConntrackConfiguration.TCPEstablishedTimeout.Duration > 0 {
  527. timeout := int(s.ConntrackConfiguration.TCPEstablishedTimeout.Duration / time.Second)
  528. if err := s.Conntracker.SetTCPEstablishedTimeout(timeout); err != nil {
  529. return err
  530. }
  531. }
  532. if s.ConntrackConfiguration.TCPCloseWaitTimeout != nil && s.ConntrackConfiguration.TCPCloseWaitTimeout.Duration > 0 {
  533. timeout := int(s.ConntrackConfiguration.TCPCloseWaitTimeout.Duration / time.Second)
  534. if err := s.Conntracker.SetTCPCloseWaitTimeout(timeout); err != nil {
  535. return err
  536. }
  537. }
  538. }
  539. informerFactory := informers.NewSharedInformerFactoryWithOptions(s.Client, s.ConfigSyncPeriod,
  540. informers.WithTweakListOptions(func(options *v1meta.ListOptions) {
  541. options.LabelSelector = "!" + apis.LabelServiceProxyName
  542. }))
  543. // Create configs (i.e. Watches for Services and Endpoints)
  544. // Note: RegisterHandler() calls need to happen before creation of Sources because sources
  545. // only notify on changes, and the initial update (on process start) may be lost if no handlers
  546. // are registered yet.
  547. serviceConfig := config.NewServiceConfig(informerFactory.Core().V1().Services(), s.ConfigSyncPeriod)
  548. serviceConfig.RegisterEventHandler(s.Proxier)
  549. go serviceConfig.Run(wait.NeverStop)
  550. endpointsConfig := config.NewEndpointsConfig(informerFactory.Core().V1().Endpoints(), s.ConfigSyncPeriod)
  551. endpointsConfig.RegisterEventHandler(s.Proxier)
  552. go endpointsConfig.Run(wait.NeverStop)
  553. // This has to start after the calls to NewServiceConfig and NewEndpointsConfig because those
  554. // functions must configure their shared informer event handlers first.
  555. informerFactory.Start(wait.NeverStop)
  556. // Birth Cry after the birth is successful
  557. s.birthCry()
  558. // Just loop forever for now...
  559. s.Proxier.SyncLoop()
  560. return nil
  561. }
  562. func (s *ProxyServer) birthCry() {
  563. s.Recorder.Eventf(s.NodeRef, api.EventTypeNormal, "Starting", "Starting kube-proxy.")
  564. }
  565. func getConntrackMax(config kubeproxyconfig.KubeProxyConntrackConfiguration) (int, error) {
  566. if config.MaxPerCore != nil && *config.MaxPerCore > 0 {
  567. floor := 0
  568. if config.Min != nil {
  569. floor = int(*config.Min)
  570. }
  571. scaled := int(*config.MaxPerCore) * goruntime.NumCPU()
  572. if scaled > floor {
  573. klog.V(3).Infof("getConntrackMax: using scaled conntrack-max-per-core")
  574. return scaled, nil
  575. }
  576. klog.V(3).Infof("getConntrackMax: using conntrack-min")
  577. return floor, nil
  578. }
  579. return 0, nil
  580. }
  581. // CleanupAndExit remove iptables rules and exit if success return nil
  582. func (s *ProxyServer) CleanupAndExit() error {
  583. encounteredError := userspace.CleanupLeftovers(s.IptInterface)
  584. encounteredError = iptables.CleanupLeftovers(s.IptInterface) || encounteredError
  585. encounteredError = ipvs.CleanupLeftovers(s.IpvsInterface, s.IptInterface, s.IpsetInterface, s.CleanupIPVS) || encounteredError
  586. if encounteredError {
  587. return errors.New("encountered an error while tearing down rules")
  588. }
  589. return nil
  590. }