kubernetes之kubectl与YAML详解1( 七 )

查看所有的接口的版本apps/v1  v1是稳定版   v1beta1,2是测试版。这是http资源 。我们生产一般不用测试版本api接口
[root@mcwk8s04 ~]# kubectl api-versionsadmissionregistration.k8s.io/v1beta1apiextensions.k8s.io/v1beta1apiregistration.k8s.io/v1apiregistration.k8s.io/v1beta1apps/v1apps/v1beta1apps/v1beta2authentication.k8s.io/v1authentication.k8s.io/v1beta1authorization.k8s.io/v1authorization.k8s.io/v1beta1autoscaling/v1autoscaling/v2beta1autoscaling/v2beta2batch/v1batch/v1beta1certificates.k8s.io/v1beta1coordination.k8s.io/v1coordination.k8s.io/v1beta1events.k8s.io/v1beta1extensions/v1beta1networking.k8s.io/v1networking.k8s.io/v1beta1node.k8s.io/v1beta1policy/v1beta1rbac.authorization.k8s.io/v1rbac.authorization.k8s.io/v1beta1scheduling.k8s.io/v1scheduling.k8s.io/v1beta1storage.k8s.io/v1storage.k8s.io/v1beta1v1[root@mcwk8s04 ~]# 通过yaml配置文件部署服务我们在官网里一层层找,下面是各种资源模板
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

kubernetes之kubectl与YAML详解1

文章插图
这篇文档的这个部位,就是一个yaml的案例
kubernetes之kubectl与YAML详解1

文章插图
将官网的复制过来,然后执行部署
[root@mcwk8s03 ~]# mkdir demo[root@mcwk8s03 ~]# cd demo/[root@mcwk8s03 demo]# vim mcw-nginx-deployment.yaml[root@mcwk8s03 demo]# cat mcw-nginx-deployment.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: nginx-deploymentlabels:app: nginxspec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.14.2ports:- containerPort: 80[root@mcwk8s03 demo]#[root@mcwk8s03 demo]# kubectl create -f mcw-nginx-deployment.yamldeployment.apps/nginx-deployment created[root@mcwk8s03 demo]# kubectl get podNAMEREADYSTATUSRESTARTSAGEnginx-deployment-7fd6966748-mxg8p1/1Running05snginx-deployment-7fd6966748-pgfgq0/1ContainerCreating05snginx-deployment-7fd6966748-wsfj41/1Running05s[root@mcwk8s03 demo]#[root@mcwk8s03 demo]# 官网里面看其它资源的案例
kubernetes之kubectl与YAML详解1

文章插图
部署service
[root@mcwk8s03 demo]#[root@mcwk8s03 demo]# vim mcw-nginx-service.yaml[root@mcwk8s03 demo]# cat mcw-nginx-service.yamlapiVersion: v1kind: Servicemetadata:name: nginx-servicelabels:app: nginxspec:type: NodePortports:- port: 80targetPort: 80selector:app: nginx[root@mcwk8s03 demo]# kubectl create -f mcw-nginx-service.yamlservice/nginx-service created[root@mcwk8s03 demo]# kubectl get svcNAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGEkubernetesClusterIP10.2.0.1<none>443/TCP5d22hnginx-serviceNodePort10.2.0.247<none>80:33851/TCP4s[root@mcwk8s03 demo]# 访问上面刚刚部署的服务,版本也是对上的
kubernetes之kubectl与YAML详解1

文章插图
YAML配置文件管理资源 实用技巧? 用run命令生成kubectl run --image=nginx my-deploy -o yaml --dry-run > my-deploy.yaml? 用get命令导出kubectl get my-deploy/nginx -o=yaml --export > my-deploy.yaml? Pod容器的字段拼写忘记了kubectl explain pods.spec.containers用run命令生成   --dry-run生成配置文件[root@mcwk8s03 demo]# kubectl run mcw-nginx-deployment --image=nginx --port=80 --replicas=3 --dry-run#执行部署的命令后面接--dry-run,不会进行部署,可以检查是否有语法错误kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.deployment.apps/mcw-nginx-deployment created (dry run)[root@mcwk8s03 demo]# kubectl get podNAMEREADYSTATUSRESTARTSAGEnginx-deployment-7fd6966748-mxg8p1/1Running036mnginx-deployment-7fd6966748-pgfgq1/1Running036mnginx-deployment-7fd6966748-wsfj41/1Running036m[root@mcwk8s03 demo]# kubectl get deployNAMEREADYUP-TO-DATEAVAILABLEAGEnginx-deployment3/33337m[root@mcwk8s03 demo]#

经验总结扩展阅读