zoukankan      html  css  js  c++  java
  • kubectl 常用命令

    查看帮助

    [root@Master1 ~]# kubectl --help

    查看版本

    [root@Master1 ~]# kubectl --version
    Kubernetes v1.5.2
    

    get命令

    get命令用于获取集群的一个或一些resource信息。 使用–help查看详细信息。 Ps:kubectl的帮助信息、示例相当详细,而且简单易懂。建议大家习惯使用帮助信息。kubectl可以列出集群所有resource的详细。resource包括集群节点、运行的pod,ReplicationController,service等。

    查看所有的pods

    [root@Master1 ~]# kubectl get pod
    NAME               READY     STATUS    RESTARTS   AGE
    pod-redis          1/1       Running   1          4h
    rc-nginx-3-k962j   1/1       Running   0          13m
    rc-nginx-3-sfw9n   1/1       Running   0          13m
    rc-nginx-3-v32k0   1/1       Running   0          12m
    

    查看所有的nodes

    [root@Master1 ~]# kubectl get nodes
    NAME      STATUS    AGE
    node1     Ready     2d
    node2     Ready     2d
    

    查看所有的pods(详细)

    [root@Master1 ~]# kubectl get pods -o wide
    NAME               READY     STATUS    RESTARTS   AGE       IP          NODE
    pod-redis          1/1       Running   1          4h        10.0.78.2   node1
    rc-nginx-3-k962j   1/1       Running   0          15m       10.0.64.2   node2
    rc-nginx-3-sfw9n   1/1       Running   0          15m       10.0.64.4   node2
    rc-nginx-3-v32k0   1/1       Running   0          15m       10.0.78.3   node1
    [root@Master1 ~]# kubectl get nodes -o wide
    NAME      STATUS    AGE       EXTERNAL-IP
    node1     Ready     2d        <none>
    node2     Ready     2d        <none>
    

    查看所有的namespace

    [root@Master1 ~]# kubectl get pod --all-namespaces
    NAMESPACE     NAME                        READY     STATUS             RESTARTS   AGE
    default       pod-redis                   1/1       Running            1          4h
    default       rc-nginx-3-k962j            1/1       Running            0          17m
    default       rc-nginx-3-sfw9n            1/1       Running            0          17m
    default       rc-nginx-3-v32k0            1/1       Running            0          16m
    kube-system   kube-dns-2025727478-x6qns   0/2       ImagePullBackOff   0          33m
    

    以yaml文件形式显示一个pod详细信息

    [root@Master1 ~]# kubectl get pod pod-redis -o yaml
    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: 2018-05-07T01:47:20Z
      labels:
        name: redis
      name: pod-redis
      namespace: default
      resourceVersion: "88366"
      selfLink: /api/v1/namespaces/default/pods/pod-redis
      uid: 94d4daa5-5198-11e8-8660-000c29a28b16
    spec:
      containers:
      - image: docker.io/redis:latest
        imagePullPolicy: Always
        name: pod-redis
        ports:
        - containerPort: 6379
          hostPort: 6379
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
      dnsPolicy: ClusterFirst
      nodeName: node1
      nodeSelector:
        zone: node1
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30
    status:
      conditions:
      - lastProbeTime: null
        lastTransitionTime: 2018-05-07T01:47:20Z
        status: "True"
        type: Initialized
      - lastProbeTime: null
        lastTransitionTime: 2018-05-07T01:51:32Z
        status: "True"
        type: Ready
      - lastProbeTime: null
        lastTransitionTime: 2018-05-07T01:47:20Z
        status: "True"
        type: PodScheduled
      containerStatuses:
      - containerID: docker://193a93d80f61d80a963d19d85afe370b950b2b4f9321bc5abd7187b85d806104
        image: docker.io/redis:latest
        imageID: docker-pullable://docker.io/redis@sha256:4aed8ea5a5fc4cf05c8d5341b4ae4a4f7c0f9301082a74f6f9a5f321140e0cd3
        lastState:
          terminated:
            containerID: docker://78869119cf6ce869df4f92814e6c0e22a61885af9e7e2d1ca65a2e814eb80157
            exitCode: 1
            finishedAt: 2018-05-07T01:51:45Z
            reason: Error
            startedAt: 2018-05-07T01:51:45Z
        name: pod-redis
        ready: true
        restartCount: 1
        state:
          running:
            startedAt: 2018-05-07T01:51:31Z
      hostIP: 192.168.13.163
      phase: Running
      podIP: 10.0.78.2
      startTime: 2018-05-07T01:47:20Z
    

    以jison格式显示一个pod的详细信息

    [root@Master1 ~]# kubectl get pod pod-redis -o json
    

    以describe显示一个pod的详细信息

    describe类似于get,同样用于获取resource的相关信息。不同的是,get获得的是更详细的resource个性的详细信息,describe获得的是resource集群相关的信息。describe命令同get类似,但是describe不支持-o选项,对于同一类型resource,describe输出的信息格式,内容域相同。 注:如果发现是查询某个resource的信息,使用get命令能够获取更加详尽的信息。但是如果想要查询某个resource的状态,如某个pod并不是在running状态,这时需要获取更详尽的状态信息时,就应该使用describe命令。

    [root@Master1 ~]# kubectl describe pod pod-redis
    

    create

    使用create则可以基于rc-nginx-3.yaml文件创建出ReplicationController(rc),rc会创建两个副本:

    [root@Master1 ~]# kubectl create -f rc-nginx.yaml
    

    replace

    replace命令用于对已有资源进行更新、替换。如前面create中创建的nginx,当我们需要更新resource的一些属性的时候,如果修改副本数量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然后执行replace命令。 注:名字不能被更更新。另外,如果是更新label,原有标签的pod将会与更新label后的rc断开联系,有新label的rc将会创建指定副本数的新的pod,但是默认并不会删除原来的pod。所以此时如果使用get po将会发现pod数翻倍,进一步check会发现原来的pod已经不会被新rc控制,此处只介绍命令不详谈此问题,好奇者可自行实验。

    [root@Master1 ~]# kubectl replace -f rc-nginx.yaml
    

    patch

    如果一个容器已经在运行,这时需要对一些容器属性进行修改,又不想删除容器,或不方便通过replace的方式进行更新。kubernetes还提供了一种在容器运行时,直接对容器进行修改的方式,就是patch命令。 如前面创建pod的label是app=nginx-2,如果在运行过程中,需要把其label改为app=nginx-3,这patch命令如下:

    [root@Master1 ~]# kubectl  patch pod rc-nginx-3-k962j -p '{"metadata":{"labels":{"app":"nginx-3"}}}'
    "rc-nginx-3-k962j" patched
    

    edit

    edit提供了另一种更新resource源的操作,通过edit能够灵活的在一个common的resource基础上,发展出更过的significant resource。例如,使用edit直接更新前面创建的pod的命令为:

    [root@Master1 ~]# kubectl edit pod rc-nginx-3-k962j
    

    上面命令的效果等效于:

    [root@Master1 ~]# kubectl get pod rc-nginx-3-k962j -o yaml >> /tmp/nginx-tmp.yaml
    [root@Master1 ~]# vim /tmp/nginx-tmp.yaml
    

    Delete

    根据resource名或label删除resource。

    [root@Master1 ~]# kubectl delete -f rc-nginx.yaml 
    replicationcontroller "rc-nginx-3" deleted
    [root@Master1 ~]# kubectl delete pod -lapp=nginx-2
    

    apply

    apply命令提供了比patch,edit等更严格的更新resource的方式。通过apply,用户可以将resource的configuration使用source control的方式维护在版本库中。每次有更新时,将配置文件push到server,然后使用kubectl apply将更新应用到resource。kubernetes会在引用更新前将当前配置文件中的配置同已经应用的配置做比较,并只更新更改的部分,而不会主动更改任何用户未指定的部分。 apply命令的使用方式同replace相同,不同的是,apply不会删除原有resource,然后创建新的。apply直接在原有resource的基础上进行更新。同时kubectl apply还会resource中添加一条注释,标记当前的apply。类似于git操作。

    logs

    logs命令用于显示pod运行中,容器内程序输出到标准输出的内容。跟docker的logs命令类似。如果要获得tail -f 的方式,也可以使用-f选项。

    [root@Master1 ~]# kubectl get pods
    NAME        READY     STATUS    RESTARTS   AGE
    pod-redis   1/1       Running   1          5h
    
    [root@Master1 ~]# kubectl  logs pod-redis
    1:C 07 May 01:51:31.414 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1:C 07 May 01:51:31.415 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
    1:C 07 May 01:51:31.415 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    1:M 07 May 01:51:31.416 * Running mode=standalone, port=6379.
    1:M 07 May 01:51:31.416 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    1:M 07 May 01:51:31.416 # Server initialized
    1:M 07 May 01:51:31.416 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    1:M 07 May 01:51:31.416 * Ready to accept connections
    

    rolling-update

    rolling-update是一个非常重要的命令,对于已经部署并且正在运行的业务,rolling-update提供了不中断业务的更新方式。rolling-update每次起一个新的pod,等新pod完全起来后删除一个旧的pod,然后再起一个新的pod替换旧的pod,直到替换掉所有的pod。 rolling-update需要确保新的版本有不同的name,Version和label,否则会报错 。

    [root@Master1 ~]# kubectl rolling-update rc-nginx-2 -f  rc-nginx.yaml
    

    如果在升级过程中,发现有问题还可以中途停止update,并回滚到前面版本

    [root@Master1 ~]# kubectl rolling-update rc-nginx-2 -rollback
    

    rolling-update还有很多其他选项提供丰富的功能,如—update-period指定间隔周期,使用时可以使用-h查看help信息。

    scale

    scale用于程序在负载加重或缩小时副本进行扩容或缩小,如前面创建的nginx有两个副本,可以轻松的使用scale命令对副本数进行扩展或缩小。 扩展副本数到4:

    [root@Master1 ~]# kubectl scale rc rc-nginx-3 --replicas=4
    replicationcontroller "rc-nginx-3" scaled
    

    重新缩减副本数到2:

    [root@Master1 ~]# kubectl scale rc rc-nginx-3 --replicas=2
    replicationcontroller "rc-nginx-3" scaled
    

    autoscale

    scale虽然能够很方便的对副本数进行扩展或缩小,但是仍然需要人工介入,不能实时自动的根据系统负载对副本数进行扩、缩。autoscale命令提供了自动根据pod负载对其副本进行扩缩的功能。 autoscale命令会给一个rc指定一个副本数的范围,在实际运行中根据pod中运行的程序的负载自动在指定的范围内对pod进行扩容或缩容。如前面创建的nginx,可以用如下命令指定副本范围在1~4

    [root@Master1 ~]# kubectl autoscale rc rc-nginx-3 --min=1 --max=4
    

    attach

    类似于docker attach的功能,用于取得实时的类似于kubectl logs的信息

    [root@Master1 ~]# kubectl get pods
    NAME               READY     STATUS    RESTARTS   AGE
    pod-redis          1/1       Running   1          5h
    rc-nginx-3-6bjmk   1/1       Running   0          20m
    rc-nginx-3-d9ntz   1/1       Running   0          20m
    
    [root@Master1 ~]# kubectl attach pod-redis
    If you don't see a command prompt, try pressing enter.
    1:C 07 May 01:51:31.414 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    1:C 07 May 01:51:31.415 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
    1:C 07 May 01:51:31.415 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    1:M 07 May 01:51:31.416 * Running mode=standalone, port=6379.
    1:M 07 May 01:51:31.416 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    1:M 07 May 01:51:31.416 # Server initialized
    1:M 07 May 01:51:31.416 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    1:M 07 May 01:51:31.416 * Ready to accept connection
    

    port-forward

    转发一个本地端口到容器端口,博主一般都是使用yaml的方式编排容器,所以基本不使用此命令。

    proxy

    只尝试过使用nginx作为kubernetes多master HA方式的代理,没有使用过此命令为kubernetes api server运行过proxy。

    run

    类似于docker的run命令,直接运行一个image。

    label

    为kubernetes集群的resource打标签,如前面实例中提到的为rc打标签对rc分组。还可以对nodes打标签,这样在编排容器时,可以为容器指定nodeSelector将容器调度到指定lable的机器上,如如果集群中有IO密集型,计算密集型的机器分组,可以将不同的机器打上不同标签,然后将不同特征的容器调度到不同分组上。 在1.2之前的版本中,使用kubectl get nodes则可以列出所有节点的信息,包括节点标签,1.2版本中不再列出节点的标签信息,如果需要查看节点被打了哪些标签,需要使用describe查看节点的信息。

    cp

    kubectl cp 用于pod和外部的文件交换,比如如下示例了如何在进行内外文件交换。 在pod中创建一个文件message.log

    [root@Master1 ~]# kubectl get pods
    NAME               READY     STATUS    RESTARTS   AGE
    pod-redis          1/1       Running   1          5h
    rc-nginx-3-6bjmk   1/1       Running   0          32m
    rc-nginx-3-d9ntz   1/1       Running   0          32m
    [root@Master1 ~]# kubectl exec -it rc-nginx-3-6bjmk sh
    # pwd
    /
    # cd /tmp     
    # echo "this is a message from `hostname`" >message.log
    # cat message.log     
    this is a message from rc-nginx-3-6bjmk
    拷贝出来并确认
    [root@Master1 ~]# kubectl cp rc-nginx-3-6bjmk:/tmp/message.log message.log
    tar: Removing leading `/' from member names
    [root@Master1 ~]# cat message.log 
    this is a message from rc-nginx-3-6bjmk
    
    

    更改message.log并拷贝回pod

    [root@Master1 ~]# echo "information added in `hostname`" >>message.log
    [root@Master1 ~]# cat message.log 
    this is a message from rc-nginx-3-6bjmk
    information added in Master1
    [root@Master1 ~]# kubectl  cp message.log rc-nginx-3-6bjmk:/tmp/message.log

    确认更改后的信息

    [root@Master1 ~]# kubectl exec rc-nginx-3-6bjmk cat /tmp/message.log
    this is a message from rc-nginx-3-6bjmk
    information added in Master1

    kubectl cluster-info

    使用cluster-info和cluster-info dump也能取出一些信息,尤其是你需要看整体的全部信息的时候一条命令一条命令的执行不如kubectl cluster-info dump来的快一些

    [root@Master1 ~]# kubectl cluster-info
    Kubernetes master is running at http://localhost:8080
    KubeDNS is running at http://localhost:8080/api/v1/proxy/namespaces/kube-system/services/kube-dns
    
    To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

    作者:HaydenGuo

    出处:https://www.cnblogs.com/ghl1024/

    每一个前十年都想不到后十年我会演变成何等模样,可知人生无常,没有什么规律,没有什么必然。

    只要我还对新鲜的知识、品格的改进、情感的扩张、有胃口,这日子就是值得度过的。

  • 相关阅读:
    SQL Server临时表的使用方案
    SqlServer 临时表
    SqlServer 数据表数据移动
    IQ/OQ/DQ/PQ
    (转)C# WebApi 跨域问题解决方案:CORS
    (转).Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?
    (转)C#动态webservice调用接口
    (转)wsdl文件用SoapUI快速创建WebService,CXF生成客户端代码
    (转)C# Oracle数据库操作类
    (转)C#连接Oracle数据库(直接引用dll使用)
  • 原文地址:https://www.cnblogs.com/ghl1024/p/12078289.html
Copyright © 2011-2022 走看看