apiVersion
解释: 以后所有可用的API版本罕用的apiVersion: v1: Kubernetes API的稳固版本,蕴含很多外围对象:pod、service等。apps/v1: 蕴含一些通用的应用层的api组合,如:Deployments, RollingUpdates, and ReplicaSets。batch/v1: 蕴含与批处理和相似作业的工作相干的对象,如:job、cronjob。autoscaling/v1: 容许依据不同的资源应用指标主动调整容器。networking.k8s.io/v1: 用于Ingress。rbac.authorization.k8s.io/v1:用于RBAC
$ kubectl api-versionsadmissionregistration.k8s.io/v1apiextensions.k8s.io/v1apiregistration.k8s.io/v1apps/v1authentication.k8s.io/v1authorization.k8s.io/v1autoscaling/v1autoscaling/v2autoscaling/v2beta1autoscaling/v2beta2batch/v1batch/v1beta1certificates.k8s.io/v1coordination.k8s.io/v1discovery.k8s.io/v1discovery.k8s.io/v1beta1events.k8s.io/v1events.k8s.io/v1beta1flowcontrol.apiserver.k8s.io/v1beta1flowcontrol.apiserver.k8s.io/v1beta2networking.k8s.io/v1node.k8s.io/v1node.k8s.io/v1beta1openebs.io/v1alpha1policy/v1policy/v1beta1rbac.authorization.k8s.io/v1scheduling.k8s.io/v1storage.k8s.io/v1storage.k8s.io/v1beta1v1
kind
解释: 资源对象的类型如 pod、deployment、statefulset、job、cronjob
metadata
罕用的配置项有 name,namespacename配置资源对象显示的名字namespace配置归属的命名空间
spec
解释: 资源对象的具体资源清单
// 以pod为例apiVersion: v1 #必选,版本号,例如v1kind: Pod #必选,Pod metadata: #必选,元数据 name: nginx #必选,Pod名称 labels: #自定义标签 app: nginx #自定义标签名字 spec: #必选,Pod中容器的具体定义 containers: #必选,Pod中容器列表,一个pod里会有多个容器 - name: nginx #必选,容器名称 image: nginx #必选,容器的镜像名称 imagePullPolicy: IfNotPresent # [Always | Never | IfNotPresent] #获取镜像的策略 Alawys示意下载镜像 IfnotPresent示意优先应用本地镜像,否则下载镜像,Nerver示意仅应用本地镜像 ports: #须要裸露的端口库号列表 - containerPort: 80 #容器须要监听的端口号 restartPolicy: Always # [Always | Never | OnFailure]#Pod的重启策略,Always示意一旦不论以何种形式终止运行,kubelet都将重启,OnFailure示意只有Pod以非0退出码退出才重启,Nerver示意不再重启该Pod
// 以Service为例apiVersion: v1kind: Servicemetadata: name: service-hello labels: name: service-hellospec: type: NodePort #这里代表是NodePort类型的,另外还有ingress,LoadBalancer ports: - port: 80 #这里的端口和clusterIP(kubectl describe service service-hello中的IP的port)对应,即在集群中所有机器上curl 10.98.166.242:80可拜访公布的应用服务。 targetPort: 8080 #端口肯定要和container裸露进去的端口对应,nodejs裸露进去的端口是8081,所以这里也应是8081 protocol: TCP # 反对TCP UDP SCTP nodePort: 31111 # 所有的节点都会凋谢此端口30000--32767,此端口供内部调用。 selector: run: hello #这里选择器肯定要抉择容器的标签,之前写name:kube-node是错的。
port
clusterIp:k8s集群外部拜访service的端口,即通过clusterIP: port能够拜访到某个servicenodePort:k8s集群内部拜访service的端口,通过nodeIP: nodePort能够从内部拜访到某个service。targetPort:pod的端口,从port和nodePort来的流量通过kube-proxy流入到后端pod的targetPort上,最初进入容器。containerPort:containerPort是pod外部容器的端口,targetPort映射到containerPort