k8s常用的命令(已分组)

一、常用的创建和管理资源

1.创建资源

创建一个新的 Deployment:kubectl create deployment hello-world --image=gcr.io/google-samples/hello-app:1.0

查看 Deployment 的状态:kubectl get deployments

更新 Deployment:kubectl set image deployment/hello-world hello-world=gcr.io/google-samples/hello-app:v2

回滚 Deployment 到上一个版本:kubectl rollout undo deployment/hello-world --to-revision=1

停止 Deployment:kubectl delete deployment hello-world

查看 Deployment 日志:kubectl logs -f deployment/hello-world

2. 管理 Pod

创建 Pod:kubectl run nginx --replicas=3 --image=nginx

查看 Pod 的状态:kubectl get pods

终止 Pod:kubectl delete pod nginx

查看 Pod 日志:kubectl logs -f nginx

3. 管理 Service

创建 Service:kubectl expose deployment/nginx --port=80 --target-port=80 --type=NodePort

查看 Service 的详情:kubectl describe service nginx

更新 Service:`kubectl patch service/nginx -p '{"spec": {"ports":[{"name":"http","port":80,"targetPort":80}]}'

删除 Service:kubectl delete service nginx

停止 Service:kubectl stop service nginx

4. 管理 Ingress

创建 Ingress 规则:kubectl apply -f examples/guestbook/redis-ingress.yaml

查看 Ingress 规则:kubectl describe ingress nginx

更新 Ingress 规则:kubectl replace --force=true -f examples/guestbook/redis-ingress.yaml

删除 Ingress 规则:kubectl delete ingress nginx‍

二、查看 Kubernetes 集群的状态命令

1.查看集群的状态

查看整个集群的情况:kubectl cluster-info

查看节点状态:kubectl get nodes

查看集群日志:kubectl logs

查看控制器的状态:kubectl get controller

查看 namespace:kubectl get namespaces

查看 service 和 deployment 的状态:kubectl get services,deployments

2.查看节点情况

查看节点详细信息:kubectl describe node

查看节点 IP 地址:kubectl get nodes -o wide

查看节点网络状态:kubectl top node

查看节点上的 Pod:kubectl get pods -A --all-namespaces

3. 查看资源利用率

查看整个集群的资源占用情况:kubectl top nodes

查看单个 Pod 资源占用情况:kubectl top pod

查看资源配额:kubectl describe quota

4. 查看 Service

查看 Service 的情况:kubectl get services

查看 Service 端口映射:kubectl describe services

5. 查看 Pod

查看 Pod 的运行状态:kubectl get pods

查看 Pod 的详细信息:kubectl describe pod

查看 Pod 的日志:kubectl logs‍

三、运行容器应用命令

1.运行容器应用

运行一个新的 Container:kubectl run nginx --replicas=3 --image=nginx

查看 Pod 的状态:kubectl get pods

更新 Container:kubectl set image deployment/nginx new-image:tag

查看 Container 日志:kubectl logs -f nginx

2. Pod 管理

删除 Pod:kubectl delete pod nginx

更改 ReplicationController 或 DaemonSet 的副本数量:kubectl scale rc/rc-name --replicas=n

检查 Pod 是否健康:kubectl describe pod/pod-name

设置滚动升级策略:kubectl rollout status deploy/deployment-name

查看 Pod 执行的任务 kubectl get events

3. Service 监控

访问服务的外部地址:minikube service service-name

查看服务状态:kubectl get services

更改 Service 的类型:kubectl patch svc nginx -p '{"spec":{"type":"LoadBalancer"}'

重置服务的状态:kubectl rolling-update service-name -c new-config

4. Ingress 路由规则

设置 Ingress 规则:kubectl apply -f path/to/rule.yaml

更改 Ingress 规则:kubectl replace -f path/to/rule.yaml

查看 Ingress 规则:kubectl get ingresses

删除 Ingress 规则:kubectl delete ingresses ingress-name‍

四、更新应用程序命令

1.更新镜像版本

要更新应用程序的镜像版本,需要先查看当前的版本,然后删除现有的 Pod 并重新部署新的版本:

kubectl rollout undo deployment/nginx-deployment --to-revision=1

kubectl set image deployment/nginx-deployment nginx=new_image

2.更改资源限制

更改 Pod 上的最大内存和 CPU 数量:

kubectl edit deployment/nginx-deployment‍

五、日志管理和监控

1.获取 Pod 日志

要获取 Pod 日志,请使用 kubectl logs 命令:

kubectl logs deployment/nginx-pod

要连续查看 Pod 日志,可以使用 -f 标志:

kubectl logs -f deployment/nginx-pod

使用 Fluentd 收集日志

在集群中启用 Fluentd 以收集日志:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/fluentd-elasticsearch-stack/master/stackdriver-gcp.yaml

2.要检查 fluentd 是否成功运行,请使用 kubectl describe 命令:

kubectl describe daemonset fluentd-gcp-v2.0.18-logging

要查看采集的日志,请使用 kubectl logs 命令:

`kubectl logs $(kubectl get pods -l app=fluentd-gcp -o jsonpath='{.items[0].metadata.name}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

missterzy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值