飞道的博客

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

381人阅读  评论(0)

目录

一、kubectl命令详解

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

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

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

4、删除pod

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

①、直接删除POD 

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

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

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

5、命令行扩容和缩容

5.1、副本集的扩容

①查看原有副本集:1个 

②扩容副本集为4

5.2、副本集的缩容 

6、删除service

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

8、查看关联后的节点

9、查看service的描述信息

二、pod的生命周期

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

namespace为默认

2、 发布:kubectl expose命令 ​编辑

3、 查看端口

​三、 service的type类型


一、kubectl命令详解

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

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

                                                            ### 查看指定命名空间中的pod的详细信息                                          


  
  1. Name: nginx-w1-7464d66457-psk6h
  2. Namespace: kube-public
  3. Priority: 0
  4. Node: node01/192.168.159.11
  5. Start Time: Wed, 02 Nov 2022 01:40:47 +0800
  6. Labels: app=nginx-w1
  7. pod-template-hash=7464d66457
  8. Annotations: <none>
  9. Status: Running
  10. IP: 10.150.1.3
  11. IPs:
  12. IP: 10.150.1.3
  13. Controlled By: ReplicaSet/nginx-w1-7464d66457
  14. Containers:
  15. nginx:
  16. Container ID: docker://4011df060a9ad777793f061675fc1a784f4effe56f5585d91d5ba3670660c620
  17. Image: nginx
  18. Image ID: docker-pullable://nginx@sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
  19. Port: <none>
  20. Host Port: <none>
  21. State: Running
  22. Started: Wed, 02 Nov 2022 01:41:06 +0800
  23. Ready: True
  24. Restart Count: 0
  25. Environment: <none>
  26. Mounts:
  27. /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-llzgh (ro)
  28. Conditions:
  29. Type Status
  30. Initialized True
  31. Ready True
  32. ContainersReady True
  33. PodScheduled True
  34. Volumes:
  35. kube-api-access-llzgh:
  36. Type: Projected (a volume that contains injected data from multiple sources)
  37. TokenExpirationSeconds: 3607
  38. ConfigMapName: kube-root-ca.crt
  39. ConfigMapOptional: <nil>
  40. DownwardAPI: true
  41. QoS Class: BestEffort
  42. Node-Selectors: <none>
  43. Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
  44. node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
  45. Events: <none>
  46. Name: nginx-x1-77484d895f-ddhqt
  47. Namespace: kube-public
  48. Priority: 0
  49. Node: node02/192.168.159.13
  50. Start Time: Thu, 03 Nov 2022 16:16:11 +0800
  51. Labels: app=nginx-x1
  52. pod-template-hash=77484d895f
  53. Annotations: <none>
  54. Status: Running
  55. IP: 10.150.2.3
  56. IPs:
  57. IP: 10.150.2.3
  58. Controlled By: ReplicaSet/nginx-x1-77484d895f
  59. Containers:
  60. nginx:
  61. Container ID: docker://ecc07803682f2d7a51037024354dae0d190841443121ca0a8514ab224179cbf5
  62. Image: nginx:latest
  63. Image ID: docker-pullable://nginx@sha256:943c25b4b66b332184d5ba6bb18234273551593016c0e0ae906bab111548239f
  64. Port: <none>
  65. Host Port: <none>
  66. State: Running
  67. Started: Thu, 03 Nov 2022 16:16:36 +0800
  68. Ready: True
  69. Restart Count: 0
  70. Environment: <none>
  71. Mounts:
  72. /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-47hxr (ro)
  73. Conditions:
  74. Type Status
  75. Initialized True
  76. Ready True
  77. ContainersReady True
  78. PodScheduled True
  79. Volumes:
  80. kube-api-access-47hxr:
  81. Type: Projected (a volume that contains injected data from multiple sources)
  82. TokenExpirationSeconds: 3607
  83. ConfigMapName: kube-root-ca.crt
  84. ConfigMapOptional: <nil>
  85. DownwardAPI: true
  86. QoS Class: BestEffort
  87. Node-Selectors: <none>
  88. Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
  89. node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
  90. Events: <none>

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

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


  
  1. NAME READY STATUS RESTARTS AGE
  2. nginx-w1-7464d66457-psk6h 1/1 Running 0 43h
  3. 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


  
  1. [root@master ~] # kubectl exec -it nginx-x1-77484d895f-ddhqt /bin/bash -n kube-public
  2. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  3. 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


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

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

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

8、查看关联后的节点

[root@master ~]# kubectl get endpoints


  
  1. [root@master ~] # kubectl get endpoints
  2. NAME ENDPOINTS AGE
  3. kubernetes 192.168.159.10:6443 2d6h
  4. 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


  
  1. [root@master ~] # kubectl describe svc nginx
  2. Name: nginx1-service
  3. Namespace: default
  4. Labels: app=nginx
  5. Annotations: <none>
  6. Selector: app=nginx
  7. Type: NodePort
  8. IP Family Policy: SingleStack
  9. IP Families: IPv4
  10. IP: 10.125.32.0
  11. IPs: 10.125.32.0
  12. Port: < unset> 80/TCP
  13. TargetPort: 80/TCP
  14. NodePort: < unset> 31625/TCP
  15. Endpoints: 10.150.1.10:80,10.150.1.9:80,10.150.2.9:80
  16. Session Affinity: None
  17. External Traffic Policy: Cluster
  18. Events: <none>

二、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去访问集群外部的资源,它本身没有绑定任何的资源。


转载:https://blog.csdn.net/m0_62948770/article/details/127678600
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场