# 资源管理方式

  • 命令式对象管理
    • 直接使用命令行操作kubernetes
    • 简单,试用测试环境,不能审计和追踪
    • 一般做临时性查看工作
kubectl run nginx-pod --image=nginx:1.17.1 --port 80
1
  • 命令式对象配置
    • 通过命令和配置文件操作k8s
    • 开发环境、可以审计跟踪、项目大多文件操作麻烦
kubectl create/patch -f nginx-pod.yaml
1
  • 声名式对象配置
    • apply 命令和配置文件操作k8s
    • 只能能实现创建和更新操作
kubectl apply -f nginx-pod.yaml
1

# 命令式资源管理方式

kubectl [comand] [type] [name] [flags]
1
  • comand: kubectl --help
    • 指定资源的操作:常用命令
      • create edit get delete patch
      • explain 展示资源文档
      • run 运行镜像
      • expose 暴露资源为service
      • describe 查看详细描述信息
      • logs 输出容器在pod中的日志
      • attch 进入运行中的容器
      • exec 执行容器中的一个命令
      • cp 在pod内外复制文件
      • rollout 管理资源的发布
      • scale 容器Pod的数量
      • autoscale 自动调整Pod的数量
      • apply 通过文件配置
      • label 更新资源上的标签
      • cluster-info 显示集群信息
      • version 查看当前Server和Client的版本
  • type:
    • 通过 kubectl api-resources
    • 资源类型:
      • 集群级别
        • nodes no 集群节点
        • namespaces ns 隔离Pod使用
      • pod级别
        • pods po 容器
      • pod资源控制器
        • replicationcontrollers rc
        • replicasets rs
        • deployments deploy
        • daemonsets ds
        • jobs
        • cronjobs cj
        • horizontalpodautoscalers htp
        • statefulsets sts
      • 服务发现资源
        • services svc 统一pod对外接口
        • ingress ing 统一pod对外接口
      • 存储资源
        • volumeattachments
        • persistentvolumes pv
        • persistentvolumeclaims pvc
      • 配置资源
        • configmaps cm
        • secrets
      • deployment pod service
  • name:
    • 资源名
  • flags
    • 额外参数
kubectl get pod
kubectl get pod pod_name -o yaml # 以yaml格式显示pod信息
1
2

# demo

kubectl create ns dev
kubectl run pods --image=nginx.1.17.1 -n dev
kubectl get pods
kubectl delete pods pod-name
kubectl delete ns dev
1
2
3
4
5

# 命令式对象配置方式

命令+yaml文件 kubectl create/patch -f nginx-pod.yaml

  • nginx-pod.yaml
# 版本
apiVersion: v1
# 资源类型
kind: Namespace
# 参数
metadata:
    name: dev

---

apiVersion: v1
# 创建一个pod
kind: Pod
# 描述
metadata: 
  # 名字
  name: nginxpod
  # 所在命名空间
  namespace: dev
# 详情描述
spec:
  containers:
  # 容器使用的名字
  - name: nginx-containers
    # 容器中使用的镜像
    image: nginx:1.17.1

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

# 声名式对象配置

资源存在则更新,不存在则创建 kubectl apply -f nginx-pod.yaml