kubernetes深入浅出(全网独一无二笔记)

Kubernetes 基础部分

kubectl与集群交互

Basic Commands (Beginner):

create      从文件或者stdin创建资源
export      为deployment,pod创建service
run         
set

Basic Commands (Intermediate):

get
explain
edit
delete 

Basic Commands and Debugging Commands:

describe        查看资源详情
logs            查看pod日志
attach          attach到pod内的一个容器
exec            在指定容器内执行命令
pory-forward    为pod创建本地端口映射
proxy           为kubernetes Api Server创建代理
cp              容器内外、容器间文件拷贝
## kubectl proxy --address='0.0.0.0'  --accept-hosts='^*$'
## kubectl pory-forward deployment/dh-yace 8080:8080

Advanced Commands:

apply       从文件或者stdin创建更新内容
patch       使用startegic merge patch 语法更新对象的某些字段
replace     从文件或stdin更新资源
convert     在不同对象之间转换对象定义

Settings Commands:

label       给资源设置label
annotate    给资源设置annotation
completion  获取shell自动补全脚本(支持bash以及zsh)

Other Commands:

api-versions    Print the supported API versions on the server, in the form of "group/version"
config          修改kubectl配置(kubeconfig文件),如context
help            Help about any command
version         查看客户端和server端K8S版本

操作方法

查看资源缩写
kubectl describe {deployment/pod}
kubectl自动补全
source <(kubectl completion bash)
## 可能会出现kubectl -bash: _get_comp_words_by_ref: command not found错误,是需要安装yum -y install bash-completion后,source /etc/bash-completion.d/*即可。
用命令生成yaml文件
kubectl run --image=nginx myapp -o yaml --dry-run >myapp.yaml
用get命令导出yaml文件
kubectl get deployment/podName -o yaml --export >app.yaml
发布应用yaml文件结构查询
kubectl explain pod.spec.*
查看集群状态
kubectl cluster-info
查看集群所有组件状态信息
kubectl get componentstatus
查看集群命令合集
kubectl describe 回车
查看创建pod启动状态
kubectl get pod --watch
扩容
kubectl scale deployment nginx -n dhph --replicas=2

显示和查找资源

Get commands with basic output
$ kubectl get services                          # 列出所有 namespace 中的所有 service
$ kubectl get pods --all-namespaces             # 列出所有 namespace 中的所有 pod
$ kubectl get pods -o wide                      # 列出所有 pod 并显示详细信息
$ kubectl get deployment my-dep                 # 列出指定 deployment
$ kubectl get pods --include-uninitialized      # 列出该 namespace 中的所有 pod 包括未初始化的
使用详细输出来描述命令
$ kubectl describe nodes my-node
$ kubectl describe pods my-pod
$ kubectl get services --sort-by=.metadata.name # List Services Sorted by Name
根据重启次数排序列出 pod
$ kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
获取所有具有 app=cassandra 的 pod 中的 version 标签
$ kubectl get pods --selector=app=cassandra rc -o \
  jsonpath='{.items[*].metadata.labels.version}'
获取所有节点的 ExternalIP
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
列出属于某个 PC 的 Pod 的名字
“jq”命令用于转换复杂的 jsonpath,参考 https://stedolan.github.io/jq/
$ sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
$ echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})
查看哪些节点已就绪
$ JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
列出当前 Pod 中使用的 Secret
$ kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

更新资源

$ kubectl rolling-update frontend-v1 -f frontend-v2.json           # 滚动更新 pod frontend-v1
$ kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2  # 更新资源名称并更新镜像
$ kubectl rolling-update frontend --image=image:v2                 # 更新 frontend pod 中的镜像
$ kubectl rolling-update frontend-v1 frontend-v2 --rollback        # 退出已存在的进行中的滚动更新
$ cat pod.json | kubectl replace -f -                              # 基于 stdin 输入的 JSON 替换 pod
# 强制替换,删除后重新创建资源。会导致服务中断。
$ kubectl replace --force -f ./pod.json
# 为 nginx RC 创建服务,启用本地 80 端口连接到容器上的 8000 端口
$ kubectl expose rc nginx --port=80 --target-port=8000
# 更新单容器 pod 的镜像版本(tag)到 v4
$ kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
$ kubectl label pods my-pod new-label=awesome                      # 添加标签
$ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq       # 添加注解
$ kubectl autoscale deployment foo --min=2 --max=10                # 自动扩展 deployment “foo”

修改资源

使用策略合并补丁并修补资源。

$ kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}' # 部分更新节点
# 更新容器镜像; spec.containers[*].name 是必须的,因为这是合并的关键字
$ kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# 使用具有位置数组的 json 补丁更新容器镜像
$ kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
# 使用具有位置数组的 json 补丁禁用 deployment 的 livenessProbe
$ kubectl patch deployment valid-deployment  --type json   -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'

Scale资源

$ kubectl scale --replicas=3 rs/foo                                 # Scale a replicaset named 'foo' to 3
$ kubectl scale --replicas=3 -f foo.yaml                            # Scale a resource specified in "foo.yaml" to 3
$ kubectl scale --current-replicas=2 --replicas=3 deployment/mysql  # If the deployment named mysql's current size is 2, scale mysql to 3
$ kubectl scale --replicas=5 rc/foo rc/bar rc/baz                   # Scale multiple replication controllers

删除资源

$ kubectl delete -f ./pod.json                                              # 删除 pod.json 文件中定义的类型和名称的 pod
$ kubectl delete pod,service baz foo                                        # 删除名为“baz”的 pod 和名为“foo”的 service
$ kubectl delete pods,services -l name=myLabel                              # 删除具有 name=myLabel 标签的 pod 和 serivce
$ kubectl delete pods,services -l name=myLabel --include-uninitialized      # 删除具有 name=myLabel 标签的 pod 和 service,包括尚未初始化的
$ kubectl -n my-ns delete po,svc --all                                      # 删除 my-ns namespace 下的所有 pod 和 serivce,包括尚未初始化的

与运行中的pod进行交互

$ kubectl logs my-pod                                 # dump 输出 pod 的日志(stdout)
$ kubectl logs my-pod -c my-container                 # dump 输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
$ kubectl logs -f my-pod                              # 流式输出 pod 的日志(stdout)
$ kubectl logs -f my-pod -c my-container              # 流式输出 pod 中容器的日志(stdout,pod 中有多个容器的情况下使用)
$ kubectl run -i --tty busybox --image=busybox -- sh  # 交互式 shell 的方式运行 pod
$ kubectl attach my-pod -i                            # 连接到运行中的容器
$ kubectl port-forward my-pod 5000:6000               # 转发 pod 中的 6000 端口到本地的 5000 端口
$ kubectl exec my-pod -- ls /                         # 在已存在的容器中执行命令(只有一个容器的情况下)
$ kubectl exec my-pod -c my-container -- ls /         # 在已存在的容器中执行命令(pod 中有多个容器的情况下)
$ kubectl top pod POD_NAME --containers               # 显示指定 pod 和容器的指标度量

与节点进行交互

$ kubectl cordon my-node                                                # 标记 my-node 不可调度
$ kubectl drain my-node                                                 # 清空 my-node 以待维护
$ kubectl uncordon my-node                                              # 标记 my-node 可调度
$ kubectl top node my-node                                              # 显示 my-node 的指标度量
$ kubectl cluster-info                                                  # 显示 master 和服务的地址
$ kubectl cluster-info dump                                             # 将当前集群状态输出到 stdout                     
$ kubectl cluster-info dump --output-directory=/path/to/cluster-state   # 将当前集群状态输出到 /path/to/cluster-state
# 如果该键和影响的污点(taint)已存在,则使用指定的值替换
$ kubectl taint nodes foo dedicated=special-user:NoSchedule

资源类型

资源类型	                     缩写别名
clusters	
componentstatuses               	cs
configmaps                      	cm
daemonsets	                        ds
deployments	                        deploy
endpoints	                        ep
event	                            ev
horizontalpodautoscalers        	hpa
ingresses                       	ing
jobs	
limitranges	                        limits
namespaces                      	ns
networkpolicies	
nodes	                            no
statefulsets	
persistentvolumeclaims	            pvc
persistentvolumes               	pv
pods	                            po
podsecuritypolicies             	psp
podtemplates	
replicasets	                        rs
replicationcontrollers	            rc
resourcequotas                  	quota
cronjob	
secrets	
serviceaccount                  	sa
services                        	svc
storageclasses	
thirdpartyresources

格式输出

要以特定的格式向终端窗口输出详细信息,可以在 kubectl 命令中添加 -o 或者 -output 标志。

输出格式	描述
-o=custom-columns=<spec>	        使用逗号分隔的自定义列列表打印表格
-o=custom-columns-file=<filename>	使用 文件中的自定义列模板打印表格
-o=json	                            输出 JSON 格式的 API 对象
-o=jsonpath=<template>	            打印 jsonpath 表达式中定义的字段
-o=jsonpath-file=<filename>	        打印由 文件中的 jsonpath 表达式定义的字段
-o=name	                            仅打印资源名称
-o=wide	                            以纯文本格式输出任何附加信息,对于 Pod ,包含节点名称
-o=yaml	                            输出 YAML 格式的 API 对象

kubectl详细输出和调试

使用 -v 或 --v 标志跟着一个整数来指定日志级别。这里 描述了通用的 kubernetes 日志约定和相关的日志级别。

详细等级	描述
--v=0	总是对操作人员可见。
--v=1	合理的默认日志级别,如果您不需要详细输出。
--v=2	可能与系统的重大变化相关的,有关稳定状态的信息和重要的日志信息。这是对大多数系统推荐的日志级别。
--v=3	有关更改的扩展信息。
--v=4	调试级别详细输出。
--v=6	显示请求的资源。
--v=7	显示HTTP请求的header。
--v=8	显示HTTP请求的内容。

Kubernetes调度管理高级部分

Kubernetes调度器基本概念
scheduler之scheduling,为pod找到一个合适的Node
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥子下凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值