k8s kubectl 命令用法集

KUBECTL 命令使用

kubectl 查看所有flag是否有简称

#kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
events                            ev                                          true         Event
limitranges                       limits                                      true         LimitRange
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim
persistentvolumes                 pv                                          false        PersistentVolume
pods                              po                                          true         Pod
podtemplates                                                                  true         PodTemplate
replicationcontrollers            rc                                          true         ReplicationController
resourcequotas                    quota                                       true         ResourceQuota
secrets                                                                       true         Secret
serviceaccounts                   sa                                          true         ServiceAccount
services                          svc                                         true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io         false        APIService
controllerrevisions                            apps                           true         ControllerRevision
daemonsets                        ds           apps                           true         DaemonSet
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           apps                           true         ReplicaSet
statefulsets                      sts          apps                           true         StatefulSet
tokenreviews                                   authentication.k8s.io          false        TokenReview
localsubjectacce***eviews                      authorization.k8s.io           true         LocalSubjectAcce***eview
selfsubjectacce***eviews                       authorization.k8s.io           false        SelfSubjectAcce***eview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectacce***eviews                           authorization.k8s.io           false        SubjectAcce***eview
horizontalpodautoscalers          hpa          autoscaling                    true         HorizontalPodAutoscaler
cronjobs                          cj           batch                          true         CronJob
jobs                                           batch                          true         Job
certificatesigningrequests        csr          certificates.k8s.io            false        CertificateSigningRequest
leases                                         coordination.k8s.io            true         Lease
events                            ev           events.k8s.io                  true         Event
daemonsets                        ds           extensions                     true         DaemonSet
deployments                       deploy       extensions                     true         Deployment
ingresses                         ing          extensions                     true         Ingress
networkpolicies                   netpol       extensions                     true         NetworkPolicy
podsecuritypolicies               psp          extensions                     false        PodSecurityPolicy
replicasets                       rs           extensions                     true         ReplicaSet
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
poddisruptionbudgets              pdb          policy                         true         PodDisruptionBudget
podsecuritypolicies               psp          policy                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
roles                                          rbac.authorization.k8s.io      true         Role
priorityclasses                   pc           scheduling.k8s.io              false        PriorityClass
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment

查看所有命名空间

#kubectl get ns
NAME          STATUS   AGE
default       Active   7d23h
kube-public   Active   7d23h
kube-system   Active   7d23h

查看所有命名空间的pod信息

#kubectl get pod --all-namespaces
NAMESPACE     NAME                                    READY   STATUS    RESTARTS   AGE
kube-system   coredns-779dfc4d59-6q6ks                1/1     Running   0          3d19h
kube-system   coredns-779dfc4d59-996fz                1/1     Running   0          3d19h
kube-system   coredns-779dfc4d59-gs8sm                1/1     Running   0          3d20h
kube-system   kubernetes-dashboard-66bddbb896-sjg92   1/1     Running   0          43h

查看某一命名空间的pod信息

#kubectl get pod -n kube-system -o wide
NAME                                    READY   STATUS    RESTARTS   AGE     IP           NODE          NOMINATED NODE
coredns-779dfc4d59-6q6ks                1/1     Running   0          3d19h   10.10.62.2   172.16.0.8    <none>
coredns-779dfc4d59-996fz                1/1     Running   0          3d19h   10.10.2.2    172.16.0.10   <none>
coredns-779dfc4d59-gs8sm                1/1     Running   0          3d20h   10.10.54.2   172.16.0.9    <none>
kubernetes-dashboard-66bddbb896-sjg92   1/1     Running   0          43h     10.10.62.3   172.16.0.8    <none>

查看某一命名空间的pod,svc,ep,secret信息

#kubectl get pod,svc,ep,secret -n kube-system -o wide
NAME                                        READY   STATUS    RESTARTS   AGE     IP           NODE          NOMINATED NODE
pod/coredns-779dfc4d59-6q6ks                1/1     Running   0          3d19h   10.10.62.2   172.16.0.8    <none>
pod/coredns-779dfc4d59-996fz                1/1     Running   0          3d19h   10.10.2.2    172.16.0.10   <none>
pod/coredns-779dfc4d59-gs8sm                1/1     Running   0          3d20h   10.10.54.2   172.16.0.9    <none>
pod/kubernetes-dashboard-66bddbb896-sjg92   1/1     Running   0          43h     10.10.62.3   172.16.0.8    <none>

NAME                           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE     SELECTOR
service/kube-dns               ClusterIP   169.169.0.2     <none>        53/UDP,53/TCP   3d20h   k8s-app=kube-dns
service/kubernetes-dashboard   NodePort    169.169.1.125   <none>        443:40487/TCP   43h     k8s-app=kubernetes-dashboard

NAME                                ENDPOINTS                                              AGE
endpoints/kube-controller-manager   <none>                                                 7d23h
endpoints/kube-dns                  10.10.2.2:53,10.10.54.2:53,10.10.62.2:53 + 3 more...   3d20h
endpoints/kube-scheduler            <none>                                                 7d23h
endpoints/kubernetes-dashboard      10.10.62.3:8443                                        43h

NAME                                      TYPE                                  DATA   AGE
secret/admin-token-wbjtp                  kubernetes.io/service-account-token   3      42h
secret/coredns-token-lxsg5                kubernetes.io/service-account-token   3      3d20h
secret/default-token-b4jq9                kubernetes.io/service-account-token   3      7d23h
secret/kubernetes-dashboard-certs         Opaque                                0      43h
secret/kubernetes-dashboard-key-holder    Opaque                                2      43h
secret/kubernetes-dashboard-token-tbkh5   kubernetes.io/service-account-token   3      43h

查看集群信息

#kubectl cluster-info
Kubernetes master is running at https://172.16.0.100:6443
CoreDNS is running at https://172.16.0.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kubernetes-dashboard is running at https://172.16.0.100:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

查看某一pod的详细信息

#kubectl describe pod/kubernetes-dashboard-66bddbb896-sjg92 -n kube-system 

查看某一pod的log日志信息

#kubectl logs pod/kubernetes-dashboard-66bddbb896-sjg92 -n kube-system -f
-f:和tail -f命令一样的意思,实时日志打印

查看所有已注册节点信息

#root@<cc_172.16.0.2|~/cfssl>:#kubectl  get nodes -o wide
NAME          STATUS   ROLES    AGE     VERSION        INTERNAL-IP   EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION               CONTAINER-RUNTIME
172.16.0.10   Ready    <none>   7d19h   v1.12.0-rc.2   172.16.0.10   <none>        CentOS Linux 7 (Core)   3.10.0-862.14.4.el7.x86_64   docker://1.13.1
172.16.0.8    Ready    <none>   7d21h   v1.12.0-rc.2   172.16.0.8    <none>        CentOS Linux 7 (Core)   3.10.0-862.14.4.el7.x86_64   docker://1.13.1
172.16.0.9    Ready    <none>   7d19h   v1.12.0-rc.2   172.16.0.9    <none>        CentOS Linux 7 (Core)   3.10.0-862.14.4.el7.x86_64   docker://1.13.1
-o: 显示详细信息

查看k8s所有服务端所有组件状态是否正常

#root@<cc_172.16.0.2|~/cfssl>:#kubectl get componentstatuses
NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok                   
etcd-2               Healthy   {"health": "true"}   
etcd-0               Healthy   {"health": "true"}   
etcd-1               Healthy   {"health": "true"}   
controller-manager   Healthy   ok 

扩展现有coredns的deployment

#root@<cc_172.16.0.2|~/cfssl>:#kubectl get  deploy -n kube-system
NAME                   DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
coredns                3         3         3            3           4d13h
kubernetes-dashboard   1         1         1            1           2d12h
再扩展相应部署
#root@<cc_172.16.0.2|~/cfssl>:#kubectl scale --replicas=3 deploy coredns -n kube-system

更新已创建的svc

找到相应的svc
#root@<cc_172.16.0.2|~/soft/kubernetes/cluster/addons/dashboard>:#kubectl get svc -n kube-system
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
kube-dns               ClusterIP   169.169.0.2     <none>        53/UDP,53/TCP   4d21h
kubernetes-dashboard   NodePort    169.169.1.125   <none>        443:40487/TCP   2d19h

编辑相应的svc
#root@<cc_172.16.0.2|~/soft/kubernetes/cluster/addons/dashboard>:#kubectl edit service  kubernetes-dashboard --namespace=kube-system
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2018-11-26T13:43:07Z
  labels:
    addonmanager.kubernetes.io/mode: Reconcile
    k8s-app: kubernetes-dashboard
    kubernetes.io/cluster-service: "true"
  name: kubernetes-dashboard
  namespace: kube-system
  resourceVersion: "827241"
  selfLink: /api/v1/namespaces/kube-system/services/kubernetes-dashboard
  uid: 350c790f-f181-11e8-94ca-5254f7a51592
spec:
  clusterIP: 169.169.1.125
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 40487  删除此行,保留前面-
    port: 443
    protocol: TCP
    targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard
  sessionAffinity: None
  type: NodePort  删除此行
status:
  loadBalancer: {}

注:edit命令允许你直接编辑使用命令行工具获取的任何资源,但如果编辑出错,你在保存会提示错误,如下:
并在/tmp目录下面生成一个临时文件,其实你并示修改成功

"/tmp/kubectl-edit-hfgsa.yaml" 33L, 999C written
A copy of your changes has been stored to "/tmp/kubectl-edit-hfgsa.yaml"
error: Edit cancelled, no valid changes were saved.

我现在要取消kubernetes-dashboard的NodePort

root@<cc_172.16.0.2|~/soft/kubernetes/cluster/addons/dashboard>:#kubectl get svc -n kube-system
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
kube-dns               ClusterIP   169.169.0.2     <none>        53/UDP,53/TCP   4d21h
kubernetes-dashboard   NodePort    169.169.1.125   <none>        443:40487/TCP   2d20h

正确做法是删除上面二行内容
nodePort: 40487   但这一行前面那个-要保留
type: NodePort  
然后保存,没有报警说明修改成功,然后再执行以下命令检查

#root@<cc_172.16.0.2|~/soft/kubernetes/cluster/addons/dashboard>:#kubectl get svc -n kube-system
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
kube-dns               ClusterIP   169.169.0.2     <none>        53/UDP,53/TCP   4d21h
kubernetes-dashboard   ClusterIP   169.169.1.125   <none>        443/TCP         2d20h   #type变为clusterip,nodepor也不见了,说明修改成功

查看pods所有标签

kubectl get pods --show-labels

根据标签查看pods

kubectl get pods -l app=nginx

滚动更新镜像

kubectl set image deployment/nginx-deployment nginx=nginx:1.11

或者

kubectl edit deployment/nginx-deployment

configmap配置

将文件定义为configmap用法

复制tomcatr 的二个配置 文件 到相应的目录下面
root@<cc_172.16.0.2|~/project/configmap/tomcat-link>:#ls
logging.properties  server.xml

执t行下面命令创建tomcat-link的configmap
root@<cc_172.16.0.2|~/project/configmap/tomcat-link>:#kubectl create configmap tomcat-link --from-file=serverxml=server.xml --from-file=loggingproperties=logging.properties  -n k8s-go
configmap/tomcat-link created
或者
root@<cc_172.16.0.2|~/project/configmap/tomcat-link>:#kubectl create configmap tomcat-link --from-file=logging.properties --from-file=server.xml 
configmap/tomcat-link created
或者
root@<cc_172.16.0.2|~/project/configmap>:#kubectl create configmap tomcat-link --from-file=tomcat-link
configmap/tomcat-link created

查看执行结果
root@<cc_172.16.0.2|~/project/configmap/tomcat-link>:#kubectl describe configmap tomcat-link |more
Name:         tomcat-link
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
loggingproperties:
----
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
省略........

serverxml:
----
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

将环境变量定义为configmap用法

创建方式1:
root@<cc_172.16.0.2|~/project/configmap/tomcat-link>:#kubectl create configmap tomcat-link-env --from-literal=loglevel=info --from-literal=appdatadir=/var/data -n k8s-go
configmap/tomcat-link-env created

创建方式2:
#vim tomcat-link-env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
   name: tomcat-link-env
data:
   apploglevel: info
     appdatadir: /var/data

#kubectl create -f tomcat-link-env.yaml

定义好configmap然后就要在deployment中使用
使用方式有二种:

通过环境变量获取confgimap中的内容
通过volume挂载方式将configmap中的内容挂载到容器里面

环境变量使用

在spec.containers段定义如下内容
envFrom:
- configMapRef:
  name: tomcat-link-env     这样会根据tomcat-link-env里面定义 的环境变量来自动生成所有环境就是

文件通过挂载方式使用

       volumeMounts:
        - name: tomcat-configmap
          mountPath: /usr/local/tomcat/conf/
      volumes:
        - name: tomcat-configmap
          configMap:
            name: tomcat-link-conf
            items:
            - key: server.xml
              path: server.xml
            - key: logging.properties
              path: logging.properties

转载于:https://blog.51cto.com/running/2323797

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值