Kubernetes-yaml 文件及语法基础
- 需要提前创建好 yaml 文件,并创建好 pod 运行所需要的 namespace、yaml 文件等资源
创建业务 namespace yaml 文件
# pwd
/opt/k8s-data/yaml
# mkdir namespaces bokebi
# cd namespaces
# cat test.yaml
apiVersion: v1 #API版本
kind: Namespace #类型为namespac
metadata: #定义元数据
name: test #namespace名称
创建并验证 namespace
# kubectl apply -f test.yaml
namespace/test created
# kubectl get namespaces
NAME STATUS AGE
default Active 3d9h
kube-node-lease Active 3d9h
kube-public Active 3d9h
kube-system Active 3d9h
kubernetes-dashboard Active 3d1h
test Active 9s
yaml 与 json
json 格式
{ '人员名单':
{ '张三': { '年龄': 18, '职业': 'Linux运维工程师', '爱好': [ '看书', '学习', '加班' ] },
'李四': { '年龄': 20, '职业': 'Java开发工程师', '爱好': [ '开源技术', '微服务', '分布式存
储' ] } } }
#json 格式特点:
json 不能注释
json 可读性较差
json 语法很严格
比较适用于 API 返回值,也可用于配置文件
yaml 格式
人员名单:
张三:
年龄: 18
职业: Linux运维工程师
爱好:
- 看书
- 学习
- 加班
李四:
年龄: 20
职业: Java开发工程师 # 这是职业
爱好:
- 开源技术
- 微服务
- 分布式存储
大小写敏感
使用缩进表示层级关系
缩进时不允许使用 Tal 键,只允许使用空格
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
使用"#" 表示注释,从这个字符一直到行尾,都会被解析器忽略
比 json 更适用于配置文件
yaml 文件主要特性
- k8s 中的 yaml 文件及其他场景的 yaml 文件,大部分都是以下类型
上下级关系
列表
键值对(也称为 maps,即 key:value 格式的键值对数据)
Nginx 业务 yaml 文件详解
# pwd
/opt/k8s-data/yaml/bokebi
# mkdir nginx tomcat-app1 tomcat-app2
# cd nginx
# pwd
/opt/k8s-data/yaml/bokebi/nginx
# cat nginx.yaml
---
kind: Deployment #类型,是 deployment 控制器,kubectl explain Deployment
apiversion: extensions/vlbetal #API 版本,kubectl explain Deployment.apiversion
metadata: #pod 的元数据信息,kubectl explain Deployment.metadata
labels: #自定义 pod 的标签,kubectl explain Deployment.metadata.labels
app: bokebi-nginx-deployment-label #标签名称为 app 值为 bokebi-nginx-deployment-label,后面会用到此标签
name: bokebi-nginx-deployment #pod 的名称
namespace: bokebi #pod 的 namespace,默认是 default
spec: #定义 deployment 中容器的详细信息,kubectl explain Deployment.spec
replicas: 1 #创建出的 pod 的副本数,即多少个 pod,默认值为 1
selsctor: #定义标签选择器
matchLabels: #定义匹配的标签,必须要设置
app: #bokebi-nginx-selector #匹配的目标标签
template: #定义模板,必须定义,模板是起到描述要创建的 pod 的作用
metadata: #定义模板元数据
labels: #定义模板 label,kubectl explain Deployment.spec.template.metadata.labels
app: bokebi-nginx-selector # 定义标签,等于 Deployment.spec.seletor.matchLabels.app 的值
spec: #定义 pod 信息
containners #定义 pod 中容器列表,可以多个至少一个,pod 不能动态增减容器
- name: bokebi-nginx-container #容器名称
image: k8s.harbor.com/base-images/nginx:1.14.2 #镜像地址
#command: ["/apps/tomcat/bin/run_tomcat.sh"] #容器启动
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always #拉取镜像策略
port: #定义容器端口列表
- containerPort: 80 #定义一个端口
protocol: TCP #端口协议
name: http #端口名称
- containerPort: 443 #定义一个端口
protocol: TCP #端口协议
name: https #端口名称
env: #配置环境变量
- name: "password" #变量名称,必须要用引号引起来
value: "123456" #当前变量的值
- name: "age" #另一个变量名称
value: "18" #另一个变量的值
resources: #对资源的请求设置和限制设置
limits: #资源限制设置,上限
cpu: 2 #cpu 的限制,单位为 core 数,可以写为 2 或者 2000m 等 CPU 的压缩值,2000 毫核
memory: 2Gi #内存限制,单位可以为 Mib/Gib,将用于 docker run --memory 参数
requests: #资源请求的设置
cpu:1 #cpu 请求数,容器启动的初始可用数量,可以写 1 或者 1000m 等 CPU 的压缩值,1000 毫核
memory: 512Mi #内存请求大小,容器启动的初始可用数量,用于调度 pod 的时候使用
---
kind: Service #类型为 service
apiVersion: v1 #service API 版本,kubectl explain Service.apiVersion
metadata: #定义 service 元数据,kubectl explain Service.metadata
labels: #自定义标签,kubectl explain Service.metadata.labels
app: bokebi-nginx #定义 service 标签的内容
name: bokebi-nginx-spec #定义 service 的名称,此名称会被 DNS 解析
namespace: bokebi #该 service 隶属于的 namespaces 名称,即把 service 创建到哪个 namespace 里面
spec: #定义 service 的详细信息,kubectl explain Service.spec
type: NodePort #service 的类型,定义服务的访问方式,默认为 ClusterIP,kubectl explain Service.spec.type
ports: #定义访问端口,kubectl explain Service.spec.ports
- name: #定义一个端口名称
port: 80 #service 80 端口
protocol: TCP #协议类型
targetPort: 80 #目标 pod 的端口
nodePort: 30001 #node 节点暴漏的端口
- name: https #SSL 端口
port: 443 #service 443 端口
protocol: TCP #端口协议
targetPort: 443 #目标 pod 端口
modePort: 300043 #node 节点暴露的 SSL 端口
selector: #service 的标签选择器,定义要访问的目标 pod
app: bokebi-nginx #将流量路由到选择的 pod 上,需等于 kubectl explain Deployment.spec.selector.matchLabels