• K8s--03 资源类型


    一、常用资源类型

    1.工作负载类型
      RC  ReplicaController
      RS  ReplicaSet 
      DP  Deployment
      DS  DaemonSet  
    
    2.服务发现及负载均衡
      Service 
      Ingress 
    
    3.配置与存储资源
      ConfigMap 存储配置文件
      Secret    存储用户字典
    
    4.集群级别资源
      Namespace
      Node
      Role
      ClusterRole
      RoleBinding
      ClusterRoleBinding
    
    

    二、资源配置清单

    1.创建资源的方法
      apiserver仅能接受json格式的资源定义 
      yaml格式提供的清单,apiserver可以自动将其转换为json格式再提交
    
    2.资源清单介绍
      查看资源清单所需字段
      kubectl explain pod
    
      资源清单字段介绍
      apiVersion: v1  #属于k8s哪一个API版本或组
      kind: Pod	  #资源类型
      metadata:	  #元数据,嵌套字段
      spec:		  #定义容器的规范,创建的容器应该有哪些特性
      
      status: 	  #只读的,由系统控制,显示当前容器的状态
    
    
      json嵌套
    { 1级:
         { 2级:
              { 3级:Value
    
              }
          }
    }
    
    
    3.查看资源清单嵌套的命令
      kubectl explain pod
      kubectl explain pod.spec
      kubectl explain pod.spec.volumes
    
    4.使用命令行创建一个pod
      kubectl create deployment nginx --image=nginx:alpine
      kubectl get pod -o wide
    
    
    5.将刚才创建的pod配置到处成yaml格式
      kubectl get pod -o yaml > nginx-pod.yaml
      
      精简资源清单,删掉不需要的配置
    cat nginx-pod.yaml 
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
    
      json格式写法:
    {
     apiVersion: "v1",
     kind: "Pod",
     metadata: 
       {
          name: "nginx", 
          labels: 
            {
               app: "nginx"
            }        
        }
     spec: 
       {
         containers:
           {
             name: "nginx",
             image: "nginx:alpine",
             imagePullPolicy: "IfNotPresent"
           }
       }    
    }       
    
      删除命令行创建的资源
      kubectl delete deployments.apps nginx
    
      应用资源配置清单
      kubectl create -f nginx-pod.yaml
    
      查看pod信息
      kubectl get pod -o wide
    
      查看pod详细信息
      kubectl describe pod nginx
    

    三、查看node标签

    Node打标签
    
    1.查看node的标签
    kubectl get node --show-labels
    
    2.给node打标签
    kubectl label nodes node2 CPU=Xeon
    kubectl label nodes node3 disktype=ssd
    
    3.编辑POD资源配置清单,使用node标签选择器
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.0
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
      nodeSelector:
        #CPU: Xeon
        disktype: SSD
    
    4.删除容器重新创建
    kubectl delete pod nginx
    kubectl create -f nginx-pod.yaml
    
    5.查看结果
    kubectl get pod -o wide
    
    6.删除节点标签
    kubectl label nodes node2 CPU-
    kubectl label nodes node3 disktype-
    
    

    四、pod容器打标签

    查看pod标签
    1.标签说明
      一个标签可以给多个POD使用
      一个POD也可以拥有多个标签
        
    2.查看POD标签
      kubectl get pod --show-labels
    
    3.添加标签方法
    方法1:直接编辑资源配置清单:
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        app: nginx
        release: beta
    ----------------------
    方法2:命令行打标签
    kubectl label pods nginx release=beta
    kubectl label pods nginx job=linux
    kubectl get pod --show-labels  
    
    
    4.删除标签
    kubectl label pod nginx job- 
    kubectl get pod --show-labels
    
    
    5.实验: 生成2个POD,打上不同的标签,然后根据标签选择
    kubectl create deployment nginx --image=nginx:1.14.0
    kubectl get pod --show-labels
    kubectl label pods nginx-xxxxxxxx release=stable
    kubectl get pod --show-labels
    
    根据标签查看
    kubectl get pods -l release=beta --show-labels 
    kubectl get pods -l release=stable --show-labels 
    
    
    根据标签删除
    kubectl delete pod -l app=nginx
    
    

    五、实战操作步骤:

    [root@node1 ~]# vim nginx-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
    #先查看pod信息
    [root@node1 ~]# kubectl get pod
    No resources found in default namespace.
    #应用资源配置清单
    (注意: 如果有,就先删除 kubectl  delete dp nginx)
    [root@node1 ~]# kubectl  create -f nginx-pod.yaml
    pod/nginx created
    
    #查看pod详细信息
    [root@node1 ~]# kubectl get pod -o wide
    NAME    READY   STATUS    RESTARTS   AGE   IP         NODE    NOMINATED NODE   READINESS GATES
    nginx   1/1     Running   0          30s   10.2.1.2   node2   <none>           <none>
    #访问查看
    [root@node1 ~]# curl 10.2.1.2
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
             35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    

    运行一个demo小服务

    1.编写资源配置清单
    
    [root@node1 ~/demo]# cat tomcat-demo.yaml 
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: mysql
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - name: mysql
            image: mysql:5.7
            ports:
            - containerPort: 3306
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
    spec:
      ports:
        - port: 3306
      selector:
        app: mysql
    ---
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: myweb
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: myweb
      template:
        metadata:
          labels:
            app: myweb
        spec:
          containers:
          - name: myweb
            image: kubeguide/tomcat-app:v1
            ports:
            - containerPort: 8080
            env:
            - name: MYSQL_SERVICE_HOST
              value: 'mysql'
            - name: MYSQL_SERVICE_PORT
              value: '3306'
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: myweb
    spec:
      type: NodePort
      ports:
        - port: 8080
          nodePort: 30001
      selector:
        app: myweb
    
    =====================================
    或者分开写四个yaml文件
    cat mysql-dp.yaml
    
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: mysql
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - name: mysql
            image: mysql:5.7
            ports:
            - containerPort: 3306
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
    
    ----------------------------------------------------
    cat mysql-svc.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
    spec:
      ports:
        - port: 3306
      selector:
        app: mysql
    -----------------------------------------------
    cat tomcat-dp.yaml
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: myweb
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: myweb
      template:
        metadata:
          labels:
            app: myweb
        spec:
          containers:
          - name: myweb
            image: kubeguide/tomcat-app:v1
            ports:
            - containerPort: 8080
            env:
            - name: MYSQL_SERVICE_HOST
              value: 'mysql'
            - name: MYSQL_SERVICE_PORT
     
             value: '3306'
    -----------------------------------------------------
    cat tomcat-svc.yaml
    
    apiVersion: v1
    kind: Service
    metadata:
      name: myweb
    spec:
      type: NodePort
      ports:
        - port: 8080
          nodePort: 30001
      selector:
        app: myweb
    ---------------------------------------------------
    
    

    2.应用资源配置清单

    [root@node1 ~]# kubectl create -f tomcat-demo.yaml 
    deployment.apps/mysql created
    service/mysql created
    deployment.apps/myweb created
    service/myweb created
    
    3.查询资源是否创建成功
    [root@node1 ~]# kubectl get pod -o wide 
    NAME                     READY   STATUS    RESTARTS   AGE    IP         NODE    NOMINATED NODE   READINESS GATES
    mysql-8fcd9f64-vqkm9     1/1     Running   0          2m1s   10.2.1.2   node2   <none>           <none>
    myweb-6f974fdbdc-gsncp   1/1     Running   0          2m     10.2.1.3   node2   <none>           <none>
    myweb-6f974fdbdc-ngngv   1/1     Running   0          2m     10.2.2.2   node3   <none>           <none>
    
    4.查询暴露的端口
    [root@node1 ~]# kubectl get service 
    NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
    kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP          22h
    mysql        ClusterIP   10.1.120.187   <none>        3306/TCP         3m32s
    myweb        NodePort    10.1.37.225    <none>        8080:30001/TCP   3m31s
    
    5.浏览器登录查看
    http://10.0.0.11:30001/demo/index.jsp
    

  • 相关阅读:
    git如何忽略文件或者文件夹
    jsonp跨域请求发布出去
    PHP
    curl保存图片
    curl 请求
    输入框纯数字
    PHP允许AJAX跨域请求的两种方法
    IOS开发笔记 IOS如何访问通讯录
    Android 蓝牙开发(整理大全)
    新的移动服务示例
  • 原文地址:https://www.cnblogs.com/gongjingyun123--/p/12543106.html
Copyright © 2020-2023  润新知