【K8s笔记】kubernetes的相关命令

这是一些常见的kubectldocker命令

kubectl

other

  • kubectl get nodes
  • kubectl get namespaces / ns
  • kubectl delete all --all - 注意,使用这个命令的时候,kubernetes服务也会被删除,但是会自动重新创建
  • kubectl apply -f {YAML}
  • kubectl delete -f {YAML}

context

  • kubectl config current-context
  • kubectl config get-contexts
  • kubectl config user-context {CONTEXT} - set context
  • kubectl config delete-context {CONTEXT}
  • kubectl config rename-context {OLD_NAME} {NEW_NAME}
  • kubectl config delete-context {CONTEXT}
  • kubectx - see all contexts
  • kubectx {CONTEXT} - switch context
  • kubectx {OLD_CONTEXT}={NEW_CONTEXT} - rename context
  • kubectx -d {CONTEXT}- del context

namespace

  • kubectl get ns
  • kubectl config set-context --current --namespace={NAMESPACE}
  • kubectl create ns {NAMESPACE}
  • kubectl delete ns {NAMESPACE}
  • kubectl get pods --all-namespaces

pod

  • kubectl get pods
  • kubectl describe pod {POD}
  • kubectl delete pod {POD}
  • kubectl run {POD} --image={IMAGE}
  • kubectl get pods -o wide
  • kubectl exec {POD} -- {COMMAND}

deployment

  • kubectl get deployments / kubectl get deploy
  • kubectl create deployment {DEPLOYMENT} --image={IMAGE} --replicas=n
  • kubectl describe deployment {DEPLOYMENT}
  • kubectl delete deployment {DEPLOYMENT}
  • kubectl scale deployment {DEPLOYMENT} --replicas=n
  • kubectl delete deployment {DEPLOYMENT}
  • kubectl set image deployment {DEPLOYMENT} {DEPLOYMENT}={NEW_IMAGE}
  • kubectl rollout status deployment {DEPLOYMENT}

Service

  • kubectl get services / kubectl get svc
  • kubectl describe service {SERVICE}
  • kubectl delete service {SERVICE}
  • kubectl expose deployment {DEPLOYMENT} --port={OUTER_PORT} --target-port={INNER_PORT} - expose这里就会自动映射随机pod了
  • kubectl expose deployment {DEPLOYMENT} --type=NodePort --port={INNER_PORT}
  • kubectl expose deploy {DEPLOYMENT} --type=LoadBalancer --port={INNER_PORT}

docker

management

  • docker info
  • docker version
  • docker login
  • docker images
  • docker ps - running container
  • docker ps -a - running & stopping container
  • docker image inspect {CONTAINER}

running & stopping

  • docker pull {IMAGE}
  • docker run {IMAGE}
  • docker run -d {IMAGE} - detached
  • docker run --name {NAME} - customize container name
  • docker run -v {VOLUME / DIR_IN_THE_HOST}:{DIR_IN_THE_CONTAINER}
  • docker start {CONTAINER}
  • docker stop/kill {CONTAINER}

limit

  • docker run --memory="{MAX_MEMORY}" {IMAGE}
  • docker run --cpus="{MAX_CPU_CORE}" {IMAGE}

shell

  • docker run -it {CONTAINER} -- {SHELL} - attach shell
  • docker run -it -- microsoft/powershell:nanoserver pwsh.exe - attach powershell
  • docker container exec -it {CONTAINER} {SHELL} - attach shell to a running container
  • docker container exec -it {CONTAINER} bash/sh - “ssh” into a container

cleaning up

  • docker rm {CONTAINER}
  • docker rm $(docker ps -a -q) - rm all stopped container
  • docker rmi {IMAGE}
  • docker system prune -a - rm all image that not in use, CAREFULLY USE IT

building

  • docker build -t {NAME:TAG} -f {FILE}
  • docker build -t {NAME:TAG} .
  • docker tag {IMAGE} {NAME:TAG} - tag an existing image

volume - use in the production env

  • docker create volume {VOLUME}
  • docker volume ls
  • docker volume inspect {VOLUME}
  • docker volume rm {VOLUME}
  • docker volume prune - del all volume that are not mounted

compose

需要docker-compose.yml配置文件在项目的根目录

  • docker compose build -f {DOCKERFILE}
  • docker compose start
  • docker compose stop
  • docker compose up -d - build and start
  • docker compose ps
  • docker compose rm
  • docker compose down - stop and rm
  • docker compose logs - get logs
  • docker compose exec {CONTAINER} sh/bash
  • docker compose --project-name {PROJECT_NAME} up -d
  • docker compose -p {PROJECT_NAME} up -d
  • docker compose ls
  • docker compose cp {CONTAINER_ID}:{SRC_PATH} {LOCAL_DEST_PATH}
  • docker compose cp {LOCAL_SRC_PATH} {CONTAINER_ID}:{DEST_PATH} - only support host to container or container to host

registry

  • docker tag {IMAGE} {USER}/{IMAGE}:{TAR}
  • docker pull {USER}/{IMAGE}:{TAG}
  • docker push {USER}/{IMAGE}:{TAG}

【K8s笔记】kubernetes的相关命令
https://学习.fun/k8s-note/k8s-cli/
Author
Stephen Zeng
Posted on
April 29, 2025
Licensed under