YAML文件常用指令
配置文件說明:
- 定義配置時,指定最新穩定版API(當前為v1)。
- 配置文件應該存儲在集群之外的版本控制倉庫中。如果需要,可以快速回滾配置、重新創建和恢復。
- 應該使用YAML格式編寫配置文件,而不是JSON。盡管這些格式都可以使用,但YAML對用戶更加友好。
- 可以將相關對象組合成單個文件,通常會更容易管理。
- 不要沒必要的指定默認值,簡單和最小配置減少錯誤。
- 在注釋中說明一個對象描述更好維護。
- YAML是一種標記語言很直觀的數據序列化格式,可讀性高。類似於XML數據描述語言,語法比XML簡單的很多。
- YAML數據結構通過縮進來表示,連續的項目通過減號來表示,鍵值對用冒號分隔,數組用中括號括起來,hash用花括號括起來。

YAML文件格式注意事項:
- 1. 不支持制表符tab鍵縮進,需要使用空格縮進
- 2. 通常開頭縮進2個空格
- 3. 字符后縮進1個空格,
- 4. “---” 表示YAML格式,一個文件的開始
- 5. “#”注釋
- # 指定api版本
- apiVersion:
- # 指定需要創建的資源對象
- kind:
- # 源數據、可以寫name,命名空間,對象標簽
- metadata:
- # 指定對象名稱
- name:
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
- # 描述資源相關信息
- spec:
- # 指定pod 副本數,默認1
- replicas:
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3
- # 資源標簽選擇器
- selector:
- # 描述資源具體信息
- template:
- # 匹配標簽字段
- matchLabels:
- # 指定pod標簽value:key
- labels:
kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx
- # 指定容器信息
- containers:
- # 指定容器名稱
- - name:
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.9 ports: - containerPort: 80
- # 指定鏡像名稱
- image:
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.9 ports: - containerPort: 80
- # 暴露容器端口
- ports:
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.9 ports: - containerPort: 80
- # 指定暴露容器端口
- - containerPort:
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.9 ports: - containerPort: 80
- # 添加環境變量
- env:
apiVersion: v1 kind: Pod metadata: name: pod-test labels: os: centos spec: containers: - name: hello image: centos:6 env: # 變量key - name: Test # 變量value value: "123456"
- # 啟動容器后執行命令
- command:
apiVersion: v1 kind: Pod metadata: name: pod-test labels: os: centos spec: containers: - name: hello image: centos:6 command: ["bash","-c","while true;do date;sleep 1;done"]
- # 重啟策略 可添加(Always,OnFailure,Never)
- restartPolicy:
apiVersion: v1 kind: Pod metadata: name: pod-test labels: os: centos spec: containers: - name: hello image: centos:6 restartPolicy: OnFailure
- # 健康檢查模式(httpGet、exec、tcpSocket)
- livenessProbe:
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 ports: - containerPort: 80 livenessProbe: # 健康檢查模式 httpGet: # 指定檢查目錄 path: /index.html # 訪問端口 port: 80
- # 容器內管理volume數據卷
- volumeMounts:
apiVersion: v1 kind: Pod metadata: name: pod-test labels: test: centos spec: containers: - name: hello-read image: centos:6 # 容器內管理數據卷 volumeMounts: # 數據卷名稱 - name: data # 容器數據卷路徑 mountPath: /data # 數據卷 volumes: # 數據卷名稱 - name: data # 數據宿主機卷路徑 hostPath: # 指定宿主機數據卷路徑 path: /data
- # 宿主級管理volume數據卷管理
- volumes:
apiVersion: v1 kind: Pod metadata: name: pod-test labels: test: centos spec: containers: - name: hello-read image: centos:6 # 容器內管理數據卷 volumeMounts: # 數據卷名稱 - name: data # 容器數據卷路徑 mountPath: /data # 數據卷 volumes: # 數據卷名稱 - name: data # 數據宿主機卷路徑 hostPath: # 指定宿主機數據卷路徑 path: /data
- # hsotip監聽IP,可通過哪些宿主級ip訪問
- hostIP:
apiVersion: v1 kind: Pod metadata: name: nginx-pod2 labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 # hostport管理 ports: # 指定http - name: http # 指定端口 containerPort: 80 # hsotip監聽IP,可通過哪些宿主級ip訪問 hostIP: 0.0.0.0 # 宿主級暴露端口,它會映射到containerport的容器端口 hostPort: 89 # 指定協議類型 protocol: TCP
- # 宿主級暴露端口,它會映射到containerport的容器端口
- hostPort:
apiVersion: v1 kind: Pod metadata: name: nginx-pod2 labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 # hostport管理 ports: # 指定http - name: http # 指定端口 containerPort: 80 # hsotip監聽IP,可通過哪些宿主級ip訪問 hostIP: 0.0.0.0 # 宿主級暴露端口,它會映射到containerport的容器端口 hostPort: 89 # 指定協議類型 protocol: TCP
- # 暴露端口的協議類型
- protocol:
apiVersion: v1 kind: Pod metadata: name: nginx-pod2 labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 # hostport管理 ports: # 指定http - name: http # 指定端口 containerPort: 80 # hsotip監聽IP,可通過哪些宿主級ip訪問 hostIP: 0.0.0.0 # 宿主級暴露端口,它會映射到containerport的容器端口 hostPort: 89 # 指定協議類型 protocol: TCP
- # 固定IP地址
- clusterIP:
apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - name: http protocol: TCP port: 888 targetPort: 80 clusterIP: "10.10.10.11"
- # 服務類型
- type:
apiVersion: v1 kind: Service metadata: name: nginx-service2 labels: app: nginx spec: selector: app: nginx ports: - name: http port: 8080 targetPort: 80 # 服務類型 type: NodePort
- # node節點創建socker的暴露端口,默認30000~32767
- nodePort:
apiVersion: v1 kind: Service metadata: name: nginx-service2 labels: app: nginx spec: selector: app: nginx ports: - name: http port: 8080 targetPort: 80 # 服務類型 type: NodePort
