概述
k8s支持使用YAML和JSON格式的文件来创建资源对象,相比较而言:
- json格式的文件用于接口之间消息的传递,更适合二次开发
- yaml格式的文件只是一种简洁的非标记性语言,更适合运维
YAML的文件格式和注意事项
- 不支持制表符tab键缩进,需要使用空格缩进,使用缩进表示层级关系
- 通常开头缩进2个空格,缩进的空格数不重要,只要相同层级的元素左对齐即可
- 字符后缩进一个空格,如冒号、逗号、横杆
- 用#号注释
- 如果包含特殊字符用单引号引起来
- 布尔值必须用引号括起来
- —表示yaml文件格式的分割
来吧!展示!!
查看 资源版本标签
[root@localhost ~]# 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@localhost bate1]# vim http-zero.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: http-deployment
labels:
app: http
spec:
replicas: 3
selector:
matchLabels:
app: http
template:
metadata:
labels:
app: http
spec:
containers:
- name: http
image: httpd
ports:
- containerPort: 80
创建资源
[root@localhost bate1]# kubectl create -f http-zero.yaml
deployment.apps/http-deployment created
……等一会
[root@localhost bate1]# kubectl get pods
NAME READY STATUS RESTARTS AGE
http-deployment-766c5bfc5c-9x4c4 1/1 Running 0 3m6s
http-deployment-766c5bfc5c-ctmnk 1/1 Running 0 3m6s
http-deployment-766c5bfc5c-dkjgk 1/1 Running 0 3m6s
创建service服务提供访问
[root@localhost bate1]# vim http-service.yaml
apiVersion: v1
kind: Service
metadata:
name: http-service
labels:
app: http
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: http
[root@localhost bate1]# kubectl create -f http-service.yaml
service/http-service created
[root@localhost bate1]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
http-service NodePort 10.0.0.138 <none> 80:42018/TCP 17s
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
自动生成yaml或者json文件
测试创建资源对象的命令正确性,并不真正执行创建
[root@localhost bate1]# kubectl run nginx-beta1 --image=nginx --port=80 --replicas=2 --dry-run
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-beta1 created (dry run)
自动生成,但不保存
[root@localhost bate1]# kubectl run nginx-beta1 --image=nginx --port=80 --replicas=2 --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-beta1
name: nginx-beta1
spec:
replicas: 2
selector:
matchLabels:
run: nginx-beta1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: nginx-beta1
spec:
containers:
- image: nginx
name: nginx-beta1
ports:
- containerPort: 80
resources: {}
status: {}
[root@localhost bate1]# kubectl run nginx-beta1 --image=nginx --port=80 --replicas=2 --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-beta1",
"creationTimestamp": null,
"labels": {
"run": "nginx-beta1"
}
},
"spec": {
"replicas": 2,
"selector": {
"matchLabels": {
"run": "nginx-beta1"
}
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"run": "nginx-beta1"
}
},
"spec": {
"containers": [
{
"name": "nginx-beta1",
"image": "nginx",
"ports": [
{
"containerPort": 80
}
],
"resources": {}
}
]
}
},
"strategy": {}
},
"status": {}
}
自动生成并保存
[root@localhost bate1]# kubectl run nginx-beta1 --image=nginx --port=80 --replicas=2 --dry-run -o yaml > nginx-beta1.yaml
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
[root@localhost bate1]# ls
http-service.yaml http-zero.yaml nginx-beta1.yaml
[root@localhost bate1]# cat nginx-beta1.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: nginx-beta1
name: nginx-beta1
spec:
replicas: 2
selector:
matchLabels:
run: nginx-beta1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: nginx-beta1
spec:
containers:
- image: nginx
name: nginx-beta1
ports:
- containerPort: 80
resources: {}
status: {}
将现有的资源生成模板并导出
[root@localhost bate1]# kubectl get pods
NAME READY STATUS RESTARTS AGE
http-deployment-766c5bfc5c-9x4c4 1/1 Running 0 16m
http-deployment-766c5bfc5c-ctmnk 1/1 Running 0 16m
http-deployment-766c5bfc5c-dkjgk 1/1 Running 0 16m
[root@localhost bate1]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
http-deployment 3 3 3 3 18m
[root@localhost bate1]# kubectl get deployment/http-deployment --export -o yaml > http01.yaml
[root@localhost bate1]# ls
http01.yaml http-service.yaml http-zero.yaml nginx-beta1.yaml
[root@localhost bate1]# cat http01.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: null
generation: 1
labels:
app: http
name: http-deployment
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/http-deployment
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 2
selector:
matchLabels:
app: http
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: http
spec:
containers:
- image: httpd
imagePullPolicy: Always
name: http
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: {}