kubernetes基础 – 常用命令

欢迎加入本站的kubernetes技术交流群,微信添加:加Blue_L。


kubectl命令将用户的请求经过处理转换为apiserver的相应接口提交给apiserver处理。 可通过kubectl -v=8查看提交的详细信息。

资源的增删改查

创建资源

kubectl create -f - 
               -f filename/directory/url
               -n namespace
               -l
kubectl create -h
​
kubectl create service loadbalancer test --tcp=80:80
kubectl create deployment test --image=nginx
​
kubectl apply

删除资源

kubectl delete pod name
kubectl delete pod/name -n namespace
                        -l k8s-app=canal
                        -f 
kubectl delete ns
kubectl delete pod/all -all
kubectl delete -h

编辑资源

KUBE_EDITOR=vim kubectl edit cm -n kube-system canal-config
kubectl patch deployment test --patch='{"spec": {"template": {"spec": {"containers": [{"name": "test1", "image": "busybox"}]}}}}'
kubectl apply

获取资源

kubectl get -n
            -l
            -o wide / json /yaml / jsonpath
            -w / --watch-only
            --show-labels
            -A / --all-namespaces

列出所有支持的资源类型

kubectl api-resources

列出所有的资源组版本

kubectl api-versions

描述资源状态

kubectl describe pod / service...

日志相关

kubectl logs -n kube-system --tail=20 -f -c -p
docker ps -a | grep podname
docker logs container_id

代理相关

最好配置本地kubeconfig

# 启动代理
# https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/#install-kubectl-binary-with-curl-on-macos 本地安装kubectl
​
kubectl proxy
curl localhost:8001/api/v1/pods
# http://kubernetes_master_address/api/v1/namespaces/namespace_name/services/[https:]service_name[:port_name]/proxy
​
# 端口转发
kubectl port-forward
kubectl port-forward pod/mypod 5000 6000
kubectl port-forward deployment/mydeployment 5000 6000
kubectl port-forward service/myservice 5000 6000
kubectl port-forward pod/mypod 8888:5000
kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000
kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000
kubectl port-forward pod/mypod :5000

元数据

# 标签
kubectl label
# 批注
kubectl annotate
kubectl annotate -h
kubectl annotate pods foo aaa=bbbb
kubectl annotate -f aaa.yaml aaa=bbb
                 --overwrite
                 --all
kubectl annotate pods foo description-

shell操作

kubectl cp
kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
​
kubectl exec
kubectl exec mypod -- date
kubectl exec mypod -c ruby-container -- date
kubectl exec mypod -c ruby-container -i -t -- bash -il
kubectl exec mypod -i -t -- ls -t /usr
kubectl exec deploy/mydeployment -- date
kubectl exec svc/myservice -- date

查看负载

kubectl top pod -n kube-system -l selector
kubectl top pod express-server
kubectl top nodes

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注