yaml 文件详解及示例 Kubernetes 的几个重要概念:
资源对象: kubernetes基于声明式API,和资源对象进行交互。
yaml文件:为了方便后期管理,通过使用yaml文件通过API管理资源对象。
yaml必需字段:
apiVersion - 创建该对象所使用的 Kubernetes API 的版本
kind - 想要创建的对象的类型
metadata - 定义识别对象唯一性的数据,包括一个 name 名称 、可选的 namespace
spec:定义资源对象的详细规范信息(统一的label标签、容器名称、镜像、端口映射等)
status(Pod创建完成后k8s自动生成status状态)
Pod:
pod是k8s中的最小单元。
一个pod中可以运行一个容器,也可 以运行多个容器。
运行多个容器的话,这些容器是一起 被调度的。
Pod的生命周期是短暂的,不会自愈, 是用完就销毁的实体。
一般我们是通过Controller来创建和管理pod的
yaml 文件详解 yaml 文件必需字段
每个API对象都有3大类属性: 元数据metadata、规范spec和状态status
spec和status的区别:
基于yaml 文件,在k8s 中实现pod的创建与删除等功能
yaml 与json
{ '人员名单': { '张三': { '年龄': 18 , '职业': 'Linux运维工程师', '爱好': [ '看书', '学习', '加班' ] } , '李四': { '年龄': 20 , '职业': 'Java开发工程师', '爱好': [ '开源技术', '微服务', '分布式存 储' ] } } }
json特点:
json 不能注释
json 可读性较差
json 语法很严格
比较适用于API 返回值,也可用于配置文件
yaml格式
人员名单: 张三: 年龄: 18 职业: Linux运维工程师 爱好: - 看书 - 学习 - 加班 李四: 年龄: 20 职业: Java开发工程师 - 开源技术 - 微服务 - 分布式存储
大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tal键,只允许使用空格 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 使用”#” 表示注释,从这个字符一直到行尾,都会被解析器忽略 比json更适用于配置文件
k8s中的yaml文件以及其他场景的yaml文件,大部分都是以下类型:
apiVersion: v1 kind: Pod metadata: name: string namespace: string labels: - name: string annotations: - name: string spec: containers: - name: string image: string imagePullPolicy: [Always | Never | IfNotPresent ] command: [string ] args: [string ] workingDir: string volumeMounts: - name: string mountPath: string readOnly: boolean ports: - name: string containerPort: int hostPort: int protocol: string env: - name: string value: string resources: limits: cpu: string memory: string requests: cpu: string memory: string livenessProbe: exec: command: [string ] httpGet: path: string port: number host: string scheme: string HttpHeaders: - name: string value: string tcpSocket: port: number initialDelaySeconds: 0 timeoutSeconds: 0 periodSeconds: 0 successThreshold: 0 failureThreshold: 0 securityContext: privileged:false restartPolicy: [Always | Never | OnFailure ] nodeSelector: obeject imagePullSecrets: - name: string hostNetwork:false volumes: - name: string emptyDir: {} hostPath: string path: string secret: scretname: string items: - key: string path: string configMap: name: string items: - key: string path: string
例子:
apiVersion: v1 kind: Pod metadata: name: test-pod labels: k8s-app: apache version: v1 kubernetes.io/cluster-service: "true" annotations: - name: String spec: restartPolicy: Always nodeSelector: zone: node1 containers: - name: test-pod image: 10.192 .21 .18 :5000/test/chat:latest imagePullPolicy: Never command: ['sh' ] args: ["$(str)" ] env: - name: str value: "/etc/run.sh" resources: requests: cpu: 0.1 memory: 32Mi limits: cpu: 0.5 memory: 1000Mi ports: - containerPort: 80 name: httpd protocol: TCP livenessProbe: httpGet: path: / port: 80 scheme: HTTP initialDelaySeconds: 180 timeoutSeconds: 5 periodSeconds: 15 lifecycle: postStart: exec: command: - 'sh' - 'yum upgrade -y' preStop:#容器关闭之前运行的任务 exec: command: ['service httpd stop' ] volumeMounts: - name: volume mountPath: /data readOnly: True volumes: - name: volume hostPath: path: /opt
实践nginx+tomcat
使用yaml创建nginx pod
Nginx.yaml:
kind: Deployment apiVersion: extensions/v1beta1 metadata: labels: app: linux66-nginx-deployment-label name: linux66-nginx-deployment namespace: linux66 spec: replicas: 1 selector: matchLabels: app: linux36-nginx-selector template: metadata: labels: app: linux36-nginx-selector spec: containers:#定义pod中容器列表,可以多个至少一个,pod不能动态增减容器 - name: linux36-nginx-container image: harbor.magedu.net/linux36/nginx-web1:v1 imagePullPolicy: Always ports: - containerPort: 80 protocol: TCP name: http - containerPort: 443 protocol: TCP name: https env: - name: "password" value: "123456" - name: "age" value: "18" resources: limits: cpu: 500m memory: 2Gi requests: cpu: 200m memory: 512Mi --- kind: Service apiVersion: v1 metadata: labels: app: linux66-nginx name: linux66-nginx-spec namespace: linux66 spec: type: NodePort ports: - name: http port: 80 protocol: TCP targetPort: 80 nodePort: 30001 - name: https port: 443 protocol: TCP targetPort: 443 nodePort: 30043 selector: app: linux66-nginx
创建对应pod:
[root@master yaml] Error from server (NotFound): error when creating "nginx.yaml" : namespaces "linux66" not found Error from server (NotFound): error when creating "nginx.yaml" : namespaces "linux66" not found [root@master yaml] namespace/linux66 created [root@master yaml] NAME STATUS AGE default Active 19d kube-node-lease Active 19d kube-public Active 19d kube-system Active 19d kubernetes-dashboard Active 16d linux66 Active 7s [root@master yaml] deployment.apps/linux66-nginx-deployment created service/linux66-nginx-service created
查看pod状态可以看到对应pod创建:
测试访问Nginx web界面:
使用yaml创建tomcat pod
Tomcat.yaml:
kind: Deployment apiVersion: apps/v1 metadata: labels: app: linux66-tomcat-app1-deployment-label name: linux66-tomcat-app1-deployment namespace: linux66 spec: replicas: 1 selector: matchLabels: app: linux66-tomcat-app1-selector template: metadata: labels: app: linux66-tomcat-app1-selector spec: containers: - name: linux66-tomcat-app1-container image: tomcat:7.0.94-alpine imagePullPolicy: Always ports: - containerPort: 8080 protocol: TCP name: http env: - name: "password" value: "123456" - name: "age" value: "18" resources: limits: cpu: 1 memory: "512Mi" requests: cpu: 500m memory: "512Mi" --- kind: Service apiVersion: v1 metadata: labels: app: linux66-tomcat-app1-service-label name: linux66-tomcat-app1-service namespace: linux66 spec: ports: - name: http port: 80 protocol: TCP targetPort: 8080 selector: app: linux66-tomcat-app1-selector
同样使用kubectl apply -f tomcat.yaml
创建
在nginx容器中添加location, 将login/的请求转发给后端tomcat:
[root@master yaml] NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES linux66-nginx-deployment-5b456947b4-6scvj 1/1 Running 0 52m 172.20.2.22 192.168.68.149 <none> <none> linux66-tomcat-app1-deployment-78b9b46cc-7jh7f 1/1 Running 0 2m42s 172.20.1.24 192.168.68.150 <none> <none> [root@master yaml] bash-4.4 bash-4.4 bash-4.4 bash-4.4 ---- [root@master yaml] NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES linux66-nginx-deployment-5b456947b4-6scvj 1/1 Running 0 55m 172.20.2.22 192.168.68.149 <none> <none> linux66-tomcat-app1-deployment-78b9b46cc-7jh7f 1/1 Running 0 5m26s 172.20.1.24 192.168.68.150 <none> <none> [root@master yaml] NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE linux66-nginx-service NodePort 10.68.201.209 <none> 80:30006/TCP,443:30443/TCP 123m linux66-tomcat-app1-service ClusterIP 10.68.235.125 <none> 80/TCP 73m [root@master yaml] root@linux66-nginx-deployment-5b456947b4-6scvj:/ server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } location /login { proxy_pass http://linux66-tomcat-app1-service; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } root@linux66-nginx-deployment-5b456947b4-6scvj:/ nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful root@linux66-nginx-deployment-5b456947b4-6scvj:/ 2022/05/16 11:06:00 [notice] 468
此时,访问 http://192.168.68.150:30006/login/index.jsp,可以发现流量正确转发给了tomcat
`