Kubernetes资源详解

华子目录

  • 1.Kubernetes中的资源
    • 1.1资源管理介绍
    • 1.2资源管理方式
      • 1.2.1命令式对象管理
      • 1.2.2`kubectl`常见`command`命令
      • 1.2.3资源类型
      • 1.2.4常用资源类型
  • `基本命令`示例
  • `运行和调试`命令示例
  • `高级命令`示例
    • 总结
  • `其他命令`示例

1.Kubernetes中的资源

1.1资源管理介绍

在这里插入图片描述

  • kubernetes中,所有的内容都抽象为资源,用户需要通过操作资源来管理kubernetes
  • kubernetes 本质上就是一个集群系统,用户可以在集群部署各种服务
  • 所谓的部署服务,其实就是在kubernetes集群中运行一个个的容器,并将指定的程序跑在容器
  • kubernetes最小管理单元pod不是容器只能容器放在pod
  • kubernetes一般也不会直接管理pod,而是通过pod控制器来管理pod
  • pod容器服务访问是由kubernetes提供的service资源来实现的。pod中的容器不能直接被访问,需要通过service微服务端口进行暴露,最终我们通过微服务来访问pod中的容器
  • pod中程序的数据需要持久化是由kubernetes提供的各种存储系统来实现的

1.2资源管理方式

  • 命令式对象管理:直接使用命令去操作kubernetes资源
[root@k8s-master ~]# kubectl run nginx-pod --image=nginx:latest --port=80#nginx-pod是pod名
  • 命令式对象配置:通过命令配置配置文件去操作kubernetes资源
    • 不能对yaml中的内容做更新k8s不允许对create创建的yaml文件进行修改
[root@k8s-master ~]# kubectl  create/patch  -f  nginx-pod.yml
  • 声明式对象配置:通过apply命令和配置文件去操作kubernetes资源
    • 可以对yaml中的内容做更新(修改完yaml文件的内容后,可以使用apply对其进行应用
[root@k8s-master ~]# kubectl  apply  -f  nginx-pod.yml
类型适用环境优点缺点
命令式对象管理测试简单只能操作活动对象,无法审计,跟踪
命令式对象配置开发可以审计,跟踪项目大时,配置文件多,操作麻烦
声明式对象配置开发支持目录操作意外情况下难以调试

1.2.1命令式对象管理

  • kubectlkubernetes集群的命令行工具,通过它能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署
  • kubectl命令的语法如下:
[root@k8s-master ~]# kubectl  [command]  [type]  [name]  [flags]
  • command:指定要对资源执行的操作,例如creategetdelete
  • type:指定资源类型,比如deploymentpodservice
  • name:指定资源名称名称大小写敏感
  • flags:指定额外可选参数

查看所有pod

[root@k8s-master ~]# kubectl get pods 

查看某一个pod

[root@k8s-master ~]# kubectl get pods pod名

查看某个pod,以yaml格式显示

[root@k8s-master ~]# kubectl get pods pod名 -o yaml

创建两个名为webserver1webserver2pod

  • 其中myapp是一个nginx服务
[root@k8s-master ~]# kubectl run webserver1 --image myapp:v1
pod/webserver1 created
[root@k8s-master ~]# kubectl run webserver2 --image myapp:v2
pod/webserver2 created[root@k8s-master ~]# kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
webserver1   1/1     Running   0          8m27s
webserver2   1/1     Running   0          6m37s[root@k8s-master ~]# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP           NODE            NOMINATED NODE   READINESS GATES
webserver1   1/1     Running   0          2m    10.244.1.2   k8s-node1.org   <none>           <none>
webserver2   1/1     Running   0          10s   10.244.2.6   k8s-node2.org   <none>           <none>[root@k8s-master ~]# kubectl get pods -o name
pod/webserver1
pod/webserver2

1.2.2kubectl常见command命令

命令分类命令翻译命令作用
基本命令create创建创建一个资源
edit编辑编辑一个资源
get获取获取一个资源
patch补丁更新更新一个资源
delete删除删除一个资源
explain解释展示资源文档
运行和调试run运行集群运行一个指定的镜像
expose暴露暴露资源为service
describe描述显示资源内部信息
logs日志输出容器pod中的日志
attach缠绕进入运行中的容器
exec执行执行容器中的一个命令
cp复制pod内外复制文件
rollout首次展示管理资源的发布
scale规模扩(缩)容pod的数量
autoscale自动调整自动调整pod数量
高级命令apply应用通过文件资源进行配置
label标签更新资源上的标签
其他命令cluster-info集群信息显示集群信息
version版本显示当前serverclient版本

1.2.3资源类型

kubernetes所有的内容都抽象为资源

  • 查看所有资源类型
[root@k8s-master ~]# kubectl api-resources
NAME                                SHORTNAMES   APIVERSION                        NAMESPACED   KIND
bindings                                         v1                                true         Binding
componentstatuses                   cs           v1                                false        ComponentStatus
configmaps                          cm           v1                                true         ConfigMap
endpoints                           ep           v1                                true         Endpoints
events                              ev           v1                                true         Event
limitranges                         limits       v1                                true         LimitRange
namespaces                          ns           v1                                false        Namespace
nodes                               no           v1                                false        Node
persistentvolumeclaims              pvc          v1                                true         PersistentVolumeClaim
persistentvolumes                   pv           v1                                false        PersistentVolume
pods                                po           v1                                true         Pod
podtemplates                                     v1                                true         PodTemplate
replicationcontrollers              rc           v1                                true         ReplicationController
resourcequotas                      quota        v1                                true         ResourceQuota
secrets                                          v1                                true         Secret
serviceaccounts                     sa           v1                                true         ServiceAccount
services                            svc          v1                                true         Service
mutatingwebhookconfigurations                    admissionregistration.k8s.io/v1   false        MutatingWebhookConfiguration
validatingadmissionpolicies                      admissionregistration.k8s.io/v1   false        ValidatingAdmissionPolicy
validatingadmissionpolicybindings                admissionregistration.k8s.io/v1   false        ValidatingAdmissionPolicyBinding
validatingwebhookconfigurations                  admissionregistration.k8s.io/v1   false        ValidatingWebhookConfiguration
customresourcedefinitions           crd,crds     apiextensions.k8s.io/v1           false        CustomResourceDefinition
apiservices                                      apiregistration.k8s.io/v1         false        APIService
controllerrevisions                              apps/v1                           true         ControllerRevision
daemonsets                          ds           apps/v1                           true         DaemonSet
deployments                         deploy       apps/v1                           true         Deployment
replicasets                         rs           apps/v1                           true         ReplicaSet
statefulsets                        sts          apps/v1                           true         StatefulSet
selfsubjectreviews                               authentication.k8s.io/v1          false        SelfSubjectReview
tokenreviews                                     authentication.k8s.io/v1          false        TokenReview
localsubjectaccessreviews                        authorization.k8s.io/v1           true         LocalSubjectAccessReview
selfsubjectaccessreviews                         authorization.k8s.io/v1           false        SelfSubjectAccessReview
selfsubjectrulesreviews                          authorization.k8s.io/v1           false        SelfSubjectRulesReview
subjectaccessreviews                             authorization.k8s.io/v1           false        SubjectAccessReview
horizontalpodautoscalers            hpa          autoscaling/v2                    true         HorizontalPodAutoscaler
cronjobs                            cj           batch/v1                          true         CronJob
jobs                                             batch/v1                          true         Job
certificatesigningrequests          csr          certificates.k8s.io/v1            false        CertificateSigningRequest
leases                                           coordination.k8s.io/v1            true         Lease
endpointslices                                   discovery.k8s.io/v1               true         EndpointSlice
events                              ev           events.k8s.io/v1                  true         Event
flowschemas                                      flowcontrol.apiserver.k8s.io/v1   false        FlowSchema
prioritylevelconfigurations                      flowcontrol.apiserver.k8s.io/v1   false        PriorityLevelConfiguration
ingressclasses                                   networking.k8s.io/v1              false        IngressClass
ingresses                           ing          networking.k8s.io/v1              true         Ingress
networkpolicies                     netpol       networking.k8s.io/v1              true         NetworkPolicy
runtimeclasses                                   node.k8s.io/v1                    false        RuntimeClass
poddisruptionbudgets                pdb          policy/v1                         true         PodDisruptionBudget
clusterrolebindings                              rbac.authorization.k8s.io/v1      false        ClusterRoleBinding
clusterroles                                     rbac.authorization.k8s.io/v1      false        ClusterRole
rolebindings                                     rbac.authorization.k8s.io/v1      true         RoleBinding
roles                                            rbac.authorization.k8s.io/v1      true         Role
priorityclasses                     pc           scheduling.k8s.io/v1              false        PriorityClass
csidrivers                                       storage.k8s.io/v1                 false        CSIDriver
csinodes                                         storage.k8s.io/v1                 false        CSINode
csistoragecapacities                             storage.k8s.io/v1                 true         CSIStorageCapacity
storageclasses                      sc           storage.k8s.io/v1                 false        StorageClass
volumeattachments                                storage.k8s.io/v1                 false        VolumeAttachment

1.2.4常用资源类型

资源分类资源名称缩写资源作用
集群级别资源nodesno集群组成部分
namespacesns隔离pod
pod资源podspo装载容器
pod资源控制器replicationcontrollersrc控制pod资源
replicasetsrs控制pod资源
deploymentsdeploy控制pod资源
daemonsetsds控制pod资源
jobs控制pod资源
cronjobscj控制pod资源
horizontalpodautoscalershpa控制pod资源
statefulsetssts控制pod资源
服务发现资源servicessvc统一pod对外接口
ingressing统一pod对外接口
存储资源volumeattachments存储
persistentvolumespv存储
persistentvolumeclaimspvc存储
配置资源configmapscm配置
secrets配置

基本命令示例

kubectl的详细说明地址:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

  • 显示集群版本
[root@k8s-master ~]# kubectl version
Client Version: v1.30.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.30.0
  • 显示集群信息
[root@k8s-master ~]# kubectl cluster-info
Kubernetes control plane is running at https://172.25.254.100:6443
CoreDNS is running at https://172.25.254.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
  • 创建一个名为webclusterdeployment控制器,控制器中pod数量为2
#先删除之前的pod
[root@k8s-master ~]# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP           NODE            NOMINATE
webserver1   1/1     Running   0          16h   10.244.1.2   k8s-node1.org   <none>
webserver2   1/1     Running   0          16h   10.244.2.6   k8s-node2.org   <none>[root@k8s-master ~]# kubectl delete pods webserver1 --force
[root@k8s-master ~]# kubectl delete pods webserver2 --force[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
#创建一个名为`webcluster`的`deployment`控制器,控制器中`pod`数量为`2`
[root@k8s-master ~]# kubectl create deployment webcluster --image nginx --replicas 2
deployment.apps/webcluster created[root@k8s-master ~]# kubectl get pods -o name
pod/webcluster-7c584f774b-9b67l
pod/webcluster-7c584f774b-d8xws[root@k8s-master ~]# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP           NODE            NOMINATED NODE   READINESS GATES
webcluster-7c584f774b-9b67l   1/1     Running   0          79s   10.244.2.7   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-d8xws   1/1     Running   0          79s   10.244.1.3   k8s-node1.org   <none>           <none>#查看deployment控制器
[root@k8s-master ~]# kubectl get deployments.apps
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
webcluster   2/2     2            2           50s
  • 查看资源帮助explain
#等级式查看
[root@k8s-master ~]# kubectl explain deployment
GROUP:      apps
KIND:       Deployment
VERSION:    v1DESCRIPTION:Deployment enables declarative updates for Pods and ReplicaSets.FIELDS:apiVersion    <string>APIVersion defines the versioned schema of this representation of an object.Servers should convert recognized schemas to the latest internal value, andmay reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind  <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata      <ObjectMeta>Standard object's metadata. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec  <DeploymentSpec>Specification of the desired behavior of the Deployment.status        <DeploymentStatus>Most recently observed status of the Deployment.[root@k8s-master ~]# kubectl explain deployment.metadata[root@k8s-master ~]# kubectl explain deployment.spec
  • 编辑名为webclusterdeployment控制器,将pod数量改为3
#编辑名为webcluster的deployment控制器
[root@k8s-master ~]# kubectl edit deployments.apps webcluster
#会进入编辑状态
apiVersion: apps/v1
kind: Deployment
metadata:annotations:deployment.kubernetes.io/revision: "1"creationTimestamp: "2024-10-05T03:47:39Z"generation: 1labels:app: webclustername: webclusternamespace: defaultresourceVersion: "27001"uid: a8bf27c5-c6bb-46d4-b4f7-4accfbb60e71
spec:progressDeadlineSeconds: 600replicas: 3     #将原来的2个pod改为3个pod
......
......
......
:wq
  • 发现改完立即生效
[root@k8s-master ~]# kubectl get deployments.apps webcluster
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
webcluster   3/3     3            3           18m[root@k8s-master ~]# kubectl get pods -o name
pod/webcluster-7c584f774b-9b67l
pod/webcluster-7c584f774b-d8xws
pod/webcluster-7c584f774b-r48fd[root@k8s-master ~]# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE    IP           NODE            NOMINATED NODE   READINESS GATES
webcluster-7c584f774b-9b67l   1/1     Running   0          20m    10.244.2.7   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-d8xws   1/1     Running   0          20m    10.244.1.3   k8s-node1.org   <none>           <none>
webcluster-7c584f774b-r48fd   1/1     Running   0          114s   10.244.2.8   k8s-node2.org   <none>           <none>
  • 利用补丁 patch更改控制器配置(编辑名为webclusterdeployment控制器,将pod数量改为4
[root@k8s-master ~]# kubectl patch deployments.apps webcluster -p '{"spec":{"replicas":4}}'
deployment.apps/webcluster patched[root@k8s-master ~]# kubectl get deployments.apps
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
webcluster   4/4     4            4           26m[root@k8s-master ~]# kubectl get pods -o name
pod/webcluster-7c584f774b-9b67l
pod/webcluster-7c584f774b-d8xws
pod/webcluster-7c584f774b-r48fd
pod/webcluster-7c584f774b-swst6[root@k8s-master ~]# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE    IP           NODE            NOMINATED NODE   READINESS GATES
webcluster-7c584f774b-9b67l   1/1     Running   0          27m    10.244.2.7   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-d8xws   1/1     Running   0          27m    10.244.1.3   k8s-node1.org   <none>           <none>
webcluster-7c584f774b-r48fd   1/1     Running   0          9m7s   10.244.2.8   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-swst6   1/1     Running   0          92s    10.244.1.4   k8s-node1.org   <none>           <none>
  • 控制器中删除pod

控制器删除一个pod后,k8s会根据数量再开一个pod

[root@k8s-master ~]# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE    IP           NODE            NOMINATED NODE   READINESS GATES
webcluster-7c584f774b-9b67l   1/1     Running   0          27m    10.244.2.7   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-d8xws   1/1     Running   0          27m    10.244.1.3   k8s-node1.org   <none>           <none>
webcluster-7c584f774b-r48fd   1/1     Running   0          9m7s   10.244.2.8   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-swst6   1/1     Running   0          92s    10.244.1.4   k8s-node1.org   <none>           <none>
[root@k8s-master ~]# kubectl delete pods webcluster-7c584f774b-9b67l
pod "webcluster-7c584f774b-9b67l" deleted
[root@k8s-master ~]# kubectl get pods -o wide
NAME                          READY   STATUS    RESTARTS   AGE     IP           NODE            NOMINATED NODE   READINESS GATES
webcluster-7c584f774b-d8xws   1/1     Running   0          31m     10.244.1.3   k8s-node1.org   <none>           <none>
webcluster-7c584f774b-r48fd   1/1     Running   0          13m     10.244.2.8   k8s-node2.org   <none>           <none>
webcluster-7c584f774b-swst6   1/1     Running   0          5m25s   10.244.1.4   k8s-node1.org   <none>           <none>
webcluster-7c584f774b-tx55p   1/1     Running   0          5s      10.244.2.9   k8s-node2.org   <none>           <none>
  • 直接删除控制器控制器控制的所有pod都会被删除
[root@k8s-master ~]# kubectl delete deployments.apps webcluster
deployment.apps "webcluster" deleted[root@k8s-master ~]# kubectl get deployments.apps
No resources found in default namespace.[root@k8s-master ~]# kubectl get pods -o wide
No resources found in default namespace.

运行和调试命令示例

  • 首先要保证一个纯净的实验环境
#发现没有pod
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
  • 运行一个名为testpodpod,该pod不属于任何控制器
[root@k8s-master ~]# kubectl run testpod --image nginx
pod/testpod created[root@k8s-master ~]# kubectl get pods -o name
pod/testpod[root@k8s-master ~]# kubectl get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP            NODE            NOMINATED NODE   READINESS GATES
testpod   1/1     Running   0          9s    10.244.2.10   k8s-node2.org   <none>           <none>
  • 端口暴露
[root@k8s-master ~]# kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    18h#--port指定pod中的端口,--target-port指定容器中的服务端口
[root@k8s-master ~]# kubectl expose pod testpod --port 8080 --target-port 80
service/testpod exposed[root@k8s-master ~]# kubectl get services
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    18h
testpod      ClusterIP   10.99.119.189   <none>        8080/TCP   8s
#访问10.244.2.12
[root@k8s-master ~]# curl 10.244.2.12
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>#访问10.99.119.189:8080
[root@k8s-master ~]# curl 10.99.119.189:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
  • 查看运行中pod详细信息
[root@k8s-master ~]# kubectl get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP            NODE            NOMINATED NODE   READINESS GATES
testpod   1/1     Running   0          10m   10.244.2.12   k8s-node2.org   <none>           <none>[root@k8s-master ~]# kubectl describe pods testpod
Name:             testpod
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node2.org/172.25.254.20
Start Time:       Sat, 05 Oct 2024 00:45:53 -0400
Labels:           run=testpod
Annotations:      <none>
Status:           Running
IP:               10.244.2.12
IPs:IP:  10.244.2.12
Containers:testpod:Container ID:   docker://13361a3a29b394fa0049c28405a76a686a26a7c9a1e1fddbcdc4312f2698156aImage:          nginxImage ID:       docker-pullable://nginx@sha256:127262f8c4c716652d0e7863bba3b8c45bc9214a57d13786c854272102f7c945Port:           <none>Host Port:      <none>State:          RunningStarted:      Sat, 05 Oct 2024 00:45:54 -0400Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-tzfw6 (ro)
Conditions:Type                        StatusPodReadyToStartContainers   TrueInitialized                 TrueReady                       TrueContainersReady             TruePodScheduled                True
Volumes:kube-api-access-tzfw6:Type:                    Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds:  3607ConfigMapName:           kube-root-ca.crtConfigMapOptional:       <nil>DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type    Reason     Age   From               Message----    ------     ----  ----               -------Normal  Scheduled  10m   default-scheduler  Successfully assigned default/testpod to k8s-node2.orgNormal  Pulling    10m   kubelet            Pulling image "nginx"Normal  Pulled     10m   kubelet            Successfully pulled image "nginx" in 281ms (281ms including waiting). Image size: 187694648 bytes.Normal  Created    10m   kubelet            Created container testpodNormal  Started    10m   kubelet            Started container testpod
  • 查看资源日志
[root@k8s-master ~]# kubectl logs
daemonsets/              pods/                    services/
deployments/             replicasets/             statefulsets/
jobs/                    replicationcontrollers/  testpod
[root@k8s-master ~]# kubectl logs pods/testpod
......
......
......
  • 运行交互pod
[root@k8s-master ~]# kubectl run -it testpod1 --image busybox
If you don't see a command prompt, try pressing enter.
/ #
/ #
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # exit      #退出交互式,不停止pod[root@k8s-master ~]# kubectl get pods -o wide
NAME       READY   STATUS    RESTARTS      AGE   IP            NODE            NOMINATED NODE   READINESS GATES
testpod    1/1     Running   0             20m   10.244.2.12   k8s-node2.org   <none>           <none>
testpod1   1/1     Running   1 (24s ago)   36s   10.244.1.6    k8s-node1.org   <none>           <none>#再次进入容器
[root@k8s-master ~]# kubectl attach -it  pods/testpod1
If you don't see a command prompt, try pressing enter.
/ #
/ #
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # exit 或  ctrl+p+q退出不停止pod[root@k8s-master ~]# kubectl get pods -o wide
NAME       READY   STATUS    RESTARTS       AGE     IP            NODE            NOMINATED NODE   READINESS GATES
testpod    1/1     Running   0              24m     10.244.2.12   k8s-node2.org   <none>           <none>
testpod1   1/1     Running   2 (3m9s ago)   4m30s   10.244.1.6    k8s-node1.org   <none>           <none>
  • 在已经运行的pod中运行容器中的指定命令
[root@k8s-master ~]# kubectl exec -it pods/testpod1 ifconfig
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
eth0      Link encap:Ethernet  HWaddr 12:C1:23:80:08:BAinet addr:10.244.1.6  Bcast:10.244.1.255  Mask:255.255.255.0inet6 addr: fe80::10c1:23ff:fe80:8ba/64 Scope:LinkUP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1RX packets:15 errors:0 dropped:0 overruns:0 frame:0TX packets:13 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0RX bytes:2148 (2.0 KiB)  TX bytes:962 (962.0 B)lo        Link encap:Local Loopbackinet addr:127.0.0.1  Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING  MTU:65536  Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
  • 复制master宿主机文件到pod中的容器中
[root@k8s-master ~]# kubectl cp anaconda-ks.cfg   testpod1:/[root@k8s-master ~]# kubectl  exec  -it  pods/testpod1  /bin/sh
/ # ls
anaconda-ks.cfg  home             root             var
bin              lib              sys
dev              lib64            tmp
etc              proc             usr
/ # touch file1
/ # ls
anaconda-ks.cfg  file1            proc             usr
bin              home             root             var
dev              lib              sys
etc              lib64            tmp
/ # echo hello world > file1
/ # cat file1
hello world111
  • 复制pod容器中的文件master宿主机中
[root@k8s-master ~]# kubectl cp  testpod1:/file1   /mnt/file
tar: removing leading '/' from member names
[root@k8s-master ~]# cd /mnt/
[root@k8s-master mnt]# ls
file
[root@k8s-master mnt]# cat file
hello world111

高级命令示例

  • 利用命令生成yaml格式文件
[root@k8s-master ~]# mkdir huazi
[root@k8s-master ~]# cd huazi/
[root@k8s-master huazi]#

首先要确保一个纯净的实验环境

[root@k8s-master huazi]# kubectl get pods
NAME       READY   STATUS    RESTARTS      AGE
testpod    1/1     Running   0             43m
testpod1   1/1     Running   2 (22m ago)   23m[root@k8s-master huazi]# kubectl delete pods testpod
pod "testpod" deleted
[root@k8s-master huazi]# kubectl delete pods testpod1 --force
pod "testpod1" deleted
#仅尝试,不运行
[root@k8s-master huazi]# kubectl create deployment webserver --image nginx --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: webservername: webserver
spec:replicas: 1selector:matchLabels:app: webserverstrategy: {}template:metadata:creationTimestamp: nulllabels:app: webserverspec:containers:- image: nginxname: nginxresources: {}
status: {}
  • 导入文件
#创建一个控制器文件
[root@k8s-master huazi]# kubectl create deployment webserver --image nginx --dry-run=client -o yaml > webserver.yml
[root@k8s-master huazi]# ls
webserver.yml
#对文件进行简单的修改
[root@k8s-master huazi]# vim  webserver.yml
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: webservername: webserver
spec:replicas: 2    #修改为2个podselector:matchLabels:app: webservertemplate:metadata:labels:app: webserverspec:containers:- image: nginxname: nginx
  • 应用文件
#这个文件是一个控制器文件
[root@k8s-master huazi]# kubectl apply -f webserver.yml
deployment.apps/webserver created[root@k8s-master huazi]# kubectl get deployments.apps
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
webserver   2/2     2            2           27s[root@k8s-master huazi]# kubectl get pods -o name
pod/webserver-7bc769cd4c-mg9kl
pod/webserver-7bc769cd4c-nl7q2[root@k8s-master huazi]# kubectl get pods -o wide
NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
webserver-7bc769cd4c-mg9kl   1/1     Running   0          2m10s   10.244.2.13   k8s-node2.org   <none>           <none>
webserver-7bc769cd4c-nl7q2   1/1     Running   0          2m10s   10.244.1.7    k8s-node1.org   <none>           <none>
  • 删除控制器文件
[root@k8s-master huazi]# kubectl delete -f webserver.yml
deployment.apps "webserver" deleted[root@k8s-master huazi]# kubectl get deployments.apps
No resources found in default namespace.[root@k8s-master huazi]# kubectl get pods -o wide
No resources found in default namespace.
  • 管理资源标签
#这个是没有控制的pod
[root@k8s-master huazi]# kubectl run webserver --image nginx
pod/webserver created[root@k8s-master huazi]# kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP            NODE            NOMINATED NODE   READINESS GATES
webserver   1/1     Running   0          12s   10.244.2.15   k8s-node2.org   <none>           <none>[root@k8s-master huazi]# kubectl get pods --show-labels
NAME        READY   STATUS    RESTARTS   AGE   LABELS
webserver   1/1     Running   0          50s   run=webserver
  • 更改标签
[root@k8s-master huazi]# kubectl label pods webserver run=web --overwrite
pod/webserver labeled[root@k8s-master huazi]# kubectl get pods --show-labels
NAME        READY   STATUS    RESTARTS   AGE     LABELS
webserver   1/1     Running   0          3m48s   run=web
  • 添加标签
[root@k8s-master huazi]# kubectl label pods webserver app=web1
pod/webserver labeled[root@k8s-master huazi]# kubectl get pods --show-labels
NAME        READY   STATUS    RESTARTS   AGE     LABELS
webserver   1/1     Running   0          5m33s   app=web1,run=web
  • 删除标签
[root@k8s-master huazi]# kubectl label pods webserver app-
pod/webserver unlabeled[root@k8s-master huazi]# kubectl get pods --show-labels
NAME        READY   STATUS    RESTARTS   AGE     LABELS
webserver   1/1     Running   0          6m48s   run=web
  • 创建一个控制器pod
#先删除之前的pod
[root@k8s-master huazi]# kubectl delete pods webserver
pod "webserver" deleted
[root@k8s-master huazi]# kubectl get pods
No resources found in default namespace.
[root@k8s-master huazi]# kubectl create deployment webserver --image nginx --dry-run=client -o yaml > web-label.yaml
[root@k8s-master huazi]# ls
web-label.yaml  webserver.yml[root@k8s-master huazi]# vim web-label.yaml
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: webservername: webserver
spec:replicas: 2   #修改为2个podselector:matchLabels:app: webservertemplate:metadata:labels:app: webserverspec:containers:- image: nginxname: nginx
[root@k8s-master huazi]# kubectl apply -f web-label.yaml
deployment.apps/webserver created[root@k8s-master huazi]# kubectl get deployments.apps
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
webserver   2/2     2            2           7s[root@k8s-master huazi]# kubectl get pods --show-labels
NAME                         READY   STATUS    RESTARTS   AGE   LABELS
webserver-7bc769cd4c-bnklx   1/1     Running   0          44s   app=webserver,pod-template-hash=7bc769cd4c
webserver-7bc769cd4c-cdj8f   1/1     Running   0          44s   app=webserver,pod-template-hash=7bc769cd4c

修改一个控制器pod上的一个标签

[root@k8s-master huazi]# kubectl get pods --show-labels
NAME                         READY   STATUS    RESTARTS   AGE   LABELS
webserver-7bc769cd4c-bnklx   1/1     Running   0          44s   app=webserver,pod-tem                                                                                                       plate-hash=7bc769cd4c
webserver-7bc769cd4c-cdj8f   1/1     Running   0          44s   app=webserver,pod-tem                                                                                                       plate-hash=7bc769cd4c[root@k8s-master huazi]# kubectl label pods webserver-7bc769cd4c-bnklx app=lee --over                                                                                                       write
pod/webserver-7bc769cd4c-bnklx labeled
[root@k8s-master huazi]# kubectl get pods --show-labels                                                                                                                                     NAME                         READY   STATUS    RESTARTS   AGE     LABELS
webserver-7bc769cd4c-bnklx   1/1     Running   0          3m14s   app=lee,pod-templat                                                                                                       e-hash=7bc769cd4c
webserver-7bc769cd4c-cdj8f   1/1     Running   0          3m14s   app=webserver,pod-t                                                                                                       emplate-hash=7bc769cd4c
webserver-7bc769cd4c-cgzcq   1/1     Running   0          3s      app=webserver,pod-t                                                                                                       emplate-hash=7bc769cd4c

我们发现k8s又起了一个新的pod

当我们删除另一个标签后,k8s又起了一个新的pod

[root@k8s-master huazi]# kubectl label pods webserver-7bc769cd4c-cdj8f pod-template-hash-
pod/webserver-7bc769cd4c-cdj8f unlabeled
[root@k8s-master huazi]# kubectl get pods --show-labels
NAME                         READY   STATUS    RESTARTS   AGE     LABELS
webserver-7bc769cd4c-bnklx   1/1     Running   0          5m22s   app=lee,pod-template-hash=7bc769cd4c
webserver-7bc769cd4c-cdj8f   1/1     Running   0          5m22s   app=webserver
webserver-7bc769cd4c-cgzcq   1/1     Running   0          2m11s   app=webserver,pod-template-hash=7bc769cd4c
webserver-7bc769cd4c-zq5mw   1/1     Running   0          5s      app=webserver,pod-template-hash=7bc769cd4c

当我们修改回来原来的标签后,k8s又把之前新的pod删除

[root@k8s-master huazi]# kubectl label pods webserver-7bc769cd4c-bnklx app=webserver --overwrite
pod/webserver-7bc769cd4c-bnklx labeled
[root@k8s-master huazi]# kubectl get pods --show-labels
NAME                         READY   STATUS    RESTARTS   AGE     LABELS
webserver-7bc769cd4c-cdj8f   1/1     Running   0          8m45s   app=webserver
webserver-7bc769cd4c-cgzcq   1/1     Running   0          5m34s   app=webserver,pod-template-hash=7bc769cd4c
webserver-7bc769cd4c-zq5mw   1/1     Running   0          3m28s   app=webserver,pod-template-hash=7bc769cd4c

总结

所以我们发现,k8s是通过标签去记录pod的数量,如果有多个标签,且多个标签必须一致。如果不一致,则k8s会根据pod的数量重新启动相应pod

其他命令示例

[root@k8s-master huazi]# kubectl cluster-info
Kubernetes control plane is running at https://172.25.254.100:6443
CoreDNS is running at https://172.25.254.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@k8s-master huazi]# kubectl version
Client Version: v1.30.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.30.0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1555108.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Nacos理论知识+应用案例+高级特性剖析

一、理论知识 Nacos功能 Nacos常用于注册中心、配置中心 Nacos关键特性 1、服务发现和服务健康监测 nacos作为服务注册中心可用于服务发现,并支持传输层&#xff08;TCP&#xff09;和应用层(HTTP&#xff09;的健康检查&#xff0c;并提供了agent上报和nacos server端主动…

Transformer架构概述(二)

目录 1. Transformer架构概述 1.1 《Attention is All You Need》论文概述 1.2 Transformer的模块组成 1.3 Encoder 和 Decoder 的区别与联系 2. Transformer的并行计算效率相对于RNN的提升 2.1 RNN中的顺序处理问题 2.2 Transformer中的并行化优势 3. Self-Attention机…

Pikachu-PHP反序列化

从后端代码可以看出&#xff0c;拿到序列化后的字符串&#xff0c;直接做反序列化&#xff1b;并且在前端做了展示&#xff1b; 如果虚拟化后的字符串&#xff0c;包含alert 内容&#xff0c;反序列化后&#xff0c;就会弹出窗口 O:1:"S":1:{s:4:"test";s…

OpenJudge | 置换选择排序

总时间限制: 1000ms 内存限制: 65536kB 描述 给定初始整数顺串&#xff0c;以及大小固定并且初始元素已知的二叉最小堆&#xff08;为完全二叉树或类似完全二叉树&#xff0c;且父元素键值总小于等于任何一个子结点的键值&#xff09;&#xff0c;要求利用堆实现置换选择排序&a…

Gralloc图形缓冲的分配过程

广告 首先帮我朋友打个广告 我们一起在运营一个视频号 感兴趣的可以帮忙点击右边这个小铃铛 铃铛 序 其实越往底下走在很多人嘴里就会变得很玄乎&#xff0c;变得不可思议&#xff0c;这里的gralloc就是一个native service&#xff0c;只是分装了一些调用接口&#xff0c;上…

Pikachu-目录遍历

目录遍历&#xff0c;跟不安全文件上传下载有差不多&#xff1b; 访问 jarheads.php 、truman.php 都是通过 get 请求&#xff0c;往title 参数传参&#xff1b; 在后台&#xff0c;可以看到 jarheads.php 、truman.php所在目录&#xff1a; /var/www/html/vul/dir/soup 图片…

传感器模块编程实践(二)W5500 SPI转以太网模块简介及驱动源码

文章目录 一.概要二.W5500芯片介绍W5500通讯协议介绍 三.W5500模块介绍四.W5500模块原理图五.W5500以太网模通讯实验六.CubeMX工程源代码下载七.小结 一.概要 我们介绍过单片机的以太网系统一般是由&#xff1a;单片机MACPHYRJ45。有些单片机比如STM32F407VET6芯片内部自带MAC…

windows下载Redis

1.下载地址 Releases tporadowski/redis GitHub 下载后&#xff0c;将压缩包解压到你的文件夹即可。&#xff08;此时&#xff0c;redis已经完成安装&#xff09; 2.使用 2.1双击redis.server.exe即可启动&#xff08;启动redis服务端&#xff09;&#xff08;或者在当前目…

超声波清洗机什么牌子值得入手?推荐四款入手不亏的眼镜清洗机

在当今这个注重细节完美的时代&#xff0c;超声波清洗机凭借其卓越的清洁效率、深层渗透力及细腻的清洗效果&#xff0c;迅速赢得了家庭与专业场景的青睐。无论是精细的珠宝、眼镜框&#xff0c;还是金属装饰品、电子设备乃至医疗器具&#xff0c;超声波技术都能精准祛除隐秘处…

0110 Redis缓存的更新策略

在很多高并发的场景如秒杀系统&#xff0c;QPS会瞬时暴增&#xff0c;如果采用直接读写数据库&#xff08;如MySQL&#xff09;的方式&#xff0c;很可能会将数据库打垮。因此这种场景需要引入Redis做缓存&#xff0c;应对高并发的访问。但同时也会引入新的风险&#xff0c;最常…

数据结构——List接口

文章目录 一、什么是List&#xff1f;二、常见接口介绍三、List的使用总结 一、什么是List&#xff1f; 在集合框架中&#xff0c;List是一个接口&#xff0c;通过其源码&#xff0c;我们可以清楚看到其继承了Collection。 Collection 也是一个接口&#xff0c;该接口中规范了后…

华为 HCIP-Datacom H12-821 题库 (31)

&#x1f423;博客最下方微信公众号回复题库,领取题库和教学资源 &#x1f424;诚挚欢迎IT交流有兴趣的公众号回复交流群 &#x1f998;公众号会持续更新网络小知识&#x1f63c; 1. 默认情况下&#xff0c;IS-IS Level-1-2 路由器会将 Level-2 区域的明细路由信息发布到Lev…

Python入门--函数

目录 1. 函数介绍 2. 函数的定义 3. 函数的参数 4. 函数的返回值 5. 函数说明文档 6. 函数的嵌套调用 7. 函数的作用域 (1). 局部变量 (2). 全局变量 (3). global关键字 1. 函数介绍 函数&#xff1a;是组织好的&#xff0c;可重复使用的&#xff0c;用来实现特定功能…

YOLO-V7 二元分类器

在评估二元分类器性能时&#xff0c;TP、FP、TN和FN是四个核心指标&#xff0c;它们分别代表真阳性、假阳性、真阴性和假阴性。以下是这些指标的定义、计算方法以及在实际应用中的意义&#xff1a; 定义 TP&#xff08;真阳性&#xff09;&#xff1a;模型正确预测为正类且实…

Yocto - 使用Yocto开发嵌入式Linux系统_06 掌握Bitbake工具

Grasping the BitBake Tool 在上一章中&#xff0c;我们了解了元数据、元数据集合概念以及 conf/layer.conf 的重要性。在本章中&#xff0c;我们将更深入地研究元数据&#xff0c;了解配方如何相互依赖&#xff0c;并了解 BitBake 如何处理依赖关系。 In the previous chapter…

k8s 中微服务之 MetailLB 搭配 ingress-nginx 实现七层负载

目录 1 MetailLB 搭建 1.1 MetalLB 的作用和原理 1.2 MetalLB功能 1.3 部署 MetalLB 1.3.1 创建deployment控制器和创建一个服务 1.3.2 下载MealLB清单文件 1.3.3 使用 docker 对镜像进行拉取 1.3.4 将镜像上传至私人仓库 1.3.5 将官方仓库地址修改为本地私人地址 1.3.6 运行清…

【路径规划】多机器人路径规划

摘要 多机器人路径规划在现代自动化、仓储管理及智能交通系统中有着广泛的应用。本文提出了一种基于A*算法的多机器人路径规划方法&#xff0c;旨在解决多机器人在同一环境中的路径冲突问题。通过采用启发式搜索和路径优化策略&#xff0c;机器人能够在保持避障的前提下实现最…

Middleware---RocketMQ

RocketMQ是一个开源的分布式消息中间件。它是一种 低延迟、高可用、高可靠、高并发 的消息队列系统&#xff0c;用于在分布式系统中进行异步通信。 RocketMQ架构模型 Producer Group&#xff1a;消息生产者组&#xff0c;负责发送消息。 Broker&#xff1a;存储消息的服务节…

java:pdfbox 3.0 去除扫描版PDF中文本水印

官网下载 https://pdfbox.apache.org/download.html下载 pdfbox-app-3.0.3.jar cd D:\pdfbox 运行 java -jar pdfbox-app-3.0.3.jar java -jar pdfbox-app-3.0.3.jar Usage: pdfbox [COMMAND] [OPTIONS] Commands:debug Analyzes and inspects the internal structu…

Java第二阶段---10方法带参---第三节 面向对象和面向过程的区别

1.案例 2.代码实现 面向过程 import java.util.Scanner;/*** Procedure Oriented Programming 面向过程编程*/public class POP {public static void main(String[] args) {Scanner sc new Scanner(System.in);while(true){System.out.println("1.学生成绩管理");…