背景说明
解决方案
版本查看
[root@cka-master ~]# kubectl api-versionsadmissionregistration.k8s.io/v1apiextensions.k8s.io/v1apiregistration.k8s.io/v1apps/v1authentication.k8s.io/v1authorization.k8s.io/v1autoscaling/v1autoscaling/v2beta1autoscaling/v2beta2batch/v1batch/v1beta1certificates.k8s.io/v1coordination.k8s.io/v1crd.projectcalico.org/v1discovery.k8s.io/v1discovery.k8s.io/v1beta1events.k8s.io/v1events.k8s.io/v1beta1flowcontrol.apiserver.k8s.io/v1beta1networking.k8s.io/v1node.k8s.io/v1node.k8s.io/v1beta1policy/v1policy/v1beta1rbac.authorization.k8s.io/v1scheduling.k8s.io/v1storage.k8s.io/v1storage.k8s.io/v1beta1v1[root@cka-master ~]#
版本选择,针对Deployment来说
| 序号 | 版本 | 备注 |
|---|---|---|
| 1 | extensions/v1beta1 | 1.6版本之前 |
| 2 | apps/v1beta1 | 1.6版本到1.9版本 |
| 3 | apps/v1 | 1.9 |
资源注册
通过kubectl api-resources可以看到已经注册的API资源
[root@cka-master ~]# kubectl api-resourcesNAME SHORTNAMES APIVERSION NAMESPACED KINDbindings v1 true Bindingcomponentstatuses cs v1 false ComponentStatusconfigmaps cm v1 true ConfigMapendpoints ep v1 true Endpointsevents ev v1 true Eventlimitranges limits v1 true LimitRangenamespaces ns v1 false Namespacenodes no v1 false Nodepersistentvolumeclaims pvc v1 true PersistentVolumeClaimpersistentvolumes pv v1 false PersistentVolumepods po v1 true Podpodtemplates v1 true PodTemplatereplicationcontrollers rc v1 true ReplicationControllerresourcequotas quota v1 true ResourceQuotasecrets v1 true Secretserviceaccounts sa v1 true ServiceAccountservices svc v1 true Servicemutatingwebhookconfigurations admissionregistration.k8s.io/v1 false MutatingWebhookConfigurationvalidatingwebhookconfigurations admissionregistration.k8s.io/v1 false ValidatingWebhookConfigurationcustomresourcedefinitions crd,crds apiextensions.k8s.io/v1 false CustomResourceDefinitionapiservices apiregistration.k8s.io/v1 false APIServicecontrollerrevisions apps/v1 true ControllerRevisiondaemonsets ds apps/v1 true DaemonSetdeployments deploy apps/v1 true Deploymentreplicasets rs apps/v1 true ReplicaSetstatefulsets sts apps/v1 true StatefulSettokenreviews authentication.k8s.io/v1 false TokenReviewlocalsubjectaccessreviews authorization.k8s.io/v1 true LocalSubjectAccessReviewselfsubjectaccessreviews authorization.k8s.io/v1 false SelfSubjectAccessReviewselfsubjectrulesreviews authorization.k8s.io/v1 false SelfSubjectRulesReviewsubjectaccessreviews authorization.k8s.io/v1 false SubjectAccessReviewhorizontalpodautoscalers hpa autoscaling/v1 true HorizontalPodAutoscalercronjobs cj batch/v1 true CronJobjobs batch/v1 true Jobcertificatesigningrequests csr certificates.k8s.io/v1 false CertificateSigningRequestleases coordination.k8s.io/v1 true Leasebgpconfigurations crd.projectcalico.org/v1 false BGPConfigurationbgppeers crd.projectcalico.org/v1 false BGPPeerblockaffinities crd.projectcalico.org/v1 false BlockAffinitycaliconodestatuses crd.projectcalico.org/v1 false CalicoNodeStatusclusterinformations crd.projectcalico.org/v1 false ClusterInformationfelixconfigurations crd.projectcalico.org/v1 false FelixConfigurationglobalnetworkpolicies crd.projectcalico.org/v1 false GlobalNetworkPolicyglobalnetworksets crd.projectcalico.org/v1 false GlobalNetworkSethostendpoints crd.projectcalico.org/v1 false HostEndpointipamblocks crd.projectcalico.org/v1 false IPAMBlockipamconfigs crd.projectcalico.org/v1 false IPAMConfigipamhandles crd.projectcalico.org/v1 false IPAMHandleippools crd.projectcalico.org/v1 false IPPoolipreservations crd.projectcalico.org/v1 false IPReservationkubecontrollersconfigurations crd.projectcalico.org/v1 false KubeControllersConfigurationnetworkpolicies crd.projectcalico.org/v1 true NetworkPolicynetworksets crd.projectcalico.org/v1 true NetworkSetendpointslices discovery.k8s.io/v1 true EndpointSliceevents ev events.k8s.io/v1 true Eventflowschemas flowcontrol.apiserver.k8s.io/v1beta1 false FlowSchemaprioritylevelconfigurations flowcontrol.apiserver.k8s.io/v1beta1 false PriorityLevelConfigurationingressclasses networking.k8s.io/v1 false IngressClassingresses ing networking.k8s.io/v1 true Ingressnetworkpolicies netpol networking.k8s.io/v1 true NetworkPolicyruntimeclasses node.k8s.io/v1 false RuntimeClasspoddisruptionbudgets pdb policy/v1 true PodDisruptionBudgetpodsecuritypolicies psp policy/v1beta1 false PodSecurityPolicyclusterrolebindings rbac.authorization.k8s.io/v1 false ClusterRoleBindingclusterroles rbac.authorization.k8s.io/v1 false ClusterRolerolebindings rbac.authorization.k8s.io/v1 true RoleBindingroles rbac.authorization.k8s.io/v1 true Rolepriorityclasses pc scheduling.k8s.io/v1 false PriorityClasscsidrivers storage.k8s.io/v1 false CSIDrivercsinodes storage.k8s.io/v1 false CSINodecsistoragecapacities storage.k8s.io/v1beta1 true CSIStorageCapacitystorageclasses sc storage.k8s.io/v1 false StorageClassvolumeattachments storage.k8s.io/v1 false VolumeAttachment[root@cka-master ~]#
通过上表可以确定对应资源的apiVersion,例如pod需要使用的版本为v1
清单文件
通过命令kubectl explain po可以看到资源对象的字段定义
[root@cka-master ~]# kubectl explain poKIND: PodVERSION: v1DESCRIPTION:Pod is a collection of containers that can run on a host. This resource iscreated by clients and scheduled onto hosts.FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata <Object>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec <Object>Specification of the desired behavior of the pod. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-statusstatus <Object>Most recently observed status of the pod. This data may not be up to date.Populated by the system. Read-only. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status[root@cka-master ~]#
支持级联操作进行细化
[root@cka-master ~]# kubectl explain po.kindKIND: PodVERSION: v1FIELD: kind <string>DESCRIPTION:Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds[root@cka-master ~]#
结构查看
root@cka-master ~]# kubectl explain po.metadata --recursiveKIND: PodVERSION: v1RESOURCE: metadata <Object>DESCRIPTION:Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataObjectMeta is metadata that all persisted resources must have, whichincludes all objects users must create.FIELDS:annotations <map[string]string>clusterName <string>creationTimestamp <string>deletionGracePeriodSeconds <integer>deletionTimestamp <string>finalizers <[]string>generateName <string>generation <integer>labels <map[string]string>managedFields <[]Object>apiVersion <string>fieldsType <string>fieldsV1 <map[string]>manager <string>operation <string>subresource <string>time <string>name <string>namespace <string>ownerReferences <[]Object>apiVersion <string>blockOwnerDeletion <boolean>controller <boolean>kind <string>name <string>uid <string>resourceVersion <string>selfLink <string>uid <string>[root@cka-master ~]#
命令生成
使用—dry-run参数
[root@cka-master ~]# kubectl run myapp --image=nginx --dry-run -o yamlW0314 04:53:19.769459 32394 helpers.go:555] --dry-run is deprecated and can be replaced with --dry-run=client.apiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: myappname: myappspec:containers:- image: nginxname: myappresources: {}dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}
针对已经存在的资源对象,通过-o参数进行导出
[root@cka-master ~]# kubectl get pod myapp -o yamlapiVersion: v1kind: Podmetadata:annotations:cni.projectcalico.org/containerID: 873146677bbf4fa3e23c5fc8803acbe053144c0ab10465262d842df0d9950827cni.projectcalico.org/podIP: 10.244.115.65/32cni.projectcalico.org/podIPs: 10.244.115.65/32creationTimestamp: "2022-03-13T20:54:38Z"labels:run: myappname: myappnamespace: defaultresourceVersion: "28262"uid: 905504ca-d4ad-48b0-89b5-2dc1b873ae21spec:containers:- image: nginximagePullPolicy: Alwaysname: myappresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: kube-api-access-tcmzkreadOnly: truednsPolicy: ClusterFirstenableServiceLinks: truenodeName: cka-node1preemptionPolicy: PreemptLowerPrioritypriority: 0restartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}serviceAccount: defaultserviceAccountName: defaultterminationGracePeriodSeconds: 30tolerations:- effect: NoExecutekey: node.kubernetes.io/not-readyoperator: ExiststolerationSeconds: 300- effect: NoExecutekey: node.kubernetes.io/unreachableoperator: ExiststolerationSeconds: 300volumes:- name: kube-api-access-tcmzkprojected:defaultMode: 420sources:- serviceAccountToken:expirationSeconds: 3607path: token- configMap:items:- key: ca.crtpath: ca.crtname: kube-root-ca.crt- downwardAPI:items:- fieldRef:apiVersion: v1fieldPath: metadata.namespacepath: namespacestatus:conditions:- lastProbeTime: nulllastTransitionTime: "2022-03-13T20:54:26Z"status: "True"type: Initialized- lastProbeTime: nulllastTransitionTime: "2022-03-13T20:55:01Z"status: "True"type: Ready- lastProbeTime: nulllastTransitionTime: "2022-03-13T20:55:01Z"status: "True"type: ContainersReady- lastProbeTime: nulllastTransitionTime: "2022-03-13T20:54:38Z"status: "True"type: PodScheduledcontainerStatuses:- containerID: docker://f83bef188bee675668dd87ed0661512037fd8a623a11f2fdc33671c62823fc5eimage: nginx:latestimageID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31lastState: {}name: myappready: truerestartCount: 0started: truestate:running:startedAt: "2022-03-13T20:55:00Z"hostIP: 192.168.184.129phase: RunningpodIP: 10.244.115.65podIPs:- ip: 10.244.115.65qosClass: BestEffortstartTime: "2022-03-13T20:54:26Z"[root@cka-master ~]#
