k8s-YAML详解

创建一个控制器

apiVersion: apps/v1
kind: Deployment
metadata:       //控制器元数据
  labels:      //自定义pod标签
    app: linux-nginx-deployment-label       //标签可以写多个,app值为pod标签
  name: linux-nginx-deployment             //pod名称
  namespace: default                      //pod所属名称空间
spec:   //定义pod规格
  replicas: 1    //副本数
  selector:   //定义标签选择器
    matchLabels:    //定义匹配的标签,标签选择器
      app: linux-nginx-selector      //匹配的目标标签 
  template:         //定义模板,模板起到描述要创建pod的作用
    metadata:      //元数据
      labels:      //模板标签
        app: linux-nginx-selector    //定义标签,必须等于Deployment.spec.seletor.matchLables
    spec:
      containers:
      - name: linux-nginx-container 
        image: nginx
        #command: ["/apps/tomcat/bin/run_tomcat.sh"] #容器启动执行的命令或脚本
        #imagePullPolicy: IfNotPresent
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        - containerPort: 443  //端口
          protocol: TCP      //端口使用的协议
          name: http        //端口名称,无实质性作用
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:            //对资源请求设置限制和上限
          limits:            //容器最大使用资源,上限
            cpu: 2          //CPU的限制,单位为core数
            memory: 2Gi    //内存限制,单位为Mib/Gib,用于docker run --memory参数
          requests:         //资源请求的限制,pod调度时对于node节点的资源请求
            cpu: 1         //CPU请求数,最少使用
            memory: 512Mi   //内存请求大小,调度时使用,定义初始值最小使用

创建一个SVC

kind: Service
apiVersion: v1
metadata:         //元数据
  name: linux-nginx-svc    //service名称
  namespace: default      //所属namespace,不同namespace资源相互隔离
  labels:                 
    app: linux-nginx     //service标签,用于匹配相同标签的pod,实现转发
spec:
  type: NodePort        //service映射类型
  ports:
  - name: http         //端口名称,无实质性作用
    port: 80          //service端口
    protocol: TCP     //协议类型
    targetPort: 80       //service转发到后端pod的端口
    nodePort: 30001     //映射到node对外暴露的端口
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
    nodePort: 30043
  selector:      //service标签选择器,匹配相同标签的pod,实现转发
    app: linux-nginx-selector   //将流量转发到标签相同的pod,须等于Deploment.spec.selector.matchLables

运行后查看svc与pod关联关系

可以看到SVC已经通过标签选择器关联到"app=linux-nginx-selector"的pod。

posted @ 2021-11-21 18:21  不会跳舞的胖子  阅读(338)  评论(0编辑  收藏  举报