# 查看所有的命名空间
kubectl get namespace / kubectl get ns
# 创建新的命名空间
kubectl create namespace fooval
# 删除命名空间
kubectl delete namespaces fooval
# 查看命名空间信息
kubectl describe ns/namespace fooval
# 创建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
# 查看服务
kubectl get services/svc -n fooval
# 查看集群状态信息
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 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
# 显示上下文列表
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
# 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
# 在本地计算机上侦听端口 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