K8S—YAML文件編寫
一.YAML的介紹
1.Kubernetes支持YAML和JSON格式創建資源對象
2.JSON格式用於接口之間消息的傳遞
3.YAML格式用於配置和管理
4.YAML是一種簡潔的非標記性語言
5.語法格式:
縮進標識層級關係
不支持製表符縮進,使用空格縮進
通常開頭縮進兩個空格
字符後縮進一個空格,如冒號,逗號等
“—”表示YAML格式,一個文件的開始
“#”表示註釋
二.YAML文件詳解
1.查看api版本
[root@localhost k8s]# 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
2.自行編寫nginx的yaml文件(手動)
[root@localhost ~]# mkdir demo
[root@localhost ~]# cd demo/
[root@localhost demo]# vim nginx-deployment.yaml
apiVersion: apps/v1 //api版本
kind: Deployment //指定類型,deployment表示控制器
metadata: //定義資源屬性
name: nginx-deployment //資源名稱
labels: //資源標籤
app: nginx
spec: //資源內容
replicas: 3 //副本
selector: //選擇器
matchLabels: //匹配標籤
app: nginx //匹配模板名稱
template: //從這開始往下,是pod的資源管理
metadata:
labels:
app: nginx
spec: //定義容器模板
containers: //信息
- name: nginx //容器名
image: nginx:1.15.4 //容器使用鏡像和版本
ports:
- containerPort: 80 //對外端口
3.使用yaml文件創建資源,並查看
[root@localhost demo]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
[root@localhost demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-ksqr7 1/1 Running 0 40m
nginx-7697996758-s52gb 1/1 Running 0 40m
nginx-7697996758-v9qzw 1/1 Running 0 40m
nginx-deployment-d55b94fd-99qxf 1/1 Running 0 58s
nginx-deployment-d55b94fd-bz76x 1/1 Running 0 58s
nginx-deployment-d55b94fd-tk27h 1/1 Running 0 58s
[root@localhost demo]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 43m
nginx-deployment 3 3 3 3 4m12s
//下三個是剛創建的
4.創建service的yaml文件(手動)
[root@localhost demo]# vim nginx-service.yaml
apiVersion: v1 //版本
kind: Service //類型
metadata: //元數據
name: nginx-service //service名稱
labels: //標籤屬性
app: nginx
spec: //詳情
type: NodePort //service的類型默認爲ClusterIP
ports: //端口
- port: 80 //內部端口
targetPort: 80 //需轉發的端口
selector: //選擇器
app: nginx //pod資源名稱
5.使用yaml文件創建service
[root@localhost demo]# kubectl create -f nginx-service.yaml
service/nginx-service created
[root@localhost demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 4d22h
nginx-service NodePort 10.0.0.83 <none> 80:47336/TCP 30s
//訪問查看
6.自動測試命令的正確性,並不執行創建(自動)
[root@localhost demo]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 --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-deployment created (dry run) //此時,此命令可被執行
7.查看yaml文件格式
[root@localhost demo]# 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: {}
8.查看生成json格式
[root@localhost demo]# 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": {}
}
9.把yaml文件格式導出,進行修改即可
[root@localhost demo]# 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@localhost demo]# ls
my-deployment.yaml nginx-deployment.yaml nginx-service.yaml
[root@localhost demo]# vim my-deployment.yaml
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: {}
10.將現有的資源生成yaml,並導出
//將現有的資源生成模板導出
[root@localhost demo]# kubectl get deploy/nginx
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 68m
[root@localhost demo]# kubectl get deploy/nginx --export -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: null
generation: 1
labels:
run: nginx
name: nginx
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 2
selector:
matchLabels:
run: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: nginx
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status: {}
//保存到文件中
[root@localhost demo]# kubectl get deploy/nginx --export -o yaml > my-deploy.yaml
[root@localhost demo]# ls
my-deployment.yaml my-deploy.yaml nginx-deployment.yaml nginx-service.yaml
//查看字段幫助信息
[root@localhost demo]# kubectl explain pods.spec.containers