TIPS之 Kubernetes client-go 常用配置

Kubernetes client-go 常用配置

Posted by 董江 on Monday, September 18, 2023

Kubernetes client-go 常用配置

第一篇: Kubernetes client-go使用方式

client-go config 配置

type Config struct {
	// Host must be a host string, a host:port pair, or a URL to the base of the apiserver.
	// If a URL is given then the (optional) Path of that URL represents a prefix that must
	// be appended to all request URIs used to access the apiserver. This allows a frontend
	// proxy to easily relocate all of the apiserver endpoints.
	Host string
	// APIPath is a sub-path that points to an API root.
	APIPath string

	// ContentConfig contains settings that affect how objects are transformed when
	// sent to the server.
	ContentConfig

	// Server requires Basic authentication
	Username string
	Password string `datapolicy:"password"`

	// Server requires Bearer authentication. This client will not attempt to use
	// refresh tokens for an OAuth2 flow.
	// TODO: demonstrate an OAuth2 compatible client.
	BearerToken string `datapolicy:"token"`

	// Path to a file containing a BearerToken.
	// If set, the contents are periodically read.
	// The last successfully read value takes precedence over BearerToken.
	BearerTokenFile string

	// Impersonate is the configuration that RESTClient will use for impersonation.
	Impersonate ImpersonationConfig

	// Server requires plugin-specified authentication.
	AuthProvider *clientcmdapi.AuthProviderConfig

	// Callback to persist config for AuthProvider.
	AuthConfigPersister AuthProviderConfigPersister

	// Exec-based authentication provider.
	ExecProvider *clientcmdapi.ExecConfig

	// TLSClientConfig contains settings to enable transport layer security
	TLSClientConfig

	// UserAgent is an optional field that specifies the caller of this request.
	UserAgent string

	// DisableCompression bypasses automatic GZip compression requests to the
	// server.
	DisableCompression bool

	// Transport may be used for custom HTTP behavior. This attribute may not
	// be specified with the TLS client certificate options. Use WrapTransport
	// to provide additional per-server middleware behavior.
	Transport http.RoundTripper
	// WrapTransport will be invoked for custom HTTP behavior after the underlying
	// transport is initialized (either the transport created from TLSClientConfig,
	// Transport, or http.DefaultTransport). The config may layer other RoundTrippers
	// on top of the returned RoundTripper.
	//
	// A future release will change this field to an array. Use config.Wrap()
	// instead of setting this value directly.
	WrapTransport transport.WrapperFunc

	// QPS indicates the maximum QPS to the master from this client.
	// If it's zero, the created RESTClient will use DefaultQPS: 5
	QPS float32

	// Maximum burst for throttle.
	// If it's zero, the created RESTClient will use DefaultBurst: 10.
	Burst int

	// Rate limiter for limiting connections to the master from this client. If present overwrites QPS/Burst
	RateLimiter flowcontrol.RateLimiter

	// WarningHandler handles warnings in server responses.
	// If not set, the default warning handler is used.
	// See documentation for SetDefaultWarningHandler() for details.
	WarningHandler WarningHandler

	// The maximum length of time to wait before giving up on a server request. A value of zero means no timeout.
	Timeout time.Duration

	// Dial specifies the dial function for creating unencrypted TCP connections.
	Dial func(ctx context.Context, network, address string) (net.Conn, error)

	// Proxy is the proxy func to be used for all requests made by this
	// transport. If Proxy is nil, http.ProxyFromEnvironment is used. If Proxy
	// returns a nil *URL, no proxy is used.
	//
	// socks5 proxying does not currently support spdy streaming endpoints.
	Proxy func(*http.Request) (*url.URL, error)

	// Version forces a specific version to be used (if registered)
	// Do we need this?
	// Version string
}

其中常用的:

  1. UserAgent: 配置来源放,用于apiserver识别请求来源。 格式:{client-name}/{version} {from}/{version} 比如:demo1/v0.0.1 Kubernetes/b136f3s
  2. Transport: golang http transport 用于配置http connections;
  3. QPSBurst 单实例qps和burst. 来决定RateLimiter;
  4. Timeout: 请求超时,非http Transport中细力度超时;
  5. Dial: 使用TCP链接;

client-go 环境变量配置: https://pkg.go.dev/k8s.io/apimachinery

  1. KUBE_PATCH_CONVERSION_DETECTOR: 比较PATCH请求冲突,如果出行冲突,则会触发一个 panic. 默认 false
  2. KUBE_CACHE_MUTATION_DETECTOR: sharedIndexInformer 实例化的是 defaultCacheMutationDetector,该实例会以 1s 为间隔,定期执行检查缓存,如果发现缓存被修改,则会触发一个失败处理函数,如果该函数没被定义,则会触发一个 panic. 默认 false
  3. GODEBUG: 开启 golang 中一个控制runtime调度变量的变量. 使用:https://dave.cheney.net/tag/godebug
  4. DEBUG_HTTP2_GOROUTINES: 开启client侧 http2.0 gotrace
  5. NO_PROXYno_proxy: 绕过代理
  6. DISABLE_HTTP2: 配置将默认http2转http1.1,默认值 ''
  7. HTTP2_READ_IDLE_TIMEOUT_SECONDS: http2 healthcheck 时间周期,默认值 30s
  8. HTTP2_PING_TIMEOUT_SECONDS: http2 healthcheck 等待响应时间,默认值 15s
  9. KUBERNETES_SERVICE_HOSTKUBERNETES_SERVICE_PORT: 替换kubconfig中的 host和port;

其他

gce部署集群

「如果这篇文章对你有用,请随意打赏」

Kubeservice博客

如果这篇文章对你有用,请随意打赏

使用微信扫描二维码完成支付