0


云原生之kubectl命令详解(二)及pod的相关操作

一、kubectl命令详解

1、查看某个资源的详细信息:kubectl describe pod pod_name -n nameapace

[root@master ~]# kubectl describe pods -n kube-public

                                                        ### 查看指定命名空间中的pod的详细信息                                          
Name:         nginx-w1-7464d66457-psk6h
Namespace:    kube-public
Priority:     0
Node:         node01/192.168.159.11
Start Time:   Wed, 02 Nov 2022 01:40:47 +0800
Labels:       app=nginx-w1
              pod-template-hash=7464d66457
Annotations:  <none>
Status:       Running
IP:           10.150.1.3
IPs:
  IP:           10.150.1.3
Controlled By:  ReplicaSet/nginx-w1-7464d66457
Containers:
  nginx:
    Container ID:   docker://4011df060a9ad777793f061675fc1a784f4effe56f5585d91d5ba3670660c620
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Wed, 02 Nov 2022 01:41:06 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-llzgh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-llzgh:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

Name:         nginx-x1-77484d895f-ddhqt
Namespace:    kube-public
Priority:     0
Node:         node02/192.168.159.13
Start Time:   Thu, 03 Nov 2022 16:16:11 +0800
Labels:       app=nginx-x1
              pod-template-hash=77484d895f
Annotations:  <none>
Status:       Running
IP:           10.150.2.3
IPs:
  IP:           10.150.2.3
Controlled By:  ReplicaSet/nginx-x1-77484d895f
Containers:
  nginx:
    Container ID:   docker://ecc07803682f2d7a51037024354dae0d190841443121ca0a8514ab224179cbf5
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 03 Nov 2022 16:16:36 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-47hxr (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-47hxr:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

2、查看指定命名空间中pod的基本信息:kubectl get pods -n namespace

[root@master ~]# kubectl get pods -n kube-public

NAME                        READY   STATUS    RESTARTS   AGE
nginx-w1-7464d66457-psk6h   1/1     Running   0          43h
nginx-x1-77484d895f-ddhqt   1/1     Running   0          5h12m

3、跨主机登录容器:kubectl exec -it pod_name /bin/bash -n namespace

root@master ~]# kubectl exec -it nginx-x1-77484d895f-ddhqt /bin/bash -n kube-public

[root@master ~]# kubectl exec -it nginx-x1-77484d895f-ddhqt /bin/bash -n kube-public
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-x1-77484d895f-ddhqt:/# 

4、删除pod

4.1、直接删除pod。但是deployment,副本控制器还在,他就会自己再创建一个。

[root@master ~]# kubectl delete pod nginx- -n kube-public
pod "nginx-w1-7464d66457-psk6h" deleted

①、直接删除POD

②、查看,发现与原先pod_name不一样,

4.2、删除pod副本控制器deployment:

[root@master ~]# kubectl delete deployment nginx-w1 -n kube-public

4.3、如果删除过程中出现幺蛾子,Pod无法删除,总是处于terminating状态,则执行强行删除--force --grace-period=0

5、命令行扩容和缩容

5.1、副本集的扩容

①查看原有副本集:1个

②扩容副本集为4

5.2、副本集的缩容

基于上面结果进行实验,设置--replicas=2,指定副本集数量后,他会根据现有的副本集数量和期望值相比较,从而决定时扩容还是缩容

6、删除service

[root@master ~]# kubectl delete svc nginx-server -n default

[root@master ~]# kubectl get svc -n default
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.125.0.1      <none>        443/TCP        2d6h
nginx-server    NodePort    10.125.126.34   <none>        80:30537/TCP   45m
nginx1-server   NodePort    10.125.103.25   <none>        80:31070/TCP   2m41s
[root@master ~]# kubectl delete svc nginx-server -n default
service "nginx-server" deleted

7、查看pod网络状态详情信息和service暴露的端口

[root@master ~]# kubectl get svc,pods -n default

8、查看关联后的节点

[root@master ~]# kubectl get endpoints

[root@master ~]# kubectl get endpoints
NAME             ENDPOINTS                                    AGE
kubernetes       192.168.159.10:6443                          2d6h
nginx1-service   10.150.1.10:80,10.150.1.9:80,10.150.2.9:80   32m

9、查看service的描述信息

[root@master ~]# kubectl describe svc nginx


[root@master ~]# kubectl describe svc nginx
Name:                     nginx1-service
Namespace:                default
Labels:                   app=nginx
Annotations:              <none>
Selector:                 app=nginx
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.125.32.0
IPs:                      10.125.32.0
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  31625/TCP
Endpoints:                10.150.1.10:80,10.150.1.9:80,10.150.2.9:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

10、核心组件查看日志

通过kubeadm方式部署的K8S

kubectl logs -f pod_name -n namespace 或者 journalctl -u kubelet -f

通过二进制部署的K8S

journalctl -u kubelet -f

二、pod的生命周期

pod生命周期是指:pod在从创建到删除过程中,所包含、经历的状态

pod的类型分为2中:

①、自主式:没有控制器管理的就是自助式的。删除时直接删除就行

②、申明式:有控制器管理的Pod。(我们一般使用的类型)

1、创建一个Pod,启动nginx实例,暴露容器80端口,设置副本集为3(创建pod)

namespace为默认

[root@master ~]# kubectl create deployment nginx-ceshi --image=nginx:1.14 --port 80 --replicas=3
deployment.apps/nginx-ceshi created

2、 发布:kubectl expose命令

3、 查看端口

三、 service的type类型

①、clusterIP:提供一个集群内部的虚拟IP以供Pod访问(service默认类型)

②、NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过 NodeIp:NodePort 的方式Kubernetes集群外部的程序可以访问service。每个端口只能是一种服务,端口范围只能是30000-32767

③、LoadBalancer:通过设置LoadBalancer映射到云服务商提供的LoadBalancer地址。这种用法仅用于在公有云服务提供商的云平台上设置service的场景。通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。

④、externalName:将service名称映射到一个DNS域名上,相当于DNS服务的CNAME记录,用于让Pod去访问集群外部的资源,它本身没有绑定任何的资源。

标签: 云原生 docker 容器

本文转载自: https://blog.csdn.net/m0_62948770/article/details/127678600
版权归原作者 忙碌滴比特流 所有, 如有侵权,请联系我们删除。

“云原生之kubectl命令详解(二)及pod的相关操作”的评论:

还没有评论