k8s

k8s 常用命令

Posted by shefh on December 21, 2020

命名空间相关命令

# 查看所有的命名空间
kubectl get namespace / kubectl get ns

# 创建新的命名空间
kubectl create namespace fooval


# 删除命名空间
kubectl delete namespaces fooval

# 查看命名空间信息
kubectl describe ns/namespace fooval

pod

# 查看所有命名空间下的pod
kubectl get pod --all-namespaces

# 查看命名空间下pod
kubectl get pods -n fooval



#查看pod详细信息
kubectl get pods -o wide -n fooval

#在fooval命名空间中创建一个pod副本
kubectl run text-nginx --image=nginx --env="EN1=V1" --env="EN2=V2" -n fooval

# 删除fooval命名空间下的pod
kubectl delete pod text-nginx -n fooval


# 查看pod信息
kubectl describe pod test-nginx -n fooval

# 以yaml或者json格式查看pod
kubectl get pod test-nginx -n fooval -o yaml/json

# 显示IP信息和运行的node信息 
kubectl -n kube-system get pods -o wide
# 查看 pod 的事件
kubectl -n default describe pod_name
# 查看日志
kubectl logs -f -n default pod_name

Force delete pod

kubectl delete pods pod_name --grace-period=0 --force


## delete pods on the specified node
kubectl drain vm-0-2-ubuntu --delete-local-data --ignore-daemonsets --force 

## 批量强制删除pod
kubectl get pod -n cattle-system | grep Terminating | awk '{print $1}' | xargs kubectl delete pod -n cattle-system --grace-period=0 --force

deployment

# 创建deployment
kubectl create deployment test-nginx --image=nginx -n fooval

# 查看已经部署了的所有应用,可以看到容器,以及容器所用的镜像,标签等信息
kubectl get deployments/deploy -o wide -n fooval

# 副本数增加到2个
kubectl scale --replicas=2 deployment test-nginx -n fooval

#编辑deployment
kubectl edit deployment test-nginx  -n fooval

services相关

# 查看服务
kubectl get services/svc -n fooval

Images

List images

crictl images

Delete images in batches

crictl images|grep none|awk '{print $3}'|xargs crictl rmi

其他

# 查看集群状态信息
kubectl cluster-info 

# 查看集群状态
kubectl get cs

# 查看集群节点信息
kubectl get nodes 

# 进入容器
kubectl exec -it test-nginx  sh

# 查看容器日志
kubectl logs -f dafen-redis-56d44cc5b-b6tvq --tail=20

资源限制

# 将deployment的test-nginx容器cpu限制为“200m”,将内存设置为“512Mi”
kubectl set resources deployment test-nginx --limits=cpu=200m,memory=512Mi


# 设置test-nginx容器中 Requests和Limits
kubectl set resources deployment test-nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi

# 删除test-nginx中容器的计算资源值
kubectl set resources deployment test-nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0

Alias

alias k=kubectl
complete -o default -F __start_kubectl k

alias k_config_xxx="export KUBECONFIG=/xxx/xx.yaml"
alias k_config_default="export KUBECONFIG=~/.kube/config"

k config view

Context

# 显示上下文列表
k config get-contexts  

 # 展示当前所处的上下文                        
k config current-context

# 设置默认的上下文为 context-name-xxx
k config use-context context-name-xxx 

# 在指定上下文中持久性地保存名字空间,供所有后续 kubectl 命令使用
k config set-context --current --namespace=default

# 使用特定的用户名和名字空间设置上下文
k config set-context gce --user=cluster-admin --namespace=foo \
  && kubectl config use-context gce

Viewing & finding resources

# Describe commands with verbose output
k describe nodes my-node
k describe pods my-pod

# Get resource manifests
k explain pods
k explain svc/ingress/hpa


# List pods Sorted by creationTimestamp
k get pods --sort-by=.metadata.creationTimestamp

# List pods Sorted by Restart Count
k get pods --sort-by='.status.containerStatuses[0].restartCount'

# List PersistentVolumes sorted by capacity
k get pv --sort-by=.spec.capacity.storage


# Get all running pods in current namespace
k get pods --field-selector=status.phase=Running

# Show labels for all pods 
k get pods --show-labels

Network

# 在本地计算机上侦听端口 5000 并转发到 my-pod 上的端口 6000
kubectl port-forward my-pod 5000:6000               

# 侦听本地端口 5000 并转发到 Service 后端端口 5000
kubectl port-forward svc/my-service 5000    

# 侦听本地端口 5000 并转发到名字为 <my-service-port> 的 Service 目标端口              
kubectl port-forward svc/my-service 5000:my-service-port  

# 侦听本地端口 5000 并转发到 <my-deployment> 创建的 Pod 里的端口 6000
kubectl port-forward deploy/my-deployment 5000:6000