k8s入门-命令行实现
Posted guardwhy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s入门-命令行实现相关的知识,希望对你有一定的参考价值。
1-什么是NameSpace?
- 俗称命名空间,可以单纯的认为
namespaces
是kubernetes集群中的虚拟化集群。在一个Kubernetes集群中可以拥有多个命名空间,它们在逻辑上彼此隔离。 可以为你提供组织,安全甚至性能方面的帮助! - Namespace是对一组资源和对象的抽象集合,比如可以用来将系统内部的对象划分为不同的项目组或用户组。常见的pods, services, replication controllers和deployments等都是属于某一个namespace的(默认是default),而node, persistentVolumes等则不属于任何namespace。
大多数的Kubernetes中的集群默认会有一个叫default的namespace
。然而实际上存在4个namespace
。
- default:资源默认被创建于default命名空间。
- kube-system:kubernetes系统组件使用。
- kube-node-lease: kubernetes集群节点租约状态。
- kube-public:公共资源使用,但实际上现在并不常用。
这个默认(default)的namespace并没什么特别,但是不能删除它。这很适合刚刚开始使用kubernetes和一些小的产品系统。但不建议应用于大型生产系统。因为这种复杂系统中,团队会非常容易意外地或者无意识地重写或者中断其他服务service。相反,创建多个命名空间来把service(服务)分割成更容易管理的块。
(default)的namespace基本作用:
多租户情况下,实现资源隔离。
属于逻辑隔离,属于管理边界,不属于网络边界。
可以针对每个namespace做资源配额。
1.1 查看命名空间
1、具体命令
## 查看命名空间
kubectl get namespace
## 查看所有命名空间的pod资源
kubectl get pod --all-namespaces
kubectl get pod -A
## 简写命令(查看命名空间)
kubectl get ns
查询结果!!!
## 1、查看命名空间
[root@k8s-master01 data]# kubectl get namespace
NAME STATUS AGE
default Active 169m
kube-node-lease Active 169m
kube-public Active 169m
kube-system Active 169m
## 2、查看所有命名空间的pod资源
[root@k8s-master01 data]# kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-6b94766748-pn4l8 1/1 Running 0 78m
kube-system calico-node-4m62k 1/1 Running 0 78m
kube-system calico-node-7bnqj 1/1 Running 0 78m
kube-system calico-node-krh64 1/1 Running 0 78m
kube-system calico-node-zwvv9 1/1 Running 0 78m
kube-system coredns-6955765f44-nwzg6 1/1 Running 0 169m
kube-system coredns-6955765f44-rdwnc 1/1 Running 0 169m
kube-system etcd-k8s-master01 1/1 Running 1 169m
kube-system kube-apiserver-k8s-master01 1/1 Running 1 169m
kube-system kube-controller-manager-k8s-master01 1/1 Running 1 169m
kube-system kube-proxy-4vdxm 1/1 Running 1 159m
kube-system kube-proxy-5nlqn 1/1 Running 1 154m
kube-system kube-proxy-npzs2 1/1 Running 1 169m
kube-system kube-proxy-z47rt 1/1 Running 1 154m
kube-system kube-scheduler-k8s-master01 1/1 Running 1 169m
## 3、查看命名空间
[root@k8s-master01 data]# kubectl get ns
NAME STATUS AGE
default Active 169m
kube-node-lease Active 169m
kube-public Active 169m
kube-system Active 169m
[root@k8s-master01 data]#
3、命名空间说明
[root@k8s-master01 data]# kubectl get namespace
NAME STATUS AGE
default Active 169m # 用户创建的pod默认在此命名空间
kube-node-lease Active 169m # 所有用户均可以访问,包括未认证用户
kube-public Active 169m # kubernetes集群节点租约状态
kube-system Active 169m # kubernetes集群在使用
1.2 NameSpace相关操作
1、创建NameSpace
kubectl create namespace guardwhy01
-- 简写命令
kubectl create ns guardwhy02
2、删除NameSpace
kubectl delete namespace guardwhy01
-- 简写命令
kubectl delete ns guardwhy02
3、执行结果!!!
[root@k8s-master01 data]# kubectl get namespace
NAME STATUS AGE
default Active 3h7m
kube-node-lease Active 3h7m
kube-public Active 3h7m
kube-system Active 3h7m
[root@k8s-master01 data]# kubectl create namespace guardwhy01
namespace/guardwhy01 created
[root@k8s-master01 data]# kubectl create ns guardwhy02
namespace/guardwhy02 created
[root@k8s-master01 data]# kubectl get namespace
NAME STATUS AGE
default Active 3h8m
guardwhy01 Active 36s
guardwhy02 Active 5s
kube-node-lease Active 3h8m
kube-public Active 3h8m
kube-system Active 3h8m
[root@k8s-master01 data]# kubectl delete namespace guardwhy01
namespace "guardwhy01" deleted
[root@k8s-master01 data]# kubectl delete ns guardwhy02
namespace "guardwhy02" deleted
[root@k8s-master01 data]# kubectl get namespace
NAME STATUS AGE
default Active 3h12m
kube-node-lease Active 3h12m
kube-public Active 3h12m
kube-system Active 3h12m
[root@k8s-master01 data]#
2- 什么是pod?
Pod是kubernetes集群能够调度的最小单元,Pod是容器的封装 。在Kubernetes集群中,Pod是所有业务类型的基础,也是K8S管理的最小单位级,它是一个或多个容器的组合。这些容器共享存储、网络和命名空间,以及如何运行的规范。在Pod中,所有容器都被同一安排和调度,并运行在共享的上下文中。对于具体应用而言,Pod是它们的逻辑主机,Pod包含业务相关的多个应用容器。
2.1 Pod的基本特点
网络:每一个Pod都会被指派一个唯一的Ip地址,在Pod中的每一个容器共享网络命名空间,包括Ip地址和网络端口。在同一个Pod中的容器可以和localhost进行互相通信。当Pod中的容器需要与Pod外的实体进行通信时,则需要通过端口等共享的网络资源。
存储:Pod能够被指定共享存储卷的集合,在Pod中所有的容器能够访问共享存储卷,允许这些容器共享数据。存储卷也允许在一个Pod持久化数据,以防止其中的容器需要被重启。
2.2 Pod的工作方式
1、K8s一般不直接创建Pod,而是通过控制器和模版配置来管理和调度。
2、Pod重启:在Pod中的容器可能会由于异常等原因导致其终止退出,Kubernetes提供了重启策略以重启容器。重启策略对同一个Pod的所有容器起作用,容器的重启由Node上的kubelet执行。Pod支持三种重启策略,在配置文件中通过restartPolicy
字段设置重启策略。
Always:只要退出就会重启。
OnFailure:只有在失败退出(exit code不等于0)时,才会重启。
Never:只要退出,就不再重启
注意,这里的重启是指在Pod的宿主Node上进行本地重启,而不是调度到其它Node上。
3、资源限制
Kubernetes通过cgroups限制容器的CPU和内存等计算资源,包括requests(请求,调度器保证调度到资源充足的Node上)和limits(上限)。
2.3 Pod基本操作
1、查看Pod,执行以下命令
-- 1、查看default命名空间下的pods
kubectl get pods
-- 2、查看kube-system命名空间下的pods
kubectl get pods -n kube-system
-- 3、查看所有命名空间下的pods
kubectl get pod --all-namespaces
kubectl get pod -A
2、创建Pod
下载tomcat相关的镜像
[root@k8s-master01 data]# docker pull tomcat:9.0.37-jdk8-openjdk-slim
[root@k8s-master01 data]# docker pull tomcat:9.0.20-jre8-alpine
[root@k8s-master01 data]# docker pull tomcat:9.0.37-jdk8
[root@k8s-master01 data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0.37-jdk8-openjdk-slim d60b68827676 9 months ago 305MB
tomcat 9.0.37-jdk8 9c7be7b021c3 9 months ago 531MB
calico/node v3.14.2 780a7bc34ed2 11 months ago 262MB
calico/pod2daemon-flexvol v3.14.2 9dfa8f25b51c 11 months ago 22.8MB
calico/cni v3.14.2 e6189009f081 11 months ago 119MB
calico/kube-controllers v3.14.2 4815e4106d26 11 months ago 52.8MB
k8s.gcr.io/kube-proxy v1.17.5 e13db435247d 14 months ago 116MB
k8s.gcr.io/kube-apiserver v1.17.5 f640481f6db3 14 months ago 171MB
k8s.gcr.io/kube-controller-manager v1.17.5 fe3d691efbf3 14 months ago 161MB
k8s.gcr.io/kube-scheduler v1.17.5 f648efaff966 14 months ago 94.4MB
k8s.gcr.io/coredns 1.6.5 70f311871ae1 20 months ago 41.6MB
k8s.gcr.io/etcd 3.4.3-0 303ce5db0e90 20 months ago 288MB
tomcat 9.0.20-jre8-alpine 387f9d021d3a 2 years ago 108MB
k8s.gcr.io/pause 3.1 da86e6ba6ca1 3 years ago 742kB
[root@k8s-master01 data]#
3、运行Pod
## 运行pod
[root@k8s-master01 data]# kubectl run tomcat-test --image=tomcat:9.0.20-jre8-alpine --port=8080
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/tomcat-test created
## 列出所有的pod
[root@k8s-master01 data]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-test-76c794f897-xgjt2 1/1 Running 0 86s
## 列出所有的pod,包括附加信息
[root@k8s-master01 data]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat-test-76c794f897-xgjt2 1/1 Running 0 103s 10.81.58.193 k8s-node02 <none> <none>
[root@k8s-master01 data]#
使用pod的IP访问容器
curl 10.81.58.193:8080
注意:无法使用Pod直接访问外网报错,因为在K8S集群中Pod是最小的单元!!!
4、Pod的删除和添加
deployment作用:作为控制器管理pod,是一个控制器控制资源的。
#1、获得运行的pod
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat-test-76c794f897-5tg9f 1/1 Running 1 28m
## 2、获取deployment控制器
[root@k8s-master01 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
tomcat-test 1/1 1 1 75m
# 3、获取所有的pods
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-test-76c794f897-5tg9f 1/1 Running 1 29m
# 4、删除pod
[root@k8s-master01 ~]# kubectl delete pod tomcat-test-76c794f897-5tg9f
pod "tomcat-test-76c794f897-5tg9f" deleted
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat-test-76c794f897-qzb26 1/1 Running 0 62s
# 5、每个pod都有不同的Ip
[root@k8s-master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat-test-76c794f897-qzb26 1/1 Running 0 98s 10.81.58.194 k8s-node02 <none> <none>
[root@k8s-master01 ~]#
删除deployment
# 1、获取deployment控制器
[root@k8s-master01 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
tomcat-test 1/1 1 1 113m
# 2、删除控制器
[root@k8s-master01 ~]# kubectl delete deployment tomcat-test
deployment.apps "tomcat-test" deleted
# 3、删除成功!!!
[root@k8s-master01 ~]# kubectl get pod
No resources found in default namespace.
# 4、重新添加新的pod
[root@k8s-master01 ~]# kubectl run tomcat9-test --image=tomcat:9.0.20-jre8-alpine --port=8080
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/tomcat9-test created
# 5、获取pod和控制器
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-nq6qt 1/1 Running 0 6s
[root@k8s-master01 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
tomcat9-test 1/1 1 1 14s
[root@k8s-master01 ~]#
5、扩容操作
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 21h
## 1、扩容5个副本
[root@k8s-master01 ~]# kubectl scale --replicas=5 deployment tomcat9-test
deployment.apps/tomcat9-test scaled
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 77s
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 77s
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 21h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 77s
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 77s
## 2、获取每个pod都有不同的Ip和Node节点
[root@k8s-master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 94s 10.81.58.195 k8s-node02 <none> <none>
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 94s 10.81.58.196 k8s-node02 <none> <none>
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 21h 10.81.85.197 k8s-node01 <none> <none>
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 94s 10.81.135.129 k8s-node03 <none> <none>
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 94s 10.81.85.198 k8s-node01 <none> <none>
[root@k8s-master01 ~]#
6、创建服务SVC
## 1、创建服务
[root@k8s-master01 ~]# kubectl expose deployment tomcat9-test \\
> --name=tomcat9-svc \\
> --port=8888 \\
> --target-port=8080 \\
> --protocol=TCP \\
> --type=NodePort
service/tomcat9-svc exposed
## 2、获取服务svc
[root@k8s-master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 29h
tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 69s
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 29h
tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 7m6s
## 3、服务的详细信息
[root@k8s-master01 ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 29h <none>
tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 9m59s run=tomcat9-test
[root@k8s-master01 ~]#
访问服务端口
curl 10.1.215.214:8888
访问集群外端口
http://8.134.122.211:32502
3- kubectl常用命令
3.1 基本语法
kubectl [command] [TYPE] [NAME] [flags]
1、command
:对一个或多个资源执行的操作,例如 create
、get
、describe
、delete
等。
2、TYPE
:指定资源类型,资源类型不区分大小写,可以指定单数、复数或缩写形式。
-- 以下命令输出相同的结果
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 66m
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 66m
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 22h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 66m
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 66m
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 66m
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 66m
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 22h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 66m
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 66m
[root@k8s-master01 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 67m
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 67m
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 22h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 67m
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 67m
[root@k8s-master01 ~]#
3、NAME
:指定资源的名称,名称区分大小写,如果省略名称则显示所有资源的详细信息。
kubectl get pods
在对多个资源执行操作时,可以按类型和名称指定每个资源,或指定一个或多个文件。
4、flags
:指定可选的参数,比如可以使用 -s
或者-server
参数指定 Kubernetes API 服务器的地址和端口。
3.2 get常用命令
kubectl get -
列出一个或多个资源。
# 1、查看集群状态信息
[root@k8s-master01 ~]# kubectl cluster-info
Kubernetes master is running at https://172.21.252.3:6443
KubeDNS is running at https://172.21.252.3:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
# 2、查看集群状态
[root@k8s-master01 ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
# 3、查看集群节点信息
[root@k8s-master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master01 Ready master 29h v1.17.5
k8s-node01 Ready <none> 29h v1.17.5
k8s-node02 Ready <none> 29h v1.17.5
k8s-node03 Ready <none> 29h v1.17.5
# 4、查看集群命名空间
[root@k8s-master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 29h
kube-node-lease Active 29h
kube-public Active 29h
kube-system Active 29h
# 5、查看服务的命名空间
[root@k8s-master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 31h
tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 158m
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 31h
tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 158m
[root@k8s-master01 ~]#
# 6、查看指定命名空间的服务
[root@k8s-master01 ~]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.1.0.10 <none> 53/UDP,53/TCP,9153/TCP 29h
# 7、以纯文本输出格式列出所有pod
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 77m
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 77m
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 22h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 77m
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 77m
# 8、以纯文本输出格式列出所有pod,并包含附加信息(如节点名)。
[root@k8s-master01 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 77m 10.81.58.195 k8s-node02 <none> <none>
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 77m 10.81.58.196 k8s-node02 <none> <none>
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 22h 10.81.85.197 k8s-node01 <none> <none>
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 77m 10.81.135.129 k8s-node03 <none> <none>
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 77m 10.81.85.198 k8s-node01 <none> <none>
[root@k8s-master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 30h
tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 55m
# 9、以纯文本输出格式列出所有副本控制器和服务。
[root@k8s-master01 ~]# kubectl get rc,svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 31h
service/tomcat9-svc NodePort 10.1.215.214 <none> 8888:32502/TCP 154m
# 10、列出在节点k8s-node01上运行的所有 pod
[root@k8s-master01 ~]# kubectl get pods --field-selector=spec.nodeName=k8s-node01
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 24h
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 172m
[root@k8s-master01 ~]#
3.3 describe常用命令
kubectl describe -
显示一个或多个资源的详细状态,默认情况下包括未初始化的资源。
# 1、显示名称为k8s-node02的节点的详细信息
[root@k8s-master01 ~]# kubectl describe nodes k8s-node02
Name: k8s-node02
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=k8s-node02
kubernetes.io/os=linux
Annotations: kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
node.alpha.kubernetes.io/ttl: 0
projectcalico.org/IPv4Address: 172.21.252.5/20
projectcalico.org/IPv4IPIPTunnelAddr: 10.81.58.192
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Sun, 04 Jul 2021 14:52:05 +0800
.........
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 250m (12%) 0 (0%)
memory 0 (0%) 0 (0%)
ephemeral-storage 0 (0%) 0 (0%)
Events: <none>
# 2、获取所有的pod
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 0 3h12m
tomcat9-test-569b5bf455-mccnc 1/1 Running 0 3h12m
tomcat9-test-569b5bf455-nq6qt 1/1 Running 2 24h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 0 3h12m
tomcat9-test-569b5bf455-sn2s8 1/1 Running 0 3h12m
# 3、获取pod tomcat9-test-569b5bf455-fltdd的详细信息!!
[root@k8s-master01 ~]# kubectl describe pod tomcat9-test-569b5bf455-fltdd
Name: tomcat9-test-569b5bf455-fltdd
Namespace: default
Priority: 0
Node: k8s-node02/172.21.252.5
Start Time: Mon, 05 Jul 2021 19:17:56 +0800
Labels: pod-template-hash=569b5bf455
run=tomcat9-test
Annotations: cni.projectcalico.org/podIP: 10.81.58.195/32
cni.projectcalico.org/podIPs: 10.81.58.195/32
Status: Running
IP: 10.81.58.195
..........
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
[root@k8s-master01 ~]#
3.4 delete常用命令
kubectl delete -
从文件、stdin 或指定标签选择器、名称、资源选择器或资源中删除资源。
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-fltdd 1/1 Running 1 14h
tomcat9-test-569b5bf455-mccnc 1/1 Running 1 14h
tomcat9-test-569b5bf455-nq6qt 1/1 Running 3 35h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 1 14h
tomcat9-test-569b5bf455-sn2s8 1/1 Running 1 14h
## 1、删除单个pod
[root@k8s-master01 ~]# kubectl delete pod tomcat9-test-569b5bf455-fltdd
pod "tomcat9-test-569b5bf455-fltdd" deleted
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-mccnc 1/1 Running 1 14h
tomcat9-test-569b5bf455-mlmv8 1/1 Running 0 2m27s
tomcat9-test-569b5bf455-nq6qt 1/1 Running 3 35h
tomcat9-test-569b5bf455-nxhrm 1/1 Running 1 14h
tomcat9-test-569b5bf455-sn2s8 1/1 Running 1 14h
# 2、删除所有pod,包括未初始化的pod
[root@k8s-master01 ~]# kubectl delete pod --all
pod "tomcat9-test-569b5bf455-mccnc" deleted
pod "tomcat9-test-569b5bf455-mlmv8" deleted
pod "tomcat9-test-569b5bf455-nq6qt" deleted
pod "tomcat9-test-569b5bf455-nxhrm" deleted
pod "tomcat9-test-569b5bf455-sn2s8" deleted
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-82cps 1/1 Running 0 75s
tomcat9-test-569b5bf455-fl8zr 1/1 Running 0 75s
tomcat9-test-569b5bf455-ndp67 1/1 Running 0 75s
tomcat9-test-569b5bf455-ndssv 1/1 Running 0 75s
tomcat9-test-569b5bf455-ndxhf 1/1 Running 0 75s
[root@k8s-master01 ~]#
3.5 其他命令
1、进入容器命令
kubectl exec -
对 pod 中的容器执行命令,与docker的exec命令非常类似。
[root@k8s-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-82cps 1/1 Running 0 75s
tomcat9-test-569b5bf455-fl8zr 1/1 Running 0 75s
tomcat9-test-569b5bf455-ndp67 1/1 Running 0 75s
tomcat9-test-569b5bf455-ndssv 1/1 Running 0 75s
tomcat9-test-569b5bf455-ndxhf 1/1 Running 0 75s
[root@k8s-master01 ~]# kubectl exec -it tomcat9-test-569b5bf455-82cps sh
/usr/local/tomcat # ls
BUILDING.txt README.md conf native-jni-lib
CONTRIBUTING.md RELEASE-NOTES include temp
LICENSE RUNNING.txt lib webapps
NOTICE bin logs work
/usr/local/tomcat # exit
[root@k8s-master01 ~]#
2、打印 Pod 中容器的日志
# 从 pod <pod-name> 开始流式传输日志。
kubectl logs -f <pod-name>
3、格式化输出
将pod信息格式化输出到一个yaml文件
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
tomcat9-test-569b5bf455-82cps 1/1 Running 0 66m
tomcat9-test-569b5bf455-fl8zr 1/1 Running 0 66m
tomcat9-test-569b5bf455-ndp67 1/1 Running 0 66m
tomcat9-test-569b5bf455-ndssv 1/1 Running 0 66m
tomcat9-test-569b5bf455-ndxhf 1/1 Running 0 66m
[root@k8s-master01 ~]# kubectl get pod tomcat9-test-569b5bf455-ndxhf -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
cni.projectcalico.org/podIP: 10.81.85.202/32
cni.projectcalico.org/podIPs: 10.81.85.202/32
creationTimestamp: "2021-07-06T01:48:47Z"
generateName: tomcat9-test-569b5bf455-
...........
containerStatuses:
- containerID: docker://00b5db960a98cd0263c1e9ff5fc02bbafe5ffb0805d587a2b01ae4a83d74e3a8
image: tomcat:9.0.20-jre8-alpine
imageID: docker-pullable://tomcat@sha256:17accf0afeeecce0310d363490cd60a788aa4630ab9c9c802231d6fbd4bb2375
lastState: {}
name: tomcat9-test
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2021-07-06T01:48:48Z"
hostIP: 172.21.252.4
phase: Running
podIP: 10.81.85.202
podIPs:
- ip: 10.81.85.202
qosClass: BestEffort
startTime: "2021-07-06T01:48:47Z"
[root@k8s-master01 ~]#
以上是关于k8s入门-命令行实现的主要内容,如果未能解决你的问题,请参考以下文章
sql [SQL查询片段]用于在命令行或通过R和其他工具使用SQL的快速代码段#tags:sql,R,text processing,命令li