网站建设维护招聘,中山专业做网站,长沙房地产网站设计,淘宝网页版手机版一、yaml文件简介 Kubernetes只支持YAML和JSON格式创建资源对象#xff0c;JSON格式用于接口之间消息的传递#xff0c;适用于开发#xff1b;YAML格式用于配置和管理#xff0c;适用于云平台管理#xff0c;YAML是一种简洁的非标记性语言。 1#xff09;yaml的语法规则JSON格式用于接口之间消息的传递适用于开发YAML格式用于配置和管理适用于云平台管理YAML是一种简洁的非标记性语言。 1yaml的语法规则 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tal键只允许使用空格 缩进的空格数目不重要只要相同层级的元素左侧对齐即可 ”#” 表示注释从这个字符一直到行尾都会被解析器忽略 注- - - 为可选的分隔符 当需要在一个文件中定义多个结构的时候需要使用 2在Kubernetes中只需要知道两种结构类型即可 Lists Maps 2.1YAML Maps Map顾名思义指的是字典即一个Key:Value 的键值对信息。例如
---
apiVersion: v1
kind: Pod上述内容表示有两个键apiVersion和kind分别对应的值为v1和Pod。 Maps的value既能够对应字符串也能够对应一个Maps。例如
---
apiVersion: v1
kind: Pod
metadata:name: kube100-sitelabels:app: web注上述的YAML文件中metadata这个KEY对应的值为一个Maps而嵌套的labels这个KEY的值又是一个Map。实际使用中可视情况进行多层嵌套。 2.2YAML Lists Lists的子项也可以是MapsMaps的子项也可以是List例如
---
apiVersion: v1
kind: Pod
metadata:name: nginxlabels:app: web
spec:containers:- name: front-endimage: nginxports:- containerPort: 80- name: flaskapp-demoimage: jcdemo/flaskappports: 8080二、yaml常见语法 1apiVersion 查看当前所有可用的API版本
[rootmaster1 ~]# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2
batch/v1
certificates.k8s.io/v1
coordination.k8s.io/v1
crd.projectcalico.org/v1
discovery.k8s.io/v1
events.k8s.io/v1
flowcontrol.apiserver.k8s.io/v1beta2
flowcontrol.apiserver.k8s.io/v1beta3
networking.k8s.io/v1
node.k8s.io/v1
policy/v1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
v1常用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。
2kind kind指定这个资源对象的类型如 pod、deployment、statefulset、job、cronjob
3metadata metadata常用的配置项有 name,namespace,即配置其显示的名字与归属的命名空间。
4spec 一个嵌套字典与列表的配置项也是主要的配置项支持的子项非常多根据资源对象的不同子项会有不同的配置。 如一个pod的spec配置
apiVersion: v1 #必选版本号例如v1
kind: 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 的 spec 的配置
apiVersion: v1
kind: Service
metadata:name: service-hellolabels:name: service-hello
spec:type: NodePort #这里代表是NodePort类型的,另外还有ingress,LoadBalancerports:- port: 80 #这里的端口和clusterIP(kubectl describe service service-hello中的IP的port)对应即在集群中所有机器上curl 10.98.166.242:80可访问发布的应用服务。targetPort: 8080 #端口一定要和container暴露出来的端口对应nodejs暴露出来的端口是8081所以这里也应是8081protocol: TCPnodePort: 31111 # 所有的节点都会开放此端口30000--32767此端口供外部调用。selector:run: nginx #这里选择器一定要选择容器的标签三、port详解 portport是k8s集群内部访问service的端口即通过clusterIP: port可以访问到某个service nodePortnodePort是外部访问k8s集群中service的端口通过nodeIP: nodePort可以从外部访问到某个service。 targetPorttargetPort是pod的端口从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上最后进入容器。 containerPortcontainerPort是pod内部容器的端口targetPort映射到containerPort。
四、yaml简单示例 1deployment
apiVersion: apps/v1 # 1.9.0 之前的版本使用 apps/v1beta2可通过命令 kubectl api-versions 查看
kind: Deployment #指定创建资源的角色/类型
metadata: #资源的元数据/属性name: nginx-deployment #资源的名字在同一个namespace中必须唯一
spec:replicas: 2 #副本数量2selector: #定义标签选择器matchLabels:app: web-servertemplate: #这里Pod的定义metadata:labels: #Pod的labelapp: web-serverspec: # 指定该资源的内容 containers: - name: nginx #容器的名字 image: nginx:1.12.1 #容器的镜像地址 ports: - containerPort: 80 #容器对外的端口2pod
apiVersion: v1
kind: Pod
metadata: name: pod-redislabels:name: redis
spec: containers:- name: pod-redisimage: docker.io/redis ports:- containerPort: 80 #容器对外的端口3service
apiVersion: v1
kind: Service # 指明资源类型是 service
metadata: name: httpd-svc # service 的名字是 httpd-svclabels: name: httpd-svc
spec: ports: # 将 service 8080 端口映射到 pod 的 80 端口使用 TCP 协议- port: 8080targetPort: 80 protocol: TCP selector: run: httpd # 指明哪些 label 的 pod 作为 service 的后端4vs
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:annotations:devops-gitops-host1: minioname: minionamespace: paas-middleware
spec:gateways:- miniohosts:- minio.paas..cmit.cloudhttp:- match:- uri:prefix: /route:- destination:host: minio-headlessport:number: 13005ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-nginxnamespace: default
spec:ingressClassName: nginx rules:- host: xx.xxx.com http:paths: - backend: service: name: xxxxxx prot: number: 8080 path: / pathTpye: Prefix 五、Label与Selector 1Label Label是Kubernetes系列中另外一个核心概念。是一组绑定到K8s资源对象上的key/value对。同一个对象的labels属性的key必须唯一。label可以附加到各种资源对象上如Node,Pod,Service,RC等。
通过给指定的资源对象捆绑一个或多个不用的label来实现多维度的资源分组管理功能以便于灵活方便地进行资源分配调度配置部署等管理工作。 示例如下 版本标签“release” : “stable” , “release” : “canary”… 环境标签“environment” : “dev” , “environment” : “production” 架构标签“tier” : “frontend” , “tier” : “backend” , “tier” : “middleware” 分区标签“partition” : “customerA” , “partition” : “customerB”… 质量管控标签“track” : “daily” , “track” : “weekly”
2Selector Label selector是Kubernetes核心的分组机制通过label selector客户端/用户能够识别一组有共同特征或属性的资源对象。符合这个标签的 Pod 会作为这个 Service 的 backend。
apiVersion: v1
kind: Service
metadata:name: hellolabels:app: hello
spec:ports:- port: 80targetPort: 80selector:app: hello六、kubectl create和 kubectl apply二者区别 kubectl create kubectl create命令可创建新资源。 因此如果再次运行该命令则会抛出错误因为资源名称在名称空间中应该是唯一的。
kubectl apply kubectl apply命令将配置应用于资源。 如果资源不在那里那么它将被创建。 kubectl apply命令可以多次运行配置若没有改变。 所以pod没有改变。