k8s常用命令

** 查看kubernetes的版本信息**

kubectl version | grep Server

执行结果:
在这里插入图片描述


监控pod进度

watch kubectl get pod -n kube-system -o wide

执行结果:
在这里插入图片描述


查看指定名称空间的pods

kubectl get pods -n kube-system

在这里插入图片描述


查看命所有名称空间的pods

kubectl get pods --all-namespaces

执行结果:
在这里插入图片描述


kubectl get 的用法

Examples:
  # List all pods in ps output format.
  kubectl get pods
  
  # List all pods in ps output format with more information (such as node name).
  kubectl get pods -o wide
  
  # List a single replication controller with specified NAME in ps output format.
  kubectl get replicationcontroller web
  
  # List deployments in JSON output format, in the "v1" version of the "apps" API group:
  kubectl get deployments.v1.apps -o json
  
  # List a single pod in JSON output format.
  kubectl get -o json pod web-pod-13je7
  
  # List a pod identified by type and name specified in "pod.yaml" in JSON output format.
  kubectl get -f pod.yaml -o json
  
  # List resources from a directory with kustomization.yaml - e.g. dir/kustomization.yaml.
  kubectl get -k dir/
  
  # Return only the phase value of the specified pod.
  kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}
  
  # List resource information in custom columns.
  kubectl get pod test-pod -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image
  
  # List all replication controllers and services together in ps output format.
  kubectl get rc,services
  
  # List one or more resources by their type and names.
  kubectl get rc/web service/frontend pods/web-pod-13je7

Options:
  -A, --all-namespaces=false: If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
      --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
      --chunk-size=500: Return large lists in chunks rather than all at once. Pass 0 to disable. This flag is beta and may change in the future.
      --field-selector='': Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.
  -f, --filename=[]: Filename, directory, or URL to files identifying the resource to get from a server.
      --ignore-not-found=false: If the requested object does not exist the command will return exit code 0.
  -k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f or -R.
  -L, --label-columns=[]: Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag options like -L label1 -L label2...
      --no-headers=false: When using the default or custom-column output format, don't print headers (default print headers).
  -o, --output='': Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
      --raw='': Raw URI to request from the server.  Uses the transport specified by the kubeconfig file.
  -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.
  -l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
      --server-print=true: If true, have the server return the appropriate table output. Supports extension APIs and CRDs.
      --show-kind=false: If present, list the resource type for the requested object(s).
      --show-labels=false: When printing, show all labels as the last column (default hide labels column)
      --sort-by='': If non-empty, sort list types using this field specification.  The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
      --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
  -w, --watch=false: After listing/getting the requested object, watch for changes. Uninitialized objects are excluded if no object name is provided.
      --watch-only=false: Watch for changes to the requested object(s), without listing/getting first.

Usage:
  kubectl get [(-o|--output=)json|yaml|wide|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=...] (TYPE[.VERSION][.GROUP] [NAME | -l label] | TYPE[.VERSION][.GROUP]/NAME ...) [flags] [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).


kubectl 创建一个tomcat

kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8
# tomcat:6.0.53-jre8 从 docker hub 拉取镜像

执行结果:
在这里插入图片描述


暴露nginx 访问

kubectl expose deployment tomcat6 --port=80 --target-port=8080 --type=NodePort
# Pod的80映射容器的 8080; service 会代理 Pod 的 80

执行结果:
在这里插入图片描述


查看暴露的service

kubectl get svc 
# 或者
kubectl get svc -o wide

注:svc 即 service

执行结果:
在这里插入图片描述
可以使用 30173 端口访问到 tomcat
在这里插入图片描述


查看 deployment

kubectl get deploymen
# 或者
kubectl get deploymen -o wide

执行结果:
在这里插入图片描述


动态扩缩容

kubectl scale --replicas=3 deployment tomcat6
# 扩容了多份,所有无论访问哪个 node 的指定端口,都可以访问到 tomcat6

执行结果:
在这里插入图片描述


查看所有资源信息

kubectl get all

执行结果:
在这里插入图片描述


删除资源
删除 deployment

kubectl delete deployment.apps/tomcat6

执行结果:
在这里插入图片描述

删除 service

kubectl delete service/tomcat6

执行结果:
在这里插入图片描述
再次查看 service
在这里插入图片描述

再次查看 pods
在这里插入图片描述

注:流程;创建 deployment 会管理 replicas,replicas 控制 pod 数量,有 pod 故障会自动拉起
新的 pod


用yaml的方式部署tomcat
先用 --help 命令查看帮助文档
在这里插入图片描述

注:–dry-run : 只是测试,并不真正执行。

# 以yaml方式输出到控制台
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml
# 输出到文件 tomcat6.yaml
kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8 --dry-run -o yaml > tomcat6.yaml

在这里插入图片描述

编辑 tomcat6.yaml, 将没用的命令删除。
在这里插入图片描述
应用 tomcat6.yaml 创建 tomcat 容器

kubectl apply -f tomcat6.yaml

在这里插入图片描述


一个pod创建2个容器

先输出 yaml 文件,得到 mypod.yaml .
在这里插入图片描述
编辑 mypod.yaml 文件
在这里插入图片描述

将不必要的信息删除,删除完成后,再添加一个 nginx 容器
在这里插入图片描述

再应用 mypod.yaml
在这里插入图片描述


更多详细命令可以参考官方文档

Kubernetes 官方文档


kubectl 文档

https://kubernetes.io/zh/docs/reference/kubectl/overview/

资源类型

https://kubernetes.io/zh-cn/docs/reference/kubectl/#%E8%B5%84%E6%BA%90%E7%B1%BB%E5%9E%8B

格式化输出

https://kubernetes.io/zh-cn/docs/reference/kubectl/#%E6%A0%BC%E5%BC%8F%E5%8C%96%E8%BE%93%E5%87%BA

常用操作

https://kubernetes.io/zh-cn/docs/reference/kubectl/#%E7%A4%BA%E4%BE%8B-%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C

命令参考

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands


kubernetes-dashboard.yaml 文件


https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml


Kubersphere 官网


https://kubesphere.io/
https://v2-1.docs.kubesphere.io/docs/zh-CN/
https://v2-1.docs.kubesphere.io/docs/zh-CN/installation/prerequisites/


Kuboard 官网


https://kuboard.cn/support/

相关推荐

  1. K8S 命令

    2024-03-15 08:50:01       38 阅读
  2. K8s命令

    2024-03-15 08:50:01       36 阅读
  3. K8S命令

    2024-03-15 08:50:01       36 阅读
  4. k8s kubectl 命令

    2024-03-15 08:50:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 08:50:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 08:50:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 08:50:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 08:50:01       18 阅读

热门阅读

  1. 力扣大厂热门面试算法题 27-29

    2024-03-15 08:50:01       18 阅读
  2. 【MySQL 系列】MySQL 引擎篇

    2024-03-15 08:50:01       19 阅读
  3. c# 新增一条数据

    2024-03-15 08:50:01       22 阅读
  4. Retelling|Facebook1

    2024-03-15 08:50:01       20 阅读
  5. 第2周 Python列表、元组刷题

    2024-03-15 08:50:01       20 阅读
  6. 缓存和数据库更新的先后处理方案

    2024-03-15 08:50:01       23 阅读
  7. 小程序开发——获取设备信息 API(四)

    2024-03-15 08:50:01       18 阅读
  8. Quartz项目实际使用

    2024-03-15 08:50:01       17 阅读
  9. SQL笔记 -- 黑马程序员

    2024-03-15 08:50:01       18 阅读