}
Resource
- Resource资源在kubernetes中的重要性是不言而喻的,常见的pod、service、deployment这些都是资源,下面是关于资源的一些小结:
-
在kubernetes环境被实例化的资源即资源对象(ResourceObject);
-
资源被分为持久性(Persistent Entity)和非持久性(Ephemeral Entity),持久性如deployment,创建后会在etcd保存,非持久性如pod;
- 资源的源码:
type APIResource struct {
Name string json:"name" protobuf:"bytes,1,opt,name=name"
SingularName string json:"singularName" protobuf:"bytes,6,opt,name=singularName"
Namespaced bool json:"namespaced" protobuf:"varint,2,opt,name=namespaced"
Group string json:"group,omitempty" protobuf:"bytes,8,opt,name=group"
Version string json:"version,omitempty" protobuf:"bytes,9,opt,name=version"
Kind string json:"kind" protobuf:"bytes,3,opt,name=kind"
Verbs Verbs json:"verbs" protobuf:"bytes,4,opt,name=verbs"
ShortNames []string json:"shortNames,omitempty" protobuf:"bytes,5,rep,name=shortNames"
Categories []string json:"categories,omitempty" protobuf:"bytes,7,rep,name=categories"
StorageVersionHash string json:"storageVersionHash,omitempty" protobuf:"bytes,10,opt,name=storageVersionHash"
}
-
kubernetes为资源准备了8种操作:create、delete、deletecollection、get、list、patch、update、watch,每一种资源都支持其中的一部分,这在每个资源的API文档中可以看到;
-
资源支持以命名空间(namespace)进行隔离;
-
资源对象描述文件在日常操作中频繁用到,一共由五部分组成:apiVersion、kind、metadata、spec、status,下图是官方的deployment描述文件,用于创建3个nginx pod,对着红框和文字就了解每个部分的作用了:
- 上图并没有status,该部分是用来反应当前资源对象状态的,体现在资源的数据结构中,如下所示:
type Deployment struct {
metav1.TypeMeta json:",inline"
metav1.ObjectMeta json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"
Spec DeploymentSpec json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"
Status DeploymentStatus json:"status,omitempty" protobuf:"bytes,3,opt,name=status"
}
官方文档速查
-
在实际学习和开发中,对指定资源做增删改查操作时,官方文档是我们最可靠的依赖,地址:https://kubernetes.io/docs/reference/kubernetes-api/
-
打开deployment的文档,如下图:
-
另外还有API文档也是必不可少的,遗憾的是目前没有找到1.20的API在线文档,最新的是1.19版本,地址:https://v1-19.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/
-
下图是deployment的api接口文档,可见示例、path、请求响应参数都有详细的说明,对咱们的学习和开发提供了强有力的支持:
APIResources数据结构
- APIResource是个常用的数据结构了,可以用来描述资源,例如resource_quota_controller_test.go中有对其的使用:
func TestDiscoverySync(t *testing.T) {
serverResources := []*metav1.APIResourceList{
{
GroupVersion: “v1”,
APIResources: []metav1.APIResource{
{Name: “pods”, Namespaced: true, Kind: “Pod”, Verbs: metav1.Verbs{“create”, “delete”, “list”, “watch”}},
},
},
}
unsyncableServerResources := []*metav1.APIResourceList{
{
GroupVersion: “v1”,
APIResources: []metav1.APIResource{
{Name: “pods”, Namespaced: true, Kind: “Pod”, Verbs: metav1.Verbs{“create”, “delete”, “list”, “watch”}},
{Name: “secrets”, Namespaced: true, Kind: “Secret”, Verbs: metav1.Verbs{“create”, “delete”, “list”, “watch”}},
},
},
}
实际操作
-
聊了这么久理论,接下来咱们在kubernetes环境执行一些和资源有关的操作;
-
查看所有资源kubectl api-resources -o wide,可见当前环境的所有资源,及其相关属性:
[root@kubebuilder 07]# kubectl api-resources -o wide
NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS
bindings v1 true Binding [create]
componentstatuses cs v1 false ComponentStatus [get list]
configmaps cm v1 true ConfigMap [create delete deletecollection get list patch update watch]
endpoints ep v1 true Endpoints [create delete deletecollection get list patch update watch]
events ev v1 true Event [create delete deletecollection get list patch update watch]
limitranges limits v1 true LimitRange [create delete deletecollection get list patch update watch]
namespaces ns v1 false Namespace [create delete get list patch update watch]
…
- 只看apps这个group下面的资源kubectl api-resources --api-group apps -o wide:
[root@kubebuilder 07]# kubectl api-resources --api-group apps -o wide
NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS
controllerrevisions apps/v1 true ControllerRevision [create delete deletecollection get list patch update watch]
daemonsets ds apps/v1 true DaemonSet [create delete deletecollection get list patch update watch]
deployments deploy apps/v1 true Deployment [create delete deletecollection get list patch update watch]
replicasets rs apps/v1 true ReplicaSet [create delete deletecollection get list patch update watch]
statefulsets sts apps/v1 true StatefulSet [create delete deletecollection get list patch update watch]
- 查看指定资源的详情kubectl explain configmap:
[root@kubebuilder 07]# kubectl explain configmap
KIND: ConfigMap
VERSION: v1
DESCRIPTION:
ConfigMap holds configuration data for pods to consume.
FIELDS:
apiVersion
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
binaryData <map[string]string>
…
- 查看所有Group和Version的命令kubectl api-versions
[root@kubebuilder 07]# kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
言尽于此,完结
无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。
- 第一,设计模式能让专业人之间交流方便,如下:
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
搜集费时费力,能看到此处的都是真爱!
计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
[外链图片转存中…(img-WNDPAwta-1714412114880)]
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
[外链图片转存中…(img-Cvx6FZZU-1714412114880)]
搜集费时费力,能看到此处的都是真爱!