Docker Kubernetes YAML文件常用指令
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