k8s常用命令

301 阅读1分钟

随着企业的服务上云,开发人员熟练掌握k8s有利于问题排查,本文整理常用的k8s命令,后面也会持续更新添加。

kubectl apply -f xx.yaml -n ns //部署pod
kubectl delete -f xx.yaml -n ns //删除pod
kubectl edit deploy podname -n ns //升级更新app pod镜像
kubectl edit statefulset wangfeng-c3 -n wangfeng //更新statefulset镜像
kubectl scale deploy console-tm-service --replicas=4 -n wangfeng2 //扩所容pod

kubectl get pods -n ns //查看pod列表
kubectl get nodes -n ns //查看node列表
kubectl get nods -n ns --show-labels //查看标签信息
kubectl describe pod xxx -n ns //查看pod信息,包括运行在哪个节点上

kubectl logs -f podname -n ns [container name] //查看pod中某个容器日志

kubectl label node nodename key=value //给节点打标签
kubectl taint node nodename key=value:NoSchedule //给节点打污点,配合容忍度,可以设置该节点只能运行某些pod 

kubectl exec -it podname -n ns -- /bin/sh //登录pod容器

docker exec -it [container_id] /bin/sh //登录docker容器 

kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar //拷贝文件或文件夹

kubectl exec wangfeng1-c1-0 -n wangfeng1 -- sh -c 'rm -rf /opt/gemini/parta' //删除pod文件

kubectl create rolebinding "tmp:resource-controller-role-bind-username1"   
--clusterrole="tmp:resource-controller" --user=username1 -n wangfeng2 //授权用户访问集群

Dockerfile 的指令每执行一次都会在 docker 上新建一层。所以过多无意义的层,会造成镜像膨胀过大。例如:

FROM centos
RUN yum install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN tar -xvf redis.tar.gz

以上执行会创建 3 层镜像。可简化为以下格式:

FROM centos
RUN yum install wget \
    && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
    && tar -xvf redis.tar.gz

如上,以 && 符号连接命令,这样执行后,只会创建 1 层镜像。 参考:www.runoob.com/docker/dock…