YAML文件创建资源对象

1. k8s YAML文件讲解

2. 配置文件详解

  • deployment详解

    apiVersion: apps/v1     # apiVersion是指定api版本
    kind: Deployment        # kind是指定资源版本
    metadata:               # metadata 资源的元素
      name: test             # 指定名称
      namespace: default     # 这是指定namespace的命名空间
      
      
    spec:                   # 资源规格
      replicas: 3           # 设置副本数几个
      selector:             # 标签选择器
        matchLabels:
          app: test          # pod 标签名字
          
          
      template:              # pod的模板
        metadata:            # pod的元数据
          labels:
            app: test         # pod的标签名
      
        spec:                # pod的规格
          containers:        # 容器配置
          - name: test         # 容器名称
            image: nginx:1.16     # 镜像
    
  • 端口对外暴露

    apiVersion: v1      # apiVersion是指定api版本
    kind: Service       # kind是指定资源版本
    metadata:           # metadata 资源的元素
      name: web          # 指定名称
      namespace: default   # 这是指定namespace的命名空间
      
    spec:                  # 资源规格
      ports:               # 端口
      - port: 88           # service负载均衡服务内部通信端口88
        protocol: TCP       # 端口协议
        targetPort: 80      #  容器内部访问的端口
      selector:
        app: web
      type: NodePort          # 外部暴露端口
    
  • 注释:框架大致是一样的,每个容器的配置,都不太一样

3. 案例

3.1 创建deployment服务

  • 创建deployment应用

    [root@k8s-master yaml]# vim yaml-test.yaml 
    [root@k8s-master yaml]# cat yaml-test.yaml 
    apiVersion: apps/v1     # apiVersion是指定api版本
    kind: Deployment        # kind是指定资源版本
    metadata:               # metadata 资源的元素
      name: test             # 指定名称
      namespace: default     # 这是指定namespace的命名空间
      
      
    spec:                   # 资源规格
      replicas: 3           # 设置副本数几个
      selector:             # 标签选择器
        matchLabels:
          app: test          # pod 标签名字
          
          
      template:              # pod的模板
        metadata:            # pod的元数据
          labels:
            app: test         # pod的标签名
      
        spec:                # pod的规格
          containers:        # 容器配置
          - name: test         # 容器名称
            image: nginx:1.16     # 镜像
    
    [root@k8s-master yaml]# kubectl apply -f yaml-test.yaml 
    deployment.apps/test created
    
  • 应用创建的资源描述yaml文件

    # 部署应用
    kubectl apply -f yaml-test.yaml 
    
    # 卸载
    kubectl delete -f yaml-test.yaml 
    
  • 注释:

    ​ kubectl create -f yaml-test.yaml 第一次可以启动,但是我们在后面修改一下配置,就会报出,服务已存在

    ​ kubectl apply -f yaml-test.yaml 就不会出现create的问题,yaml创建服务,推荐使用apply的方式

3.2 将上面的deployment服务对外暴露

  • 将deployment应用对外暴露

    [root@k8s-master yaml]# vim service-test.yaml
    [root@k8s-master yaml]# cat service-test.yaml
    apiVersion: v1      # apiVersion是指定api版本
    kind: Service       # kind是指定资源版本
    metadata:           # metadata 资源的元素
      name: test          # 指定名称
      namespace: default   # 这是指定namespace的命名空间
      
    spec:                  # 资源规格
      ports:               # 端口
      - port: 88           # service负载均衡服务内部通信端口88
        protocol: TCP       # 端口协议
        targetPort: 80      #  容器内部应用的端口
      selector:
        app: test
      type: NodePort          # 外部暴露端口
    
    
  • 应用文件

    [root@k8s-master yaml]# kubectl apply -f service-test.yaml 
    service/test created
    

3.3 验证应用

[root@k8s-master yaml]# kubectl get pods,service 
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-s5wvx   1/1     Running   0          20h
pod/test-5f655598-5jfrt      1/1     Running   0          28m
pod/test-5f655598-bhhm4      1/1     Running   0          28m
pod/test-5f655598-v5l8f      1/1     Running   0          28m
pod/web-674477549d-flj78     1/1     Running   0          19h
pod/web-674477549d-m7lsj     1/1     Running   0          166m
pod/web-674477549d-stk84     1/1     Running   0          166m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        2d1h
service/nginx        NodePort    10.109.59.90     <none>        80:30836/TCP   20h
service/test         NodePort    10.102.164.143   <none>        88:31756/TCP   4m52s
service/web          NodePort    10.102.43.204    <none>        80:30291/TCP   19h

  • 浏览器访问测试
原文地址:https://www.cnblogs.com/scajy/p/14823152.html