陈述式 | 命令行:kubectl命令行工具 优点:90%以上的场景都可以满足,对增,删,查比较方便,对改不是很友好 缺点:命令比较冗长,复杂,难记 |
声明式 | k8s当中的yaml文件来实现资源管理---声明式 |
Kubernetes kubectl 命令表 _ Kubernetes(K8S)中文文档_Kubernetes中文社区
所有的kubectl的命令行
?kubectl命令详解
可以查看,部署,查看pod的情况(详细的信息,日志,发布和回滚) |
查看版本
[root@master01 ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.15", GitCommit:"8f1e5bf0b9729a899b8df86249b56e2c74aebc55", GitTreeState:"clean", BuildDate:"2022-01-19T17:27:39Z", GoVersion:"go1.15.15", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.15", GitCommit:"8f1e5bf0b9729a899b8df86249b56e2c74aebc55", GitTreeState:"clean", BuildDate:"2022-01-19T17:23:01Z", GoVersion:"go1.15.15", Compiler:"gc", Platform:"linux/amd64"}
?查看k8s所有资源对象的简写
[root@master01 ~]# kubectl api-resources
?查看k8s的集群信息
[root@master01 ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.233.81:6443
KubeDNS is running at https://192.168.233.81:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
?自动补齐命令
[root@master01 ~]# source <(kubectl completion bash)
可以写在脚本里,也可以在命令行执行
查看master节点的状态
[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"}
这个是简写
[root@master01 ~]# kubectl get componentstatuses
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
?查看默认命名空间pod的信息
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d6h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 12m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d6h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 12m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 12m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d6h
pods也可以,一样的效果
?查看命名空间有哪些(所有)
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 3d7h
kube-node-lease Active 3d7h
kube-public Active 3d7h
kube-system Active 3d7h
查看当前集群所有的命名空间
default就是默认的命名空间
?
查看指定命名空间的pod
[root@master01 ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-7f89b7bc75-9ldmr 1/1 Terminating 2 3d7h
coredns-7f89b7bc75-fm5fm 1/1 Running 0 15m
coredns-7f89b7bc75-g4frq 1/1 Running 0 15m
coredns-7f89b7bc75-vz54f 1/1 Terminating 2 3d7h
etcd-master01 1/1 Running 4 3d7h
kube-apiserver-master01 1/1 Running 4 3d7h
kube-controller-manager-master01 1/1 Running 5 3d7h
kube-flannel-ds-5mcgj 1/1 Running 5 3d7h
kube-flannel-ds-lh8g5 1/1 Running 2 3d7h
kube-flannel-ds-rfxh4 1/1 Running 2 3d7h
kube-proxy-j9pgx 1/1 Running 2 3d7h
kube-proxy-qwdd2 1/1 Running 4 3d7h
kube-proxy-v259v 1/1 Running 2 3d7h
kube-scheduler-master01 1/1 Running 4 3d7h
查看指定命名空间内的pod需要加 -n命名
查看pod的详细部署情况(查看默认命名空间内pod的详细信息)
[root@master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d6h 10.244.1.9 node01 <none> <none>
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 17m <none> <none> <none> <none>
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d6h 10.244.2.9 node02 <none> <none>
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 17m <none> <none> <none> <none>
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 17m <none> <none> <none> <none>
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d6h 10.244.1.8 node01 <none> <none>
查看指定命名空间的详细信息
[root@master01 ~]# kubectl get pod -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-7f89b7bc75-9ldmr 1/1 Terminating 2 3d7h 10.244.1.10 node01 <none> <none>
coredns-7f89b7bc75-fm5fm 1/1 Running 0 22m 10.244.0.2 master01 <none> <none>
coredns-7f89b7bc75-g4frq 1/1 Running 0 22m 10.244.0.3 master01 <none> <none>
coredns-7f89b7bc75-vz54f 1/1 Terminating 2 3d7h 10.244.2.8 node02 <none> <none>
etcd-master01 1/1 Running 4 3d7h 192.168.233.81 master01 <none> <none>
kube-apiserver-master01 1/1 Running 4 3d7h 192.168.233.81 master01 <none> <none>
kube-controller-manager-master01 1/1 Running 5 3d7h 192.168.233.81 master01 <none> <none>
kube-flannel-ds-5mcgj 1/1 Running 5 3d7h 192.168.233.81 master01 <none> <none>
kube-flannel-ds-lh8g5 1/1 Running 2 3d7h 192.168.233.83 node02 <none> <none>
查看节点信息和状态
[root@master01 ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master01 Ready control-plane,master 3d8h v1.20.15
node01 NotReady <none> 3d7h v1.20.15
node02 NotReady <none> 3d7h v1.20.15
[root@master01 ~]# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master01 Ready control-plane,master 3d8h v1.20.15 192.168.233.81 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://24.0.7
node01 NotReady <none> 3d7h v1.20.15 192.168.233.82 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://24.0.7
node02 NotReady <none> 3d7h v1.20.15 192.168.233.83 <none> CentOS Linux 7 (Core) 3.10.0-693.el7.x86_64 docker://24.0.7
查看node节点的详细信息
?查看pod的详细信息
[root@master01 ~]# kubectl describe pod myapp-test-5d94dbb4f-9j2fm
加上name名字
?指定查看pod的详细信息
[root@master01 ~]# kubectl get ns
[root@master01 ~]# kubectl get pod -n kube-system
[root@master01 ~]# kubectl describe pod -n kube-system etcd-master01
要-n指定命名空间,不指定就是查看默认的
?查看pod的输出日志
[root@master01 ~]# kubectl logs myapp-test-5d94dbb4f-9j2fm
[root@master01 ~]# kubectl logs -f myapp-test-5d94dbb4f-9j2fm
-f表示动态查看日志
查看指定命名空间的日志输出信息
[root@master01 ~]# kubectl get pod -n kube-system
[root@master01 ~]# kubectl logs -f etcd-master01 -n kube-system
必须要-n指定申明命名空间,否则就是查看默认命名空间
?创建命名空间
[root@master01 ~]# kubectl create ns chen
namespace/chen created
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
chen Active 10s
default Active 3d8h
kube-node-lease Active 3d8h
kube-public Active 3d8h
kube-system Active 3d8h
删除命名空间
[root@master01 ~]# kubectl delete ns chen
namespace "chen" deleted
[root@master01 ~]# kubectl get ns
NAME STATUS AGE
default Active 3d8h
kube-node-lease Active 3d8h
kube-public Active 3d8h
kube-system Active 3d8h
要先声明动作delete删除指定的组件ns命名空间,chen是要删除命名空间的名称
删除pod
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 57m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 58m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 58m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
[root@master01 ~]# kubectl delete pod myapp-test-5d94dbb4f-9j2fm
先声明动作:create , delete ,get , desvribe 指定对象:如 ns,pod,service 指定对象的名称 chen等的名称,如果不是默认命名空间需要 -n 指定命名空间
deployment的创建方式部署pod
特点以及两种方式部署 陈述式部署:命令行 声明式:yaml文件部署 滚动更新:不是一次性的吧所有pod全部部署,而是一个个来,pod的更新时使用,逐步的引入新的pod,逐步的减少旧的pod 自我修复:如果有pod节点发生故障,deployment会自动启动新的pod来进行代替 回滚:如果更新有问题,deployment会提供一个还原点,可以手动还原到未更新前的状态 扩容和缩容:deployment可以随时调整pod的数量,以适应流量的变化 上述的功能需要实现必须要基于deployment创建的服务才可以,绝大多数的pod都是使用deployment创建的 |
[root@master01 ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
myapp-test 0/3 3 0 3d7h
查看默认命名空间使用deployment创建的pod数量
[root@master01 ~]# kubectl get deployments -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
coredns 2/2 2 2 3d8h
指定命名空间查看
daemonset创建方式
daemonset:不能通过命令行创建,只能通过yaml文件中点定义的方式创建 daemonset是后台运行创建,会在每个节点上都创建一个相同方式,相同版本的容器运行pod daemonset一般都是依赖环境和重要组件。一般不会对这些资源进行操作。 |
[root@master01 ~]# kubectl get daemonsets
No resources found in default namespace.
[root@master01 ~]# kubectl get daemonsets -n kube-system
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
kube-flannel-ds 3 3 1 3 1 <none> 3d8h
kube-proxy 3 3 1 3 1 kubernetes.io/os=linux 3d8h
#daemonset不能通过命令行创建,只能通过yaml文件中点定义的方式创建
deployment部署pod
[root@master01 ~]# kubectl create deployment nginx-chen --image=nginx
deployment.apps/nginx-chen created
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 73m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 74m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 74m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
nginx-chen-699bd94c4f-g667s 0/1 Pending 0 24s
[root@master01 ~]# kubectl create deployment nginx-chen --image=nginx --replicas=3
# --replicas=3表示指定运行容器的数量是3,不加默认是1
指定命名空间创建
[root@master01 ~]# kubectl create ns chen
namespace/chen created
[root@master01 ~]# kubectl create deployment nginx-chen --image=nginx -n chen
deployment.apps/nginx-chen created
在指定命名空间创建deployment
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 77m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 77m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 77m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
nginx-chen-699bd94c4f-g667s 0/1 Pending 0 3m38s
[root@master01 ~]# kubectl get pod -n chen
NAME READY STATUS RESTARTS AGE
nginx-chen-699bd94c4f-phbc6 0/1 Pending 0 15s
[root@master01 ~]# kubectl delete pod nginx-chen-699bd94c4f-g667s
pod "nginx-chen-699bd94c4f-g667s" deleted
删除掉
[root@master01 ~]# kubectl delete pod nginx-chen-699bd94c4f-phbc6 -n chen
pod "nginx-chen-699bd94c4f-phbc6" deleted
删除掉指定的命名空间里的pod
如果是基于deployment方式创建的pod,或者是daemonset方式创建的pod,是由控制器创建的pod,使用delete删除pod是删不掉的,相当于重启pod
基于deployment方式创建的pod,一旦删除deployment,基于这个deployment创建的pod都会被删除,慎用
[root@master01 ~]# kubectl run nginx1 --image=nginx
pod/nginx1 created
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 86m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 86m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 86m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
nginx-chen-699bd94c4f-j86vn 0/1 Pending 0 6m27s
nginx1 0/1 Pending 0 5s
[root@master01 ~]# kubectl delete pod nginx1
pod "nginx1" deleted
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 87m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 87m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 87m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
nginx-chen-699bd94c4f-j86vn 0/1 Pending 0 7m10s
不是基于控制器创建,会被直接删除
远程进入节点容器
[root@master01 ~]# kubectl exec -it nginx-chen-699bd94c4f-j86vn bash
docker的exec只能在本机内部使用,不能跨主机,kubectl exec可以跨主机进入容器
[root@master01 ~]# kubectl create deployment nginx --image=nginx -n chen
[root@master01 ~]# kubectl get pod -n chen
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-kkq5r 0/1 Pending 0 14s
nginx-chen-699bd94c4f-bd6kk 0/1 Pending 0 13m
[root@master01 ~]# kubectl exec -it nginx-chen-699bd94c4f-bd6kk bash -n chen
快速的让容器结束
[root@master01 ~]# kubectl delete pod nginx-chen-699bd94c4f-j86vn --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nginx-chen-699bd94c4f-j86vn" force deleted
#--grace-period:过度的存活期。默认是30秒。可以让pod优雅的结束容器内的进程,然后退出pod
#=0:表示立即停止pod。必须要force实现
#主要是用于结束卡在销毁状态的pod
对deployment创建的pod进行扩缩容
[root@master01 ~]# kubectl scale deployment nginx-chen --replicas=3
deployment.apps/nginx-chen scaled
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 102m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 102m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 102m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
nginx-chen-699bd94c4f-g4f6k 0/1 Pending 0 11s
nginx-chen-699bd94c4f-rn2l9 0/1 Pending 0 11s
nginx-chen-699bd94c4f-wd266 0/1 Pending 0 2m1s
扩容
[root@master01 ~]# kubectl scale deployment nginx-chen --replicas=1
deployment.apps/nginx-chen scaled
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-test-5d94dbb4f-9j2fm 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-dv65r 0/1 Pending 0 102m
myapp-test-5d94dbb4f-gdwm6 1/1 Terminating 1 3d7h
myapp-test-5d94dbb4f-hjtjv 0/1 Pending 0 102m
myapp-test-5d94dbb4f-hn9wc 0/1 Pending 0 102m
myapp-test-5d94dbb4f-p9zld 1/1 Terminating 1 3d7h
nginx-chen-699bd94c4f-wd266 0/1 Pending 0 2m22s
缩容
创建pod是并没有指定副本数,后续也可以对它的副本数进行修改,只能是deployment创建的方式才可以,daemonset创建的不行
如何把服务的service进行发布
[root@master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d9h
myapp-test NodePort 10.96.208.213 <none> 80:30096/TCP 3d8h
nginx NodePort 10.96.22.254 <none> 80:30221/TCP 3d8h
[root@master01 ~]# kubectl get svc -n chen
No resources found in chen namespace.
#查看当前命名空间的service的类型
#-n:指定命名空间
[root@master01 ~]# kubectl delete svc nginx
service "nginx" deleted
[root@master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d9h
myapp-test NodePort 10.96.208.213 <none> 80:30096/TCP 3d8h
#删除当前命名空间的service
#-n:指定命名空间
ClusterIP:创建service的默认类型,提供一个集群内部的虚拟IP地址,通过这个虚拟IP可以直接访问pod的资源。无法对外提供访问 |
NodePort:会在每一个node节点上都开放一个相同对的端口。外部可以通过node的本机IP地址+端口,访问pod资源。集群外部访问service资源的一种方式。四层代理方式。 nodeip:nodeport 会随机指派,也可以指定。 端口会从30000-32767的固定范围随机指派一个端口,或者可以指定一个这个范围内的端口 |
[root@master01 ~]# kubectl expose deployment nginx-chen --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@master01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d9h
myapp-test NodePort 10.96.208.213 <none> 80:30096/TCP 3d8h
nginx-service NodePort 10.96.214.169 <none> 80:30127/TCP 13s
#--port=80:声明service集群的端口
#--target-port=80:声明pod内部容器的端口
nginx-service NodePort 10.96.94.66 <none> 80:30386/TCP 9s
#10.96.94.66:集群内部的IP地址,外部不可以访问这个IP地址
#80:对应的是内部的service的端口
#30386:和内部的service的端口做映射
pod内部的容器端口是固定的。
--port是service和容器映射的端口可以自定义
但是--target-port容器内部的端口是固定的不可以自定义
80:30386这里的80是内部容器和service端口做的映射
这里的30386是外部主机和内部的service的端口做的映射
容器内部的端口 --port 先和service做映射。这个端口不能对外访问。如果类型是 NodePort 会在每个node节点上开通一个相同的端口,这个端口就是service和本机端口的映射。所以访问30386就可以访问容器内的服务。
[root@master01 ~]# kubectl edit svc nginx-service
#修改service的默认端口
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2023-12-31T23:49:21Z"
labels:
app: nginx-chen
name: nginx-service
namespace: default
resourceVersion: "45708"
uid: b91ce2b0-8ee8-451d-80df-b2572ba25539
spec:
clusterIP: 10.96.214.169
clusterIPs:
- 10.96.214.169
externalTrafficPolicy: Cluster
ports:
- nodePort: 30127
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-chen
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
pod内部的容器的端口是固定的,--port是service和容器映射的端口,可以是任意
LoadBalancer类型
LoadBalancer:如果service的类型设定为LoadBalancer。地址映射(云平台会提供一个LoadBalancer的地址)这种用法仅用于公有云服务供应商在云平台上设置好的service场景。再通过外部来访问,实现负载均衡访问。(地址需要额外付费) 创建好了service,指定类型为LoadBalancer。 |
ExternalName类型
ExternalName:DNS映射,给service分配一个域名,通过域名来访问后端pod资源。 使用ExternalName的service的类型,不能提供负载均衡,和 LoadBalancer 类型一样必须要设置一个LoadBalancer的地址才可以实现。 |
kubectl edit svc nginx1-server
#进入service内直接修改
四个类型 默认类型只对内访问 NodePort类型我配置过可以对外访问 LoadBalancer类型需要花钱 ExternalName类型需要配置域名 |
项目的生命周期:创建 > 发布 > 更新 > 回滚 > 删除 |
kubectl set image deployment nginx1 nginx=nginx:1.10
#滚动更新服务的版本
回滚
kubectl rollout history deployment nginx1
#查看回滚点信息
#数字的大小决定了距离上次操作的远近。数字越大就是最近的一次操作
kubectl set image deployment nginx1 nginx=nginx:1.15 --record
#滚动更新服务的版本
#--record:可以在回滚点上添加标识
kubectl rollout undo deployment nginx1 --to-revision=1
#指定回滚点还原
kubectl rollout status deployment nginx1
#查看回滚过程
kubectl get pod -w
#动态查看回滚状态
kubectl get all
#查看当前命名空间内部的所有信息。
#-o wide:可以查看到详细信息
#-n:指定命名空间查看