• k8s篇二之、操作命令 与 yml配置文件编写


    一、名词解释

    kubectl 是k8s集群的命令行工具,通过它能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署,

    • Master: 集群控制节点,每个集群需要至少一个master节 点负责集群的管控
    • Node: 工作负载节点,由master分配容器到这些node工作节点上,然后node节点上的docker负责容器的运行
    • NameSpace: 命名空间,用来隔离pod的运行环境
    • Pod: kubernetes的最小控制单元,容器都是运行在pod中的,-个pod中可以有1个或者多个容器
    • Label: 标签,用于对pod进行分类,同一类pod会拥有相同的标签
    • Controller: 控制器,通过它来实现对pod的管理,比如启动pod、 停止pod、伸缩pod的数量等等
    • Service: pod对外服务的统一入口,下面可以维护者同一类的多个pod

    二、kubectl 常用命令

    kubectl命令的语法如下: kubectl [command] [type] [name] [flags]

    • comand: 指定要对资源执行的操作,例如create、 get、delete
    • type: 指定资源类型,比如deployment、pod、 service
    • name: 指定资源的名称,名称大小写敏感
    • flags: 指定额外的可选参数

    1、基础

    kubectl cluster-info      ## 查看k8s 集群的信息
    kubectl version           ## 查看k8s 集群的版本
    kubectl get nodes         ## 查询k8s集群节点
    kubectl get pods,service  ## 查询正在运行的服务
    
    • 1
    • 2
    • 3
    • 4

    2、命名空间namespace相关查询

    
    kubectl create namespace test-dev                  # 命令形式创建一个namespace
    kubectl create ns test-dev                         # 命令形式创建一个namespace (简写)
    kubectl get ns                                     # 查询所有的namespace
    
    kubectl delete ns test-dev                         # 删除命名空间namespace (会同时删除pod)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、pod 相关查询

    
    kubectl get pod                                          # 查看所有的pod
    kubectl get pods -n test-dev                             # 查看指定命名空间下的pod (指定具体命名空间test-dev)
    kubectl describe pod nginx-6867cdf567-94kcb              # 查看pod详细内容(nginx-6867cdf567-94kcb pod需要存在)
    kubectl describe pod nginx-6867cdf567-94kcb -n test-dev  # 查看该pod的详细信息(指定具体命名空间test-dev)
    kubectl get pod -n test-dev -o wide                      # 查看该pod 的id
    kubectl get deployment -n test-dev                       # 查询所有的test-dev 下的pod控制器  
    
    
    kubectl delete deployment nginx -n test-dev                  # 删除pod控制器
    kubectl delete pods pod nginx-6867cdf567-94kcb -n test-dev   # 删除指定命名空间下pod (删除后pod控制器会自动在创建新的pod)
    
    kubectl delete pod nginx-6867cdf567-94kcb -n default --force --grace-period=0  # 强制删除
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4、创建 pod 运行容器(nginx)

    # 示例1:在test-dev命名空间下运行一个nginx 的 pod
    kubectl run pod --image=nginx -n test-dev
        
    # 示例2: 指定版本端口以及名称
    kubectl run nginx --image=nginx:1.17.9 --port=80 --namespace=test-dev
    
    # 可先查pod详情获取容器ip ,在使用crud 执行容器内部ip (对应节点中),判断容器是否运行成功
    curl 172.17.0.2      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5、标签

    kubectl label pod nginx-pod -n mayikt-sit version=1.0              # 为我们的pod打标签
    kubectl get pod -n test-dev --show-labels                          # 查看标签
    kubectl label pod nginx-pod -n  test-dev version=2.0 --overwrite   # 更新标签
    
    • 1
    • 2
    • 3

    6、操作示例 (完整流程演示)

    先创建命名空间,在创建pod, 在使用curl 检查服务是否正常

    # 1、创建一个命名空间
    kubectl create namespace test-dev 
    
    # 2、查询命名空间列表
    kubectl get ns 
    
    # 3、创建一个 nginx 的 pod 服务
    kubectl run nginx --image=nginx:1.17.9 --port=80 --namespace=test-dev
    
    # 4、查询指定命名空间的 pod 列表
    kubectl get pods -n test-dev
    
    # 5、查询 nginx pod  的详细信息,查看所在节点,内部ip等
    kubectl describe pod nginx-864f9875b9-h467q -n test-dev
    
    # 6、在指定节点中执行 curl 中服务服务
    curl 172.17.0.2
    
    # 7、删除命名空间(会同步删除pod)
    kubectl delete ns test-dev
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    三、 命令式对象配置 (yml)

    在服务器上创建一个 yml 文件 test-pro.yml , 类似于 docker-compose

    1、编写配置

    1.1、简写配置

    创建 test-pro.yml 内容如下

    ## Namespace 命名空间配置 (多个玩家相同配置重复执行只会创建一次)
    apiVersion: v1
    kind: Namespace
    metadata:
      name: test-dev           # 名称    
      
    ---
    
    ## pod 配置
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-pod          # 指定pod的名称
      namespace: test-dev      # 指定该pod对应的Namespace
      labels:                  # 指定labels标签
        version: "8.0"   
        env: "sit"
    spec:
      # 支持配置多个
      containers: 
      - name: nginx-container   # 运行一个nginx容器
        image: nginx:1.17.9     # 指定镜像的名称版本
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    1.2、较完整配置
    ## Namespace 命名空间配置 (多个玩家相同配置重复执行只会创建一次)
    apiVersion: v1
    kind: Namespace
    metadata:
      name: test-dev           # 命名空间名称
    ---
    
    ## pod 配置
    apiVersion: v1
    kind: Pod
    metadata:
      name: xijia-nginx-pod    # 指定pod的名称
      namespace: test-dev      # 指定该pod对应的Namespace
      labels:                  # 指定labels标签
        version: "8.0"
        env: "sit"
    spec:
      # 容器集:支持配置多个
      containers:
        ## 容器1
        - name: xijia-nginx-container     # 运行一个nginx容器
          image: nginx:1.17.9             # 指定镜像的名称版本
          imagePullPolicy: IfNotPresent   # 1、用于设置镜像拉取策略
          env:                            # 2、设置环境变量列表(容器中可读取 echo $xijia)
            - name: "xijia"
              value: "1"
          ports:                          # 3、设置容器暴露的端口列表
            - name: xijia-nginx
              containerPort: 80
              protocol: TCP
          resources:                      # 4、资源配额
            limits:  #限制资源(上限)
              cpu: "2"         # 限制 CPU核心线程数
              memory: "1Gi"    # 内存限制
            requests: #请求资源(下限)
              cpu: "1"         # CPU限制,单位是core数
              memory: "512Mi"  # 内存限制
        ## 容器2
        - name: xijia-tomcat-container
          image: tomcat:8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    1、镜像拉取策略(imagePullPolicy )

    • 1.Always: 每次都是从远程仓库拉取镜像
    • 2.IfNotPresent: 如果本地有该镜像则使用本地,如果本地没有该镜像则使用远程
    • 3.Never: 只使用本地镜像,不去远程仓库拉取,本地没有该镜像则会报错

    2、环境变量配置(env)

    • name : 环境变量的key
    • value : 环境变量的值

    3、端口映射(ports ) (ports , name # 端口的名称,name在pod中是唯一的不允许重复)

    • containerPort #容器要暴露的端口 (0
    • hostPort #容器要映射到主机上的端口
    • hostIP # 容器要映射到主机的IP(-般省略)
    • protocol # 端口协议,必须是UDP、TCP或SCTP。 默认为“TCP"。

    4、资源限制(resources)

    • 1.limits: 限制容器运行时的最大占用资源,当容器占用资源超过limits设置的值时会被终止,并进行重启;
    • 2.requests :用于设置限制容器需要的最小资源,如果环境资源不够,容器则将无法启动
    • cpu: 核心数数
    • memory: 内存大小,可以使用Gi、Mi、G、M等形式
    1.3、完整清单
    apiVersion: v1      #必填,版本号,例如v1
    kind: Pod           #必填,资源类型,例如Pod、service、Deployment
    metadata:           #必填,元数据
      name: mayikt-pod  #必填,Pod名称
      namespace: mayikt-namespace01 #Pod所属的命名空间, 默认为”default"
      labels:           #自定义标签列表
        - name: v1
    spec:               #必填,Pod中容器的详细定义
      containers:       #必填 , Pod中容器列表
        - name: mayikt-container01  #必填,容器名称
          image: nginx:1.7.9 #必填, 容器的镜像名称
          imagePullPolicy: [ Always Never |IfNotPresent] #获取镜像的策略
          command: [string] #容器的启动命令 列表,如不指定,使用打包时使用的启动命令
          args: [string]    #容器的启动命令参数列表
          workingDir: string #容器的工作目录
          volumeMounts:      #挂载到容器内部的存储卷配置
          - name: string     #引用pod定义的共享存储卷的名称,需用volumes[ ]部分定义的的卷名
            mountPath: string #存储卷在容器内mount的绝对路径,应少于512字符
            read0nly: boolean #是否为只读模式
          ports: #需要暴露的端口库号列表
          - name: string   #端口的名称
            containerPort: int #容器需要 监听的端口号
            hostPort: int   #容器所在主机需要监听的端口号,默认与Container相同
            protocol: string  #控端口协议,支持TCP和UDP,默认TCP
          env:      #容器运行前需设置的环境变量列表
          - name: string    #环境变量名称
            value: string #环境变量的值
          resources: #资源限制和请求的设置
            limits: #资源限制的设置
              cpu: string  #Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
              memory: string #内存限制, 单位可以为Mib/Gib,将用于docker run --memory参数
            requests: #资源请求的设置
              cpu: string     #Cpu请求,容器启动的初始可用数量
              memory: string #内存请求,容器启动时的初始可用数量
          lifecycle: #生命周期钩子
              postStart: #容器启动后立即执行此钩子,如果执行失败,会根据重启策略进行重启
              preStop: #容器终止前执行此钩子,无论结果如何,容器都会终止
          livenessProbe: #树对Pod内各容器健康检查的设置, 当探测无响应几次后将自动重启该容器
            exec:    #对Pod容器内检查方式设置为exec方式
              command: [string] #exec方式需要 制定的命令或脚本
            httpGet:     #对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
              path: string
              port: number
              host: string
              scheme: string
              HttpHeaders:
              - name: string
                value: string
            tcpSocket: #对Pod内个容器 健康检查方式设置为tcpSocket方式
               port: number
    
    ## =================================================
    apiVersion: v1 # 必选,API的版本号
    kind: Pod # 必选,类型Pod
    metadata: # 必选,元数据即基础信息
    name: nginx # 必选,符合RFC 1035规范的Pod名称
    namespace: web-testing # 可选,不指定默认为default,Pod所在的命名空间
    labels: # 可选,标签选择器,一般用于Selector
    - app: nginx
    annotations: # 可选,注释列表
    - app: nginx
    spec: # 必选,用于定义容器的详细信息
    containers: # 必选,容器列表
    
    name: nginx # 必选,符合RFC 1035规范的容器名称
    image: nginx:v1 # 必选,容器所用的镜像的地址
    
    imagePullPolicy: Always # 可选,镜像拉取策略
    镜像拉取策略分三种:
    Always不管镜像存不存在本地都拉取(默认)
    Nerver 不管镜像存不存在本地都不拉取
    IfNotpresent 本地有镜像不拉取,没有就拉取
    
    workingDir: /usr/share/nginx/html # 可选,容器的工作目录
    volumeMounts: # 可选,存储卷配置
    
    name: webroot # 存储卷名称
    mountPath: /usr/share/nginx/html # 挂载目录
    readOnly: true # 只读
    ports: # 可选,容器需要暴露的端口号列表
    
    name: http # 端口名称
    containerPort: 80 # 端口号
    protocol: TCP # 端口协议,默认TCP
    env: # 可选,环境变量配置
    
    name: TZ # 变量名
    value: Asia/Shanghai
    name: LANG
    value: en_US.utf8
    resources: # 可选,资源限制和资源请求限制
    limits: # 最大限制设置
    cpu: 1000m
    memory: 1024MiB
    requests: # 启动所需的资源
    cpu: 100m
    memory: 512MiB
    readinessProbe: # 可选,容器状态检查
    httpGet: # 检测方式
    path: / # 检查路径
    port: 80 # 监控端口
    timeoutSeconds: 2 # 超时时间
    initialDelaySeconds: 60 # 初始化时间
    livenessProbe: # 可选,监控状态检查
    exec: # 检测方式
    command:
    - cat
    - /health
    httpGet: # 检测方式
    path: /_health
    port: 8080
    httpHeaders:
    - name: end-user
    value: jason
    tcpSocket: # 检测方式
    port: 80
    initialDelaySeconds: 60 # 初始化时间
    timeoutSeconds: 2 # 超时时间
    periodSeconds: 5 # 检测间隔
    successThreshold: 2 # 检查成功为2次表示就绪
    failureThreshold: 1 # 检测失败1次表示未就绪
    securityContext: # 可选,限制容器不可信的行为
    provoleged: false
    restartPolicy: Always # 可选,默认为Always
    nodeSelector: # 可选,指定Node节点
    region: subnet7
    imagePullSecrets: # 可选,拉取镜像使用的secret
    
    name: default-dockercfg-86258
    hostNetwork: false # 可选,是否为主机模式,如是,会占用主机端口
    volumes: # 共享存储卷列表
    
    name: webroot # 名称,与上述对应
    emptyDir: {} # 共享卷类型,空
    hostPath: # 共享卷类型,本机目录
    path: /etc/hosts
    secret: # 共享卷类型,secret模式,一般用于密码
    secretName: default-token-tf2jp # 名称
    defaultMode: 420 # 权限
    configMap: # 一般用于配置文件
    name: nginx-conf
    defaultMode: 420
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144

    2、执行配置命令

    相关操作命令

    kubectl create -f test-dev.yml    # 创建 Namespace 和 Pod (首次添加yml执行)
    kubectl apply -f test-dev.yml         # 更新 Namespace 和 Pod (修改yml内容后执行)
    
    kubectl get ns                        # 查询命名空间     
    kubectl get pods -n test-dev          # 查询 pod
    
    # 可先查pod详情获取容器ip ,在使用crud 执行容器内部ip,判断容器是否运行成功
    curl 172.17.0.2       
    
    
    kubectl get -f test-dev.yml          # 查看资源
    kubectl delete -f test-dev.yml       # 删除(同时删除 pod 和 Namespace)
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    APP启动流程一(源码30)之向Zygote发送创建APP进程的请求
    MyBatis
    Altium Designer实用系列(一)----原理图导入PCB、PCB板子外形、多层板绘制等
    pg distinct 改写递归优化(德哥的思路)
    10:00面试,10:08就出来了,技术官问我K8s的核心概念!
    多地接连进入汛期,如何做好积水监测站的管理?
    elementUI-表单-校验
    【服务器】fiber协程模块
    STM8的C语言编程(1)--基本程序与启动代码分析
    JVM内存模型(面试题)
  • 原文地址:https://blog.csdn.net/qq_41463655/article/details/126927531