Kubernetes--YAML文件

it2024-01-23  60

文章目录

概述使用YAML文件创建资源对象k8s中的port

概述

k8s支持使用YAML和JSON格式的文件来创建资源对象,相比较而言:

●json格式的文件用于接口之间消息的传递,更适合二次开发

●yaml格式的文件只是一种简洁的非标记性语言,更适合运维

Kubernetes支持YAML和JSON格式创建资源对象

JSON格式用于接口之间消息的传递

YAML格式用于配置和管理

YAML是一种简洁的非标记性语言

语法格式: ●缩进标识层级关系

●不支持制表符缩进,使用空格缩进

● 通常开头缩进两个空格

● 字符后缩进一个空格,如冒号,逗号,短横杆等

● “—”表示YAML格式,一个文件的开始

● “#”表示注释

使用YAML文件创建资源对象

1、查看资源版本标签

[root@master ~]# kubectl api-versions admissionregistration.k8s.io/v1beta1 apiextensions.k8s.io/v1beta1 apiregistration.k8s.io/v1 apiregistration.k8s.io/v1beta1 apps/v1 apps/v1beta1 apps/v1beta2 authentication.k8s.io/v1 authentication.k8s.io/v1beta1 authorization.k8s.io/v1 authorization.k8s.io/v1beta1 autoscaling/v1 autoscaling/v2beta1 autoscaling/v2beta2 batch/v1 batch/v1beta1 certificates.k8s.io/v1beta1 coordination.k8s.io/v1beta1 events.k8s.io/v1beta1 extensions/v1beta1 kuboard.cn/v1 networking.k8s.io/v1 policy/v1beta1 rbac.authorization.k8s.io/v1 rbac.authorization.k8s.io/v1beta1 scheduling.k8s.io/v1beta1 storage.k8s.io/v1 storage.k8s.io/v1beta1 v1

2、创建目录,编辑测试文件

[root@master test]# vim nginx-deployment.yaml apiVersion: apps/v1 '指定api版本标签' kind: Deployment '定义资源的类型/角色,deployment为控制器' metadata: '定义资源的元数据' name: nginx-deployment '定义资源的名称,在同一个namespace中必须唯一' labels: '定义资源的标签' app: nginx spec: '定义容器模板' replicas: 3 '定义副本数量' selector: '选择器' matchLabels: '匹配标签' app: nginx '匹配模板名称' template: '模板' metadata: labels: app: nginx spec: containers: '定义容器信息' - name: nginx '-:表示参数,容器名,与标签名要相同' image: nginx:1.15.4 '容器使用的镜像以及版本' ports: - containerPort: 80 '定义容器的对外端口'

3、创建资源对象

[root@master test]# kubectl create -f nginx-deployment.yaml deployment.apps/nginx-deployment created [root@master test]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-d55b94fd-7rp7w 1/1 Running 0 55s nginx-deployment-d55b94fd-lzdc8 1/1 Running 0 55s nginx-deployment-d55b94fd-x2xjq 1/1 Running 0 55s

4、创建service服务对外提供访问并测试

[root@master test]# vim nginx-service.yaml apiVersion: v1 kind: Service metadata: name: nginx-service labels: app: nginx spec: type: NodePort ports: - port: 80 targetPort: 80 selector: app: nginx [root@master test]# kubectl create -f nginx-service.yaml service/nginx-service created [root@master test]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 13d nginx-service NodePort 10.0.0.206 <none> 80:32307/TCP 3m41s

查看生成yaml格式

[root@master test]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. apiVersion: apps/v1beta1 kind: Deployment metadata: creationTimestamp: null labels: run: nginx-deployment name: nginx-deployment spec: replicas: 3 selector: matchLabels: run: nginx-deployment strategy: {} template: metadata: creationTimestamp: null labels: run: nginx-deployment spec: containers: - image: nginx name: nginx-deployment ports: - containerPort: 80 resources: {} status: {}

查看生成json格式

[root@master test]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o json kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. { "kind": "Deployment", "apiVersion": "apps/v1beta1", "metadata": { "name": "nginx-deployment", "creationTimestamp": null, "labels": { "run": "nginx-deployment" } }, "spec": { "replicas": 3, "selector": { "matchLabels": { "run": "nginx-deployment" } }, "template": { "metadata": { "creationTimestamp": null, "labels": { "run": "nginx-deployment" } }, "spec": { "containers": [ { "name": "nginx-deployment", "image": "nginx", "ports": [ { "containerPort": 80 } ], "resources": {} } ] } }, "strategy": {} }, "status": {} }

将生成的yaml导入到文本中方便查看

[root@master test]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run -o yaml > my-deployment.yaml kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. [root@master test]# vim my-deployment.yaml apiVersion: apps/v1beta1 kind: Deployment metadata: creationTimestamp: null apiVersion: apps/v1beta1 kind: Deployment metadata: creationTimestamp: null labels: run: nginx-deployment name: nginx-deployment spec: replicas: 3 selector: matchLabels: run: nginx-deployment strategy: {} template: metadata: creationTimestamp: null labels: run: nginx-deployment spec: containers: - image: nginx name: nginx-deployment

保存到文件中

[root@master01 demo]# kubectl get deploy/nginx-deployment --export -o yaml> my--deploy.yaml

查看字段帮助信息

[root@localhost demo]# kubectl explain pods.spec.containers

k8s中的port

port port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service。

nodePort nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service。

targetPort targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。

containerPort containerPort是pod内部容器的端口,targetPort映射到containerPort。

最新回复(0)