01 基本介绍及Pod基础

发布于:2025-05-21 ⋅ 阅读:(24) ⋅ 点赞:(0)

01 查看各种资源

01-1 查看K8s集群的内置资源

[root@master01 ~]# 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

image-20250520131331412

图1 查看K8s内置资源

01-2 查看具体的资源

# 使用全程查看资源
[root@master01 ~]# kubectl get componentstatuses 
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE                         ERROR
controller-manager   Healthy   ok                              
scheduler            Healthy   ok                              
etcd-0               Healthy   {"health":"true","reason":""}

image-20250520131532774

图2 使用全称查看资源
# 也可以使用简称查看资源
# 查看简称可以使用kubectl api-resources命令查看,里面的SHORTNAMES是简称
[root@master01 ~]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE                         ERROR
controller-manager   Healthy   ok                              
scheduler            Healthy   ok                              
etcd-0               Healthy   {"health":"true","reason":""}

image-20250520131640204

图3 使用简称查看资源

02 资源清单

02-1 资源清单结构

# apiVersion: 
	声明资源API的版本号。
# kind:
	声明资源的类型。
# metadata:
	声明资源你的元数据信息。这个元数据信息包括但不限于名称,标签,名称空间,资源注解,资源的创建时间等。
# spec:
	声明资源的期望状态,用户希望资源如何运行,比如Pod资源我们要考虑启动容器名称,容器使用的镜像,启动命令,暴露端口,环境变量,资源限制等。
# status:
	资源的状态,一般情况下,不需要维护该字段,是K8S集群内部自行维护。
# 举例,查看node01节点的node资源的资源清单
# 里面就是这五种结构
[root@master01 ~]# kubectl get nodes node01 -o yaml
apiVersion: v1
kind: Node
metadata:
  annotations:
    flannel.alpha.coreos.com/backend-data: '{"VNI":1,"VtepMAC":"ba:64:c7:34:04:3d"}'
    flannel.alpha.coreos.com/backend-type: vxlan
    flannel.alpha.coreos.com/kube-subnet-manager: "true"
    flannel.alpha.coreos.com/public-ip: 172.2.25.52
    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
    node.alpha.kubernetes.io/ttl: "0"
    volumes.kubernetes.io/controller-managed-attach-detach: "true"
  creationTimestamp: "2025-05-20T01:18:22Z"
  labels:
    beta.kubernetes.io/arch: amd64
    beta.kubernetes.io/os: linux
    kubernetes.io/arch: amd64
    kubernetes.io/hostname: node01
    kubernetes.io/os: linux
  name: node01
  resourceVersion: "16552"
  uid: 28bd4f86-dbf2-48e0-ba4b-8219dc2e14dd
spec:
  podCIDR: 172.22.2.0/24
  podCIDRs:
  - 172.22.2.0/24
status:
  addresses:
  - address: 172.2.25.52
    type: InternalIP
  - address: node01
    type: Hostname
  allocatable:
    cpu: "2"
    ephemeral-storage: "139085221249"
    hugepages-1Gi: "0"
    hugepages-2Mi: "0"
    memory: 3758896Ki
    pods: "110"
  capacity:
    cpu: "2"
    ephemeral-storage: 150917124Ki
    hugepages-1Gi: "0"
    hugepages-2Mi: "0"
    memory: 3861296Ki
    pods: "110"
  conditions:
  - lastHeartbeatTime: "2025-05-20T01:19:47Z"
    lastTransitionTime: "2025-05-20T01:19:47Z"
    message: Flannel is running on this node
    reason: FlannelIsUp
    status: "False"
    type: NetworkUnavailable
  - lastHeartbeatTime: "2025-05-20T05:24:08Z"
    lastTransitionTime: "2025-05-20T01:18:22Z"
    message: kubelet has sufficient memory available
    reason: KubeletHasSufficientMemory
    status: "False"
    type: MemoryPressure
  - lastHeartbeatTime: "2025-05-20T05:24:08Z"
    lastTransitionTime: "2025-05-20T01:18:22Z"
    message: kubelet has no disk pressure
    reason: KubeletHasNoDiskPressure
    status: "False"
    type: DiskPressure
  - lastHeartbeatTime: "2025-05-20T05:24:08Z"
    lastTransitionTime: "2025-05-20T01:18:22Z"
    message: kubelet has sufficient PID available
    reason: KubeletHasSufficientPID
    status: "False"
    type: PIDPressure
  - lastHeartbeatTime: "2025-05-20T05:24:08Z"
    lastTransitionTime: "2025-05-20T01:19:54Z"
    message: kubelet is posting ready status
    reason: KubeletReady
    status: "True"
    type: Ready
  daemonEndpoints:
    kubeletEndpoint:
      Port: 10250
  images:
  - names:
    - harbor.likexy.com/likexy/registry.aliyuncs.com/google_containers/kube-proxy@sha256:6eebf5374c8529ec87d29cc9348cc29989f42a2fc0ef1d7a31a6f3f45ba6e185
    - harbor.likexy.com/likexy/registry.aliyuncs.com/google_containers/kube-proxy:v1.23.17
    - registry.aliyuncs.com/google_containers/kube-proxy:v1.23.17
    sizeBytes: 110840983
  - names:
    - harbor.likexy.com/likexy/flannel/flannel@sha256:f112dc48764d57eea8b4b04dc590779f04f77136e9f4b7e4eeefd32c6b547f73
    - flannel/flannel:v0.24.3
    - harbor.likexy.com/likexy/flannel/flannel:v0.24.3
    sizeBytes: 78604653
  - names:
    - harbor.likexy.com/likexy/flannel/flannel@sha256:5a39542e1f7746f523e459e440514614964db215b454d12ea7d713e0fa50a26f
    - flannel/flannel:v0.25.6
    - harbor.likexy.com/likexy/flannel/flannel:v0.25.6
    sizeBytes: 75116393
  - names:
    - harbor.likexy.com/likexy/registry.aliyuncs.com/google_containers/coredns@sha256:8916c89e1538ea3941b58847e448a2c6d940c01b8e716b20423d2d8b189d3972
    - harbor.likexy.com/likexy/registry.aliyuncs.com/google_containers/coredns:v1.8.6
    - registry.aliyuncs.com/google_containers/coredns:v1.8.6
    sizeBytes: 46829283
  - names:
    - harbor.likexy.com/likexy/flannel/flannel-cni-plugin@sha256:a2e795c6d83e6db30637cad3cf7482eee41c0e2d1a58e53239b82c2442730001
    - flannel/flannel-cni-plugin:v1.5.1-flannel2
    - harbor.likexy.com/likexy/flannel/flannel-cni-plugin:v1.5.1-flannel2
    sizeBytes: 10632098
  - names:
    - harbor.likexy.com/likexy/flannel/flannel-cni-plugin@sha256:4163363bea19dee4ec4200b5bb9a20fa7dc460403d37800eac0a201f59faac5e
    - flannel/flannel-cni-plugin:v1.4.0-flannel1
    - harbor.likexy.com/likexy/flannel/flannel-cni-plugin:v1.4.0-flannel1
    sizeBytes: 9868473
  - names:
    - harbor.likexy.com/likexy/registry.aliyuncs.com/google_containers/pause@sha256:74bf6fc6be13c4ec53a86a5acf9fdbc6787b176db0693659ad6ac89f115e182c
    - harbor.likexy.com/likexy/registry.aliyuncs.com/google_containers/pause:3.6
    - registry.aliyuncs.com/google_containers/pause:3.6
    sizeBytes: 682696
  nodeInfo:
    architecture: amd64
    bootID: fae0a0e3-037b-4a9c-a574-afd1914e5cd8
    containerRuntimeVersion: docker://20.10.24
    kernelVersion: 3.10.0-1160.el7.x86_64
    kubeProxyVersion: v1.23.17
    kubeletVersion: v1.23.17
    machineID: 6fdc18189448460d883a97826be9e74d
    operatingSystem: linux
    osImage: CentOS Linux 7 (Core)
    systemUUID: 1CC24D56-C740-5429-D505-DCA720499290

02-2 K8s资源的增删改查

02-2.1 创建资源

# 也可以使用 kubectl create -f 01-pod.yaml 
[root@master01 yaml]# kubectl apply -f 01-pod.yaml 
pod/likexy created

image-20250520141010272

图4 根据yaml文件创建资源

02-2.2 删除资源

[root@master01 yaml]# kubectl delete -f 01-pod.yaml 
pod "likexy" deleted

image-20250520141246171

图5 根据yaml文件删除
# 使用命令删除
[root@master01 yaml]# vim 02-network.yaml 
[root@master01 yaml]# kubectl delete pod likeixy 

image-20250520162110974

图6 使用命令删除

02-2.3 修改资源

# 1.在生产环境中,一般修改的场景是比较少的,有些字段在运行中甚至不支持修改的,如果想要修改通常做法是删除后修改并重新创建。
# 2.如果你真的想要修改也是可以的,只不过仅有部分字段支持修改,后面会说明。
# 注意,在Yaml文件中修改内容之后创建资源不会删除原来的资源,而是会重新创建一个Pod

image-20250520152851448

图7 修改Yaml后重新创建资源

02-2.4 查看资源

[root@master01 yaml]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
likexy    1/1     Running   0          12m
likexy1   1/1     Running   0          11m
[root@master01 yaml]# kubectl get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
likexy    1/1     Running   0          12m   172.22.4.3   node03   <none>           <none>
likexy1   1/1     Running   0          11m   172.22.2.3   node01   <none>           <none>

image-20250520153212965

图8 查看Pod资源和其详细信息

image-20250520153344987

图9 使用Curl测试,可以测试出内容

03 K8s使用宿主机网络

image-20250520160058354

图10 创建Pod,网络选用宿主机网络

image-20250520160223328

图11 Curl宿主机IP是能够成功访问的

网站公告

今日签到

点亮在社区的每一天
去签到