目录

1.1陈述式资源管理方法

1.1.1.管理名称空间资源

1.1.2.管理Deployment资源

1.1.3.管理Service资源

1.1.4.kubectl用法总结

1.2.声明式资源管理方法

1.1.1.管理名称空间资源

查看名称空间

[root@hdss7- ~]# kubectl get namespace
NAME STATUS AGE
default Active 4d19h
kube-node-lease Active 4d19h
kube-public Active 4d19h
kube-system Active 4d19h

  

 [root@hdss7-21 ~]# kubectl get ns
 NAME STATUS AGE
 default Active 4d19h
 kube-node-lease Active 4d19h
 kube-public Active 4d19h
 kube-system Active 4d19h

查看名称空间内的资源

[root@hdss7- ~]# kubectl get all -n default
NAME READY STATUS RESTARTS AGE
pod/nginx-ds-7hg9l / Running 3d18h
pod/nginx-ds-tnhsg / Running 3d18h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.0.1 <none> /TCP 4d19h NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/nginx-ds <none> 3d18h

创建名称空间

[root@hdss7- ~]# kubectl create namespace app
namespace/app created
[root@hdss7- ~]# kubectl get namespace
NAME STATUS AGE
app Active 35s
default Active 4d20h
kube-node-lease Active 4d20h
kube-public Active 4d20h
kube-system Active 4d20h

删除名称空间

[root@hdss7- ~]# kubectl delete ns app
namespace "app" deleted
[root@hdss7- ~]# kubectl get ns
NAME STATUS AGE
default Active 4d20h
kube-node-lease Active 4d20h
kube-public Active 4d20h
kube-system Active 4d20h

1.1.2.管理Deployment资源

创建deployment

 [root@hdss7-21 ~]# kubectl create deployment nginx-dp --image=harbor.fx.com/public/nginx:v1.7.9 -n kube-public
 deployment.apps/nginx-dp created

查看deployment

  • 简单查看
[root@hdss7- ~]# kubectl get deployment -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-dp / 56s
  • 扩展查看
[root@hdss7- ~]# kubectl get deployment -n kube-public -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-dp / 20m nginx harbor.fx.com/public/nginx:v1.7.9 app=nginx-dp
  • 详细查看
[root@hdss7- ~]# kubectl describe deployment nginx-dp -n kube-public
Name: nginx-dp
Namespace: kube-public
CreationTimestamp: Mon, Jun :: +
Labels: app=nginx-dp
Annotations: deployment.kubernetes.io/revision:
Selector: app=nginx-dp
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate
MinReadySeconds:
RollingUpdateStrategy: % max unavailable, % max surge
Pod Template:
Labels: app=nginx-dp
Containers:
nginx:
Image: harbor.fx.com/public/nginx:v1.7.9
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-dp-6f4ddd775 (/ replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m31s deployment-controller Scaled up replica set nginx-dp-6f4ddd775 to

查看pod资源

  • 简单查找
[root@hdss7- ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-dp-6f4ddd775-98dbq / Running 107m
  • 扩展查找
[root@hdss7- ~]# kubectl get pods -n kube-public -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-dp-6f4ddd775-98dbq / Running 108m 172.7.21.3 hdss7-.host.com <none> <none>
  • 扩展查找
[root@hdss7- ~]# kubectl describe pods nginx-dp-6f4ddd775-98dbq -n kube-public
Name: nginx-dp-6f4ddd775-98dbq
Namespace: kube-public
Priority:
Node: hdss7-.host.com/10.4.7.21
Start Time: Mon, Jun :: +
Labels: app=nginx-dp
pod-template-hash=6f4ddd775
Annotations: <none>
Status: Running
IP: 172.7.21.3
Controlled By: ReplicaSet/nginx-dp-6f4ddd775
Containers:
nginx:
Container ID: docker://93213abdbfb595ebe213c6764f0af4932a40c2ceaa5efc562fac9f2cde5b69fe
Image: harbor.fx.com/public/nginx:v1.7.9
Image ID: docker-pullable://harbor.fx.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
Port: <none>
Host Port: <none>
State: Running
Started: Mon, Jun :: +
Ready: True
Restart Count:
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-zld8g (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-zld8g:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-zld8g
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>

进入pod资源

[root@hdss7- ~]# kubectl exec -it nginx-dp-6f4ddd775-98dbq /bin/bash -n kube-public
root@nginx-dp-6f4ddd775-98dbq:/#

注:也可以使用docker exec进入容器

删除pod资源(重启)

[root@hdss7- ~]# kubectl delete pods nginx-dp-6f4ddd775-98dbq -n kube-public
pod "nginx-dp-6f4ddd775-98dbq" deleted

强制删除参数: --force-grace-period=0

删除deployment 

[root@hdss7- ~]# kubectl delete deployment nginx-dp -n kube-public
deployment.extensions "nginx-dp" deleted
[root@hdss7-21 ~]# kubectl get deployment -n kube-public
No resources found.
[root@hdss7-21 ~]# kubectl get pods -n kube-public
No resources found.

1.1.3 管理Service资源

 创建Service

[root@hdss7- ~]# kubectl create deployment nginx-dp --image=harbor.fx.com/public/nginx:v1.7.9 -n kube-public
deployment.apps/nginx-dp created
[root@hdss7- ~]# kubectl expose deployment nginx-dp --port= -n kube-public
service/nginx-dp exposed
[root@hdss7- ~]# kubectl scale deployment nginx-dp --replicas= -n kube-public
deployment.extensions/nginx-dp scaled
[root@hdss7- ~]# ipvsadm -Ln
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.1: nq
-> 10.4.7.21: Masq
-> 10.4.7.22: Masq
TCP 192.168.164.107: nq
-> 172.7.21.3: Masq
-> 172.7.22.3: Masq
[root@hdss7- ~]# kubectl get service -n kube-public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-dp ClusterIP 192.168.164.107 <none> /TCP 6m54s

查看service

[root@hdss7- ~]# kubectl describe service nginx-dp -n kube-public
Name: nginx-dp
Namespace: kube-public
Labels: app=nginx-dp
Annotations: <none>
Selector: app=nginx-dp
Type: ClusterIP
IP: 192.168.164.107
Port: <unset> /TCP
TargetPort: /TCP
Endpoints: 172.7.22.3:
Session Affinity: None
Events: <none>
1.1.4.kubectl用法总结

陈述式资源管理方法小结:

  • kubernetes集群管理集群的唯一入口是通过相应的方法调用apiserver的接口。
  • kubectl是官方的CLI命令行工具,用于与apiserver进行通信,将用户在命令行输入的命令,组织并转化为apiserver能识别的信息,进而实现管理K8S各种资源的一种有效途径。
  • kubectl的命令大全
  • 陈述式资源管理方法可以满足90%以上的资源管理需求,但它的缺点也很明显

    • 命令冗长、复杂、难以记忆。
    • 特定场景下,无法实现管理需求。
    • 对资源的增、删、查操作比较容易,改就很痛苦。
1.2.声明式资源管理方法

 声明式资源管理方法依赖于一资源配置清单(yaml/json)

查看资源配置清单的方法

[root@hdss7- ~]# kubectl get pods nginx-dp-6f4ddd775-x9hjg -o yaml -n kube-public
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2020-06-08T05:56:16Z"
generateName: nginx-dp-6f4ddd775-
labels:
app: nginx-dp
pod-template-hash: 6f4ddd775
name: nginx-dp-6f4ddd775-x9hjg
namespace: kube-public
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: nginx-dp-6f4ddd775
uid: 872e6838-b8ef-4a6a-8cb2-98a94dca0c91
resourceVersion: ""
selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-6f4ddd775-x9hjg
uid: 7a04423c-add2-45ee--c6e80545cb9a
spec:
containers:
- image: harbor.fx.com/public/nginx:v1.7.9
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-zld8g
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: hdss7-.host.com
priority:
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds:
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds:
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds:
volumes:
- name: default-token-zld8g
secret:
defaultMode:
secretName: default-token-zld8g
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-06-08T05:56:16Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2020-06-08T05:56:17Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2020-06-08T05:56:17Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2020-06-08T05:56:16Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://f5e7ac3cef3c1439df242cc2519ca22c7b942ec44099ffdd0258fed05a075dea
image: harbor.fx.com/public/nginx:v1.7.9
imageID: docker-pullable://harbor.fx.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
lastState: {}
name: nginx
ready: true
restartCount:
state:
running:
startedAt: "2020-06-08T05:56:17Z"
hostIP: 10.4.7.22
phase: Running
podIP: 172.7.22.3
qosClass: BestEffort
startTime: "2020-06-08T05:56:16Z" [root@hdss7- ~]# kubectl get svc nginx-dp -o yaml -n kube-public
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2020-06-08T05:58:22Z"
labels:
app: nginx-dp
name: nginx-dp
namespace: kube-public
resourceVersion: ""
selfLink: /api/v1/namespaces/kube-public/services/nginx-dp
uid: df2f2bd9-bfa7-4cdb-b25b-1488e63944a9
spec:
clusterIP: 192.168.164.107
ports:
- port:
protocol: TCP
targetPort:
selector:
app: nginx-dp
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}

解释资源配置清单

 [root@hdss7-21 ~]# kubectl explain service

创建资源配置清单

[root@hdss7- ~]# vim nginx-ds.yaml
apiVersion: v1
kind: Service
metadata:
labels:
nginx: nginx-ds
name: nginx-ds
namespace: default
spec:
ports:
- port:
protocol: TCP
targetPort:
selector:
app: nginx-ds
type: ClusterIP

应用资源配置清单

[root@hdss7- ~]# kubectl create -f nginx-ds.yaml
service/nginx-ds configured
[root@hdss7- ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> /TCP 5d1h
nginx-ds ClusterIP 192.168.100.178 <none> /TCP 8m54s

修改资源配置清单

  • 离线修改
[root@hdss7- ~]# docker login docker.io
[root@hdss7- ~]# docker pull fangxing1001/nginx:curl
[root@hdss7- ~]# vim nginx-ds.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-test
spec:
template:
metadata:
labels:
app: nginx-test
spec:
containers:
- name: mynginx
image: harbor.fx.com/public/nginx:v1.7.9
ports:
- containerPort:
[root@hdss7- ~]# kubectl apply -f nginx-ds.yaml
deployment.extensions/nginx-test created
[root@hdss7- ~]# vim nginx-ds.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-test
spec:
template:
metadata:
labels:
app: nginx-test
spec:
containers:
- name: mynginx
image: harbor.fx.com/public/nginx:curl
ports:
- containerPort:
[root@hdss7- ~]# kubectl apply -f nginx-ds.yaml
deployment.extensions/nginx-test configured
[root@hdss7- ~]# kubectl describe pods nginx-test-655f555749-wwbnp
Name: nginx-test-655f555749-wwbnp
Namespace: default
Priority:
Node: hdss7-.host.com/10.4.7.22
Start Time: Mon, Jun :: +
Labels: app=nginx-test
pod-template-hash=655f555749
Annotations: <none>
Status: Running
IP: 172.7.22.4
Controlled By: ReplicaSet/nginx-test-655f555749
Containers:
mynginx:
Container ID: docker://35a16c22463b9ff05adb69bb2f3665c316c58de657ab01f9c9f63896d0d1c3f0
Image: harbor.fx.com/public/nginx:curl
Image ID: docker-pullable://harbor.fx.com/public/nginx@sha256:75f7e3c69bb839f8400adbca228bce8b7e5bc3e9bce2ff1bb543094b82337e16
Port: /TCP
Host Port: /TCP
State: Running
Started: Mon, Jun :: +
Ready: True
Restart Count:
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-zkqff (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-zkqff:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-zkqff
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m13s default-scheduler Successfully assigned default/nginx-test-655f555749-wwbnp to hdss7-.host.com
Normal Pulling 3m11s kubelet, hdss7-.host.com Pulling image "harbor.fx.com/public/nginx:curl"
Normal Pulled 3m4s kubelet, hdss7-.host.com Successfully pulled image "harbor.fx.com/public/nginx:curl"
Normal Created 3m4s kubelet, hdss7-.host.com Created container mynginx
Normal Started 3m4s kubelet, hdss7-.host.com Started container mynginx
  • 在线修改
[root@hdss7- ~]# kubectl edit svc nginx-ds
service/nginx-ds edited
[root@hdss7- ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> /TCP 5d1h
nginx-ds ClusterIP 192.168.100.178 <none> /TCP 16m

删除资源

[root@hdss7- ~]# kubectl delete -f nginx-ds.yaml
deployment.extensions "nginx-test" deleted
[root@hdss7- ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-ds-7hg9l / Running 4d1h
nginx-ds-tnhsg / Running 4d1h

声明资源管理方法小结:

  • 声明式资源管理方法,依赖于统一资源配置清单文件对资源进行管理。
  • 对资源的管理,是通过事先定义在统一资源配置清单内,再通过陈述式命令应用到K8S集群里。
  • 语法格式:kubectl create/apply/delete -f /path/to/yaml

返回顶部

2.K8S的核心资源管理方法的更多相关文章

  1. 体验 k8s 的核心功能

    快速体验 k8s 的核心功能:应用部署.访问.Scale Up/Down 以及滚动更新 https://yq.aliyun.com/articles/337209?spm=a2c4e.11153940 ...

  2. k8s kubernetes 核心笔记 镜像仓库 项目k8s改造(含最新k8s v1.16.2版本)

    k8s kubernetes 核心笔记 镜像仓库 项目k8s改造 2019/10/24 Chenxin 一 基本资料 一 参考: https://kubernetes.io/ 官网 https://k ...

  3. k8s的核心对象

    一.Deployment的概念 K8S本身并不提供网络的功能,所以需要借助第三方网络插件进行部署K8S中的网络,以打通各个节点中容器的互通. POD,是K8S中的一个逻辑概念,K8S管理的是POD,一 ...

  4. K8S(05)核心插件-ingress(服务暴露)控制器-traefik

    K8S核心插件-ingress(服务暴露)控制器-traefik 1 K8S两种服务暴露方法 前面通过coredns在k8s集群内部做了serviceNAME和serviceIP之间的自动映射,使得不 ...

  5. python核心模块方法

    ********************os模块: os.remove() 删除文件 os.unlink() 删除文件 os.rename() 重命名文件 os.listdir() 列出指定目录下所有 ...

  6. K8S(03)核心插件-Flannel网络插件

    系列文章说明 本系列文章,可以基本算是 老男孩2019年王硕的K8S周末班课程 笔记,根据视频来看本笔记最好,否则有些地方会看不明白 需要视频可以联系我 K8S核心网络插件Flannel 目录 系列文 ...

  7. K8S(04)核心插件-coredns服务

    K8S核心插件-coredns服务 目录 K8S核心插件-coredns服务 1 coredns用途 1.1 为什么需要服务发现 2 coredns的部署 2.1 获取coredns的docker镜像 ...

  8. 一文讲明白K8S各核心架构组件

    目录 一.写在前面 二.K8S为我们提供了怎样的能力 三.架构 3.1.MasterNode 3.2.WorkerNode 四.核心组件 4.1.ApiServer 4.1.1.概述 4.1.2.是集 ...

  9. k8s命令补全方法

    正常安装了k8s后,使用kubect 工具后接的命令不能直接tab补全 命令补全方法: yum -y install bash-completionsource /usr/share/bash-com ...

随机推荐

  1. Java:成员变量、局部变量和静态变量

    梳理一下: 根据定义变量位置的不同,可以将变量分成两大类:成员变量和局部变量. 成员变量(俗称全局变量):在类里定义的变量.又分为实例变量和类变量(也成为静态变量). 实例变量:不以static修饰, ...

  2. 使用jQuery实现Ajax

    jQuery对Ajax操作进行了封装,在jQuery中最底层的方法是$.ajax(), 第二层是load(),  $.get(),  $.post() 第三层是$.getScript(),   $.g ...

  3. 最短路径——Dijkstar算法

    背景:本文是在小甲鱼数据结构教学视频中的代码的基础上,添加详细注释而完成的.该段代码并不完整,仅摘录了核心算法部分,结合自己的思考,谈谈理解. Dijkstar算法理解: Dijkstar算法的核心思 ...

  4. 【算法】单元最短路径之Bellman-Ford算法和SPFA算法

    SPFA是经过对列优化的bellman-Ford算法,因此,在学习SPFA算法之前,先学习下bellman-Ford算法. bellman-Ford算法是一种通过松弛操作计算最短路的算法. 适用条件 ...

  5. Linux (三) 基础命令 上

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一 . 背景知识 1. Linux系统中一切皆文件 在Linux系统中任何东西都是以文件形式来存储的.这 ...

  6. Java实现 LeetCode 524 通过删除字母匹配到字典里最长单词(又是一道语文题)

    524. 通过删除字母匹配到字典里最长单词 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到.如果答案不止一个,返回长度最长且字典顺序最小的字符 ...

  7. Java实现 LeetCode 493 翻转对

    493. 翻转对 给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对. 你需要返回给定数组中的重要翻转对的数 ...

  8. Java实现 LeetCode 307 区域和检索 - 数组可修改

    307. 区域和检索 - 数组可修改 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. update(i, val) 函数可以通过将下标 ...

  9. java实现日程表

    [编程题] 某保密单位机要人员 A,B,C,D,E 每周需要工作5天,休息两天. 上级要求每个人每周的工作日和休息日必须是固定的,不能在周间变更. 此外,由于工作需要,还有如下要求: 1. 所有人的连 ...

  10. DMR windows 软件x64

    解压缩以后,默认使用串口4的USB热点板,用notepad2软件修改MMDVM.ini的呼号,ID,频率,串口号保存在打开DMR.bat即可,晶体有偏移的运行DMR500.bat https://sh ...