Docker Kubernetes YAML文件經常使用指令

YAML文件經常使用指令html

配置文件說明:node

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

 

YAML文件格式注意事項:nginx

  • 1. 不支持製表符tab鍵縮進,須要使用空格縮進
  • 2. 一般開頭縮進2個空格
  • 3. 字符後縮進1個空格,
  • 4. 「---」 表示YAML格式,一個文件的開始
  • 5. 「#」註釋

  • # 指定api版本
  • apiVersion: 

  • # 指定須要建立的資源對象
  • kind: 

  • # 源數據、能夠寫name,命名空間,對象標籤
  • metadata:

  • # 指定對象名稱
  • name:
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
View Code

  • # 描述資源相關信息
  • spec:

  • # 指定pod 副本數,默認1
  • replicas:
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
View Code

  • # 資源標籤選擇器
  • 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
View Code

  • # 指定容器信息
  • 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
View Code

  • # 指定鏡像名稱
  • 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
View Code

  • # 暴露容器端口
  • 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
View Code

  • # 指定暴露容器端口
  • - 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
View Code

  • # 添加環境變量
  • 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"
View Code

  • # 啓動容器後執行命令
  • 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"]
View Code

  • 重啓策略 可添加(Always,OnFailure,Never
  • restartPolicy:
apiVersion: v1
kind: Pod
metadata:
  name: pod-test
  labels:
    os: centos
spec:
  containers:
  - name: hello
    image: centos:6
  restartPolicy: OnFailure
View Code

  • # 健康檢查模式(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
View Code

  • # 容器內管理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
View Code

  • # 宿主級管理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
View Code

  • 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
View Code

  • # 宿主級暴露端口,它會映射到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
View Code

  • # 暴露端口的協議類型
  • 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
View Code

  • # 固定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"
View Code

  • # 服務類型
  • 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
View Code

  • # 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
View Code

 

相關文章
相關標籤/搜索