一、常用的创建和管理资源
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}')