Kubernetes支持YAML和JSON格式创建资源对象
1,JSON格式用于接口之间消息的传递
2,YAML格式用于配置和管理
3,YAML是一种简洁的非标记性语言
语法格式:
缩进标识层级关系
不支持制表符(tab)缩进,使用空格缩进
通常开头缩进两个空格
字符后缩进一个空格,如冒号,逗号等
“—”表示YAML格式,一个文件的开始
“#”表示注释
`查看应用名称`
[root@master1 ~]# 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
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
[root@master1 ~]# mkdir demo
[root@master1 ~]# cd demo/
[root@master1 demo]# vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
#插入内容后按Esc退出插入模式,输入:wq保存退出
[root@master1 demo]# kubectl create -f my-nginx.yaml
deployment.apps/my-nginx created
[root@master1 demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
`my-nginx-d55b94fd-kc2gl 1/1 Running 0 47s`
`my-nginx-d55b94fd-tkr42 1/1 Running 0 47s`
nginx-6c94d899fd-8pf48 1/1 Running 0 23h
nginx-deployment-5477945587-f5dsm 1/1 Running 0 22h
nginx-deployment-5477945587-hmgd2 1/1 Running 0 22h
nginx-deployment-5477945587-pl2hn 1/1 Running 0 22h
[root@master1 demo]# vim my-nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx-service
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx
#插入内容后按Esc退出插入模式,输入:wq保存退出
[root@master1 demo]# kubectl create -f my-nginx-service.yaml
service/my-nginx-service created
`查看服务`
[root@master1 demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 8d
`my-nginx-service NodePort 10.0.0.210 <none> 80:40377/TCP 20s`
nginx-service NodePort 10.0.0.242 <none> 80:40422/TCP 33h
1.自动测试命令的正确性,并不执行创建:
[root@master1 demo]# kubectl run my-nginx --image=nginx --port=80 --replicas=2 --dry-run
kubectl run --generator