zoukankan      html  css  js  c++  java
  • kubectl 命令大全

    1.kubectl 命令补全

    yum install -y bash-completion
    source <(kubectl completion bash) #在bash中设置自动补全
    echo "source <(kubectl completion bash)" >> ~/.bashrc
    # source <(kubectl completion zsh) #在zsh中设置自动补全
    

    2.kubectl上下文和配置

    kubectl config view # 显示合并后的kubeconfig配置
    # 同时使用多个 kubeconfig 文件并查看合并后的配置
    KUBECONFIG=~/.kube/config:~/.kube/config-2 kubectl config view
    
    # 获取e2e用户的密码
    kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}
    
    kubectl config current-context              # 显示当前的上下文
    
    kubectl config use-context my-cluster-name  # 设置默认上下文为 my-cluster-name
    
    
    # 向 kubeconf 中增加支持基本认证的新集群
    $ kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
    
    # 使用指定的用户名和 namespace 设置上下文
    $ kubectl config set-context gce --user=cluster-admin --namespace=foo 
      && kubectl config use-context gce
    
    

    3.创建对象

    Kubernetes 的清单文件可以使用 json 或 yaml 格式定义。可以以 .yaml、.yml、或者 .json 为扩展名。
    kubectl create -f ./app.yaml # 单个文件
    kubectl create -f ./app.yaml ./app02.yaml #多个文件
    kubectl create -f ./dir #目录
    kubectl create -f https://git.io/vPieo         # 使用 url 来创建资源
    
    kubectl run nginx --image=nginx #启动一个nginx实例
    
    kubectl explain pods #获取pod的文档
    kubectl explain svc #获取svc的文档
    
    # 从 stdin 输入中创建多个 YAML 对象
    $ cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox-sleep
    spec:
      containers:
      - name: busybox
        image: busybox
        args:
        - sleep
        - "1000000"
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox-sleep-less
    spec:
      containers:
      - name: busybox
        image: busybox
        args:
        - sleep
        - "1000"
    EOF
    
    # 创建包含几个 key 的 Secret
    $ cat <<EOF | kubectl create -f -
    apiVersion: v1
    kind: Secret
    metadata:
      name: mysecret
    type: Opaque
    data:
      password: $(echo "s33msi4" | base64)
      username: $(echo "jane" | base64)
    EOF
    

    4.显示和查找资源

    kubectl get services
    kubectl get pods --all-namespaces
    kuebctl get pods -o wide
    kubectl get deployment my-dep
    # 输出描述信息
    kubectl describe nodes my-node
    kubectl describe pods my-pod
    
    # 按服务名称进行排序
    kubectl get services --sort-by=.metadata.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}'
    # 查看哪些节点已经就绪
    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
    

    5.更新资源

    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 # 强制替换,删除后重新创建资源。会导致服务中断。
    kubectl expose rc nginx --port=80 --target-port=8000   # 为nginx RC 创建服务,启动本地80端口连接到容器上的8000端口
    kubectl get pod mypod -o yaml | sed 's/(image: myimage):.*$/1:v4/' | kubectl replace -f -  # 更新单容器pod的镜像版本(tag)到v4
    kubectl label pods my-pod new-label=awesome #添加标签 
    kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq #添加注解
    kubectl autoscale deployment goo --min=2 --max=10 #自动扩展deployment “foo”
    

    6.修补资源

    $ 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"}]'
    

    7.编辑资源

    kubectl edit svc/docker-registry
    KUBE_EDITOR="vim" kubectl edit svc/docker-registry   # 使用其它编辑器
    

    8.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
    

    9.删除资源

    kubectl delete -f ./pod.json
    kubectl delete -f ./pod.yaml
    kubectl delete pods,service baz foo # 删除pod baz 和svc foo
    kubectl delete pods,services -l name=myLabel # 删除具有name=myLabel标签的pod和services
    kubectl -n my-ns delete po,svc -all #删除my-ns名称空间下的所有pod和svc
    

    10.与运行中的pod交互

    kubectl logs mypod
    kubectl logs mypod -c my-container #输出pod中容器名称为my-container的日志
    kubectl logs -f mypod # 查询pod的日志
    kubectl logs -f --tail 500 mypod # 查询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 mypod -- ls / #在容器中执行命令
    kubectl exec my-pod -c my-container -- ls /         # 在已存在的容器中执行命令(pod中有多个容器的情况下)
    kubectl top pod pod-name --containers # 显示指定pod和容器的指标度量
    

    11.与节点和集群交互

    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
    kubectl taint nodes foo dedicated=special-user:NoSchedule ##如果该键和影响的污点(taint)已存在,则使用指定的值替换
    

    12.资源类型

    下表列出的是 kubernetes 中所有支持的类型和缩写的别名。

    资源类型 缩写别名
    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

    13.格式化输出

    要以特定的格式向终端窗口输出详细信息,可以在 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 对象
    

    14.Kubectl 详细输出和调试

    使用 -v 或 --v 标志跟着一个整数来指定日志级别。
    
    详细等级	描述
    --v=0	总是对操作人员可见。
    --v=1	合理的默认日志级别,如果您不需要详细输出。
    --v=2	可能与系统的重大变化相关的,有关稳定状态的信息和重要的日志信息。这是对大多数系统推荐的日志级别。
    --v=3	有关更改的扩展信息。
    --v=4	调试级别详细输出。
    --v=6	显示请求的资源。
    --v=7	显示HTTP请求的header。
    --v=8	显示HTTP请求的内容。
    
    欢迎大家评论转发
  • 相关阅读:
    JS中的事件&对象
    WSL使用小结:从ArchLinux到Manjaro
    WSL学习:安装ArchLinux和Root/Cling以及注意事项
    Qt5网络请求使用及WebRequest函数
    有道词典中的OCR功能:第三方库的变化
    C++学习(二):学会使用stringstream
    C语言集锦(三)Direct3D和GDI+的例子
    网络那些事:浏览器、网络协议以及常见软件
    C++学习(一):现代C++尝试
    PostScript学习:另一种缩写为PS的技术
  • 原文地址:https://www.cnblogs.com/os4top16/p/12742639.html
Copyright © 2011-2022 走看看