k8s研发领域概念
基本概念 - 资源组、资源版本、资源、子资源 -> 表现形式为<group>/<version>/<resource>/<subresource>
比如: apps/v1/deployments/status; apps/v1,Kind=Deployment
常用的资源
类别 | 名称 |
工作负载型资源对象 | Pod、Replicaset、ReplicationController、Deployments、StatefulSets、Daemonset、Job、CronJob |
服务发现及负载均衡 | Service、Ingress |
配置与存储 | Volume、Persistent Volume、CSl 、configmap、secret |
集群资源 | Namespace、Node、Role、ClusterRole、RoleBinding、ClusterRoleBinding |
元数据资源 | HPA、PodTemplate、LimitRang |
资源又分为内置资源或外部Custom Resource自定义资源
资源之上又有支持的操作 - create、delete、deletecollection、get、list、patch、update、watch
资源列表长啥样
{ "kind": "APIResourceList", "apiVersion": "v1", "groupVersion": "metrics.k8s.io/v1beta1", "resources": [ { "name": "nodes", "singularName": "", "namespaced": false, "kind": "NodeMetrics", "verbs": [ "get", "list" ] }, { "name": "pods", "singularName": "", "namespaced": true, "kind": "PodMetrics", "verbs": [ "get", "list" ] } ]
所以资源都是go的一种types xxx struct结构体,register.go是将资源注册到schema
storage/store操作资源
codec编解码器
pb序列化反序列化
convert转换各个版本
命令执行是怎么玩的?Cobar!也现成的其他包
突然想先看一个源码cmd目录的apiserver文件~
import ( "math/rand" "os" "time" "github.com/spf13/pflag" cliflag "k8s.io/component-base/cli/flag" "k8s.io/component-base/logs" _ "k8s.io/component-base/logs/json/register" // for JSON log format registration _ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration "k8s.io/kubernetes/cmd/kube-apiserver/app" ) func main() { rand.Seed(time.Now().UnixNano()) pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) command := app.NewAPIServerCommand() logs.InitLogs() defer logs.FlushLogs() if err := command.Execute(); err != nil { os.Exit(1) } }
k8s源码大差不差,套路也类似,启动点基本都是这样,命令实际长这样
func NewAPIServerCommand() *cobra.Command { s := options.NewServerRunOptions() cmd := &cobra.Command{ Use: "kube-apiserver", Long: `The Kubernetes API server validates and configures data for the api objects which include pods, services, replicationcontrollers, and others. The API Server services REST operations and provides the frontend to the cluster's shared state through which all other components interact.`, // stop printing usage when the command errors SilenceUsage: true, PersistentPreRunE: func(*cobra.Command, []string) error { // silence client-go warnings. // kube-apiserver loopback clients should not log self-issued warnings. rest.SetDefaultWarningHandler(rest.NoWarnings{}) return nil }, RunE: func(cmd *cobra.Command, args []string) error { verflag.PrintAndExitIfRequested() fs := cmd.Flags() cliflag.PrintFlags(fs) err := checkNonZeroInsecurePort(fs) if err != nil { return err } // set default options completedOptions, err := Complete(s) if err != nil { return err } // validate options if errs := completedOptions.Validate(); len(errs) != 0 { return utilerrors.NewAggregate(errs) } return Run(completedOptions, genericapiserver.SetupSignalHandler()) }, Args: func(cmd *cobra.Command, args []string) error { for _, arg := range args { if len(arg) > 0 { return fmt.Errorf("%q does not take any arguments, got %q", cmd.CommandPath(), args) } } return nil }, } fs := cmd.Flags() namedFlagSets := s.Flags() verflag.AddFlags(namedFlagSets.FlagSet("global")) globalflag.AddGlobalFlags(namedFlagSets.FlagSet("global"), cmd.Name()) options.AddCustomGlobalFlags(namedFlagSets.FlagSet("generic")) for _, f := range namedFlagSets.FlagSets { fs.AddFlagSet(f) } cols, _, _ := term.TerminalSize(cmd.OutOrStdout()) cliflag.SetUsageAndHelpFunc(cmd, namedFlagSets, cols) return cmd }