kubetl命令行管理工具
Posted 水木,年華
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kubetl命令行管理工具相关的知识,希望对你有一定的参考价值。
Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。
//帮助信息
[root@master ~]# kubectl --help
查看名称空间资源信息
[root@master ~]# kubectl get pods,svc --all-namespaces -o wide 以长格式显示详细信息
//项目的生命周期,创建–》发布–》更新–》回滚–》删除
//创建 kubectl run命令
[root@master ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3
[root@master ~]# kubectl get all #以短格式显示除核心组件之外的信息
基于pod删除
[root@master ~]# kubectl delete pod nginx-deployment-8859878f8-7tctf -n default
[root@master k8s]# kubectl delete pod nginx-5ff9d6cc77-k5k5x --force --grace-period=0 -n default 强制删除
基于控制器删除
[root@master ~]# kubectl delete deploy/nginx-deployment
//项目周期
1:创建nginx
[root@master ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
查询
[root@master ~]# kubectl get pods,deployment
2:发布nginx service提供负载均衡的功能
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
# --port为pod的端口,--target-port为service的端口,30215为对外服务的端口
//查看资源对象简写
[root@master ~]# kubectl api-resources
//查看关联后端的节点
[root@master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.80.1:6443 15h
nginx-service 10.244.1.3:80,10.244.2.5:80,10.244.2.6:80 9m5s
[root@master ~]# kubectl get ep --all-namespaces #可以查看IP地址,节点信息
NAMESPACE NAME ENDPOINTS AGE
default kubernetes 192.168.80.1:6443 7d1h
default nginx-service 10.244.2.5:80,10.244.2.6:80 6d10h
kube-system kube-controller-manager <none> 7d1h
kube-system kube-dns 10.244.0.6:53,10.244.0.7:53,10.244.0.6:53 + 3 more... 7d1h
kube-system kube-scheduler <none> 7d1h
//查看具体资源的详细信息
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-5ff9d6cc77-k5k5x 1/1 Terminating 0 6d11h
nginx-5ff9d6cc77-p9v2j 1/1 Running 0 6d11h
nginx-5ff9d6cc77-w75cs 1/1 Running 0 46m
nginx-5ff9d6cc77-wnh2g 1/1 Running 0 6d11h
[root@master ~]# kubectl describe pod nginx-5ff9d6cc77-k5k5x
Name: nginx-5ff9d6cc77-k5k5x
Namespace: default
Priority: 0
Node: node1/192.168.80.2
Start Time: Thu, 30 Sep 2021 10:18:29 +0800
Labels: pod-template-hash=5ff9d6cc77
run=nginx
Annotations: <none>
Status: Terminating (lasts 49m)
Termination Grace Period: 30s
IP: 10.244.1.3
Controlled By: ReplicaSet/nginx-5ff9d6cc77
Containers:
nginx:
Container ID: docker://95b86265905ed148f40d749b767dcfbd859b6d1240f2b2388f2500470439f872
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:969419c0b7b0a5f40a4d666ad227360de5874930a2b228a7c11e15dedbc6e092
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 30 Sep 2021 10:18:46 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4mdv8 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady True
PodScheduled True
Volumes:
default-token-4mdv8:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4mdv8
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
查看节点日志
[root@master ~]# kubectl logs -f nginx-5ff9d6cc77-p9v2j -n default
//获取修改模板
[root@master k8s]# kubectl edit pod nginx-deployment-8859878f8-jpxqk -n default
#修改配置文件
[root@master k8s]# kubectl set image deploy/nginx nginx=nginx:1.14
#修改镜像版本
[root@master k8s]# kubectl describe po nginx-7485bf6f9b-x7hq2 -n default
#查看描述性信息
//处于动态监听状态
[root@master k8s]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-7485bf6f9b-x7hq2 1/1 Running 0 12m
4:回滚nginx
[root@master k8s]# kubectl rollout --help
Manage the rollout of a resource.
Valid resource types include:
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history 显示 rollout 历史
pause 标记提供的 resource 为中止状态
restart Restart a resource
resume 继续一个停止的 resource
status 显示 rollout 的状态
undo 撤销上一次的 rollout
Usage:
kubectl rollout SUBCOMMAND [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
//查看历史版本
[root@master k8s]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
//执行回滚
[root@master k8s]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx rolled back
//检查回滚状态
[root@master k8s]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
[root@master k8s]# kubectl describe po nginx-5ff9d6cc77-wgfm4 -n default
//删除nginx
查看deployment
[root@master k8s]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 44m
删除deployment
[root@master k8s]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@master k8s]# kubectl get deploy
No resources found.
[root@master k8s]# kubectl get pods
No resources found.
//删除服务SVC
[root@localhost bin]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
nginx-service NodePort 10.0.0.247 <none> 80:38804/TCP 114m
[root@localhost bin]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@localhost bin]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
//进入pod
[root@master k8s]# kubectl exec -it nginx-deployment-8859878f8-4hjnh bash
root@nginx-deployment-8859878f8-4hjnh:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@nginx-deployment-8859878f8-4hjnh:/# pwd
/
以上是关于kubetl命令行管理工具的主要内容,如果未能解决你的问题,请参考以下文章