查看kubernetes的版本信息
kubectl version | grep Server
执行结果:
查看pod的信息
kubectl describe pod POD_NAME -n NAMESPACE
注:不写 -n , 默认查的名称空间是 default
执行结果:
查看 pod 的日志
kubectl logs <POD_NAME> [-n NAMESPACE] [-c CONTAINER_NAME] [flags]
注:
其中,<POD_NAME>表示要查看日志的Pod名称;-n NAMESPACE(可选)指定名称空间,不指定的话,默认为 default;-c CONTAINER_NAME(可选)指定容器名称,默认为第一个容器;[flags](可选)包括了额外的标记参数,比如-f来持续输出最新的日志内容等。
执行结果:
查看 kubelet 日志
journalctl -u kubelet
执行结果:
注:journalctl 的用法介绍可以参考这篇文章《journalctl是什么》
监控pod进度
watch kubectl get pod -n kube-system -o wide
执行结果:
查看指定名称空间的pods
kubectl get pods -n kube-system
查看命所有名称空间的pods
kubectl get pods --all-namespaces
执行结果:
查询指定namespace下的pod控制器
kubectl get deploy -n openebs
执行结果:
删除指定pod / 删除指定pod 控制器
kubectl delete deploy maya-apiserver -n openebs
执行结果:
注:直接执行 kubectl delete pod maya-apiserver -n openebs 命令没有用,再次执行 kubectl get pods --all-namespaces 会发现它又重新创建了一个pod,这是因为当前 pod 是由 pod 控制器创建的,控制器会监控 pod 状况,一旦发现 pod 死亡,会立即重建这个 pod,所以必须删除 pod 控制器才有用。
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/docs/reference/generated/kubectl/kubectl-commands
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 官网
Helm官网
Helm3: https://helm.sh/zh/
Helm2: https://v2.helm.sh/