当前位置 : 首页 » 文章分类 :  开发  »  Kubernetes/K8S-kubectl

Kubernetes/K8S-kubectl

Kubernetes/K8S kubectl 命令行工具使用笔记

kubectl reference docs (页面下面有所有 kubectl 子命令的 doc 链接)
https://kubernetes.io/docs/reference/kubectl/kubectl/


kubectl get 查看资源

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get

kubectl api-resources 可以查看 k8s 支持的所有资源类型。
常用的:

# kubectl api-resources -o wide
NAME                      SHORTNAMES   APIGROUP   NAMESPACED   KIND                   VERBS
configmaps                cm                      true         ConfigMap              [create delete deletecollection get list patch update watch]
namespaces                ns                      false        Namespace              [create delete get list patch update watch]
nodes                     no                      false        Node                   [create delete deletecollection get list patch update watch]
persistentvolumeclaims    pvc                     true         PersistentVolumeClaim  [create delete deletecollection get list patch update watch]
persistentvolumes         pv                      false        PersistentVolume       [create delete deletecollection get list patch update watch]
pods                      po                      true         Pod                    [create delete deletecollection get list patch update watch]
services                  svc                     true         Service                [create delete get list patch update watch]
deployments               deploy       apps       true         Deployment             [create delete deletecollection get list patch update watch]
replicasets               rs           apps       true         ReplicaSet             [create delete deletecollection get list patch update watch]
statefulsets              sts          apps       true         StatefulSet            [create delete deletecollection get list patch update watch]
serviceaccounts           sa                      true         ServiceAccount         [create delete deletecollection get list patch update watch]
secrets                                           true         Secret                 [create delete deletecollection get list patch update watch]
jobs                                   batch      true         Job                    [create delete deletecollection get list patch update watch]
ingresses                 ing          extensions true         Ingress                [create delete deletecollection get list patch update watch]

-o, --output='' 输出格式,常用的有:
-o wide 带额外信息的纯文本输出。
-o yaml 输出为 yaml 格式的 api 对象。
-o json 输出为 json 格式的 api 对象。


kubectl get cs 查看组件状态(v1.19后废弃)

kubectl get componentstatuskubectl get cs 查看 k8s 组件状态。

$ kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok
controller-manager   Healthy   ok
etcd-0               Healthy   {"health":"true"}

从 v1.19 开始, componentstatus API 被废弃。
componentstatus API 提供 etcd, kube-scheduler 和 kube-controller-manager 的状态信息,但只有在这些组件和 api-server 位于同一节点上时数据才是正确的。
https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG/CHANGELOG-1.19.md#deprecation


kubectl get nodes/node 查看节点信息

查看 k8s 集群中的节点信息

$ kubectl get nodes
NAME     STATUS     ROLES    AGE   VERSION
linode   NotReady   master   11d   v1.19.0

kubectl get node -o wide 查看node及IP

# kubectl get nodes -o wide
NAME    STATUS   ROLES    AGE   VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
node1   Ready    master   38d   v1.16.3   127.0.0.1        <none>        CentOS Linux 7 (Core)   4.17.11-1.el7.elrepo.x86_64   docker://19.3.12
node2   Ready    master   38d   v1.16.3   127.0.0.2        <none>        CentOS Linux 7 (Core)   4.17.11-1.el7.elrepo.x86_64   docker://19.3.12
node3   Ready    master   38d   v1.16.3   127.0.0.3        <none>        CentOS Linux 7 (Core)   4.17.11-1.el7.elrepo.x86_64   docker://19.3.12

kubectl get node –show-labels=true 查看节点的标签

# kubectl get node --show-labels=true
NAME      STATUS   ROLES         AGE   VERSION   LABELS
linode    Ready    master,node   22d   v1.12.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=linode,node-role.kubernetes.io/master=,node-role.kubernetes.io/node=

kubectl get service/svc 查看service

# kubectl get svc
NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                       AGE
kafka-alone-headless     ClusterIP   None            <none>        8092/TCP                      36d
kafka-service            ClusterIP   10.123.12.123   <none>        8092/TCP                      36d
kubernetes               ClusterIP   10.123.0.1      <none>        443/TCP                       38d
mongo-service            ClusterIP   10.123.12.123   <none>        8017/TCP                      2d13h
mysql-service            ClusterIP   10.123.12.12    <none>        8306/TCP                      21d
redis-service            ClusterIP   10.123.12.123   <none>        8179/TCP                      36d
zk-service               ClusterIP   10.123.12.123   <none>        8121/TCP                      2d13h
zookeeper-headless       ClusterIP   None            <none>        2181/TCP,3888/TCP,2888/TCP    2d13h
gateway-service          NodePort    10.123.123.12   <none>        8081:9081/TCP                 42h

kubectl get service -o wide 查看service及selector

# kubectl get svc -o wide
NAME                  type        cluster-ip      external-ip   port(s)        age     selector
da-service            nodeport    10.233.53.253   <none>        8768:8768/tcp  4m18s   app=da
kafka-alone-headless  clusterip   none            <none>        8092/tcp       36d     app.kubernetes.io/component=kafka-broker,app.kubernetes.io/instance=kafka-alone,app.kubernetes.io/name=kafka
kafka-service         clusterip   10.233.58.186   <none>        8092/tcp       36d     app.kubernetes.io/component=kafka-broker,app.kubernetes.io/instance=kafka-alone,app.kubernetes.io/name=kafka
kubernetes            clusterip   10.233.0.1      <none>        443/tcp        38d     <none>
mongo-service         clusterip   10.233.37.193   <none>        8017/tcp       2d15h   app=mongodb-ist
mysql-service         clusterip   10.233.58.99    <none>        8306/tcp       21d     app=mysql-alone-ist-ist
redis-service         clusterip   10.233.44.137   <none>        8179/tcp       36d     app=redis-ist
zk-service            clusterip   10.233.16.246   <none>        8121/tcp       2d15h   app=zookeeper-ist

kubectl get svc xx -o yaml 查看Service的yaml

# kubectl get svc myapp-service -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2021-07-14T03:02:07Z"
  name: myapp-service
  namespace: default
  resourceVersion: "39037191"
  selfLink: /api/v1/namespaces/default/services/myapp-service
  uid: e958ddd5-396d-4be9-926c-df05b50d8d5a
spec:
  clusterIP: 127.0.0.1
  externalTrafficPolicy: Cluster
  ports:
  - name: myapp
    nodePort: 9652
    port: 8652
    protocol: TCP
    targetPort: 8652
  selector:
    app: myapp
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

kubectl get pods/pod 查看pod

kubectl get pods 查看所有pod

kubectl get pods 查看所有 pod
kubectl get pods -A -A表示列出所有 namespace 的 pod

输出结果中 READY 1/2 表示: 已就绪容器个数/pod中总容器个数

kubectl get pod -o wide 查看pod及ip

# kubectl get pod -o wide
NAME                 READY   STATUS             RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
prometheus-mm27q     1/1     Running            0          27d     127.0.0.1       linode         <none>           <none>

kubectl get pods name 查看指定pod

kubectl get pods pod-name 查看指定pod

$ kubectl get pods
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-f9fd979d6-8l6f2          0/1     Pending   0          11d
kube-system   coredns-f9fd979d6-bmbnt          0/1     Pending   0          11d
kube-system   etcd-linode                      1/1     Running   0          11d
kube-system   kube-apiserver-linode            1/1     Running   0          11d
kube-system   kube-controller-manager-linode   1/1     Running   0          10d
kube-system   kube-proxy-mlls6                 1/1     Running   0          11d
kube-system   kube-scheduler-linode            1/1     Running   0          10d

kubectl get pod name -o yaml 看pod原始yaml

kubectl get pods pod-name -o yaml 查看指定 pod 的原始 yaml
pod-name 来自 kubectl get pods 的第一列


kubectl get secret 查看密钥

kubectl get secret 查看全部密钥

查看 namespace 下的 secret 密钥

# kubectl get secret
NAME                                                       TYPE                                  DATA   AGE
apimonitor-reader-token-d7bkh                              kubernetes.io/service-account-token   3      2d
kubeapps-operator-token-k7v8w                              kubernetes.io/service-account-token   3      2d
mysql-alone-ist                                            Opaque                                2      4h35m
prometheus-prometheus-operator-prometheus-tls-assets       Opaque                                0      2d
sh.helm.release.v1.mysql-alone.v1                          helm.sh/release.v1                    1      4h35m
vitess-operator-token-nn4m5                                kubernetes.io/service-account-token   3      9h

kubectl get secret xx -o yaml 查看密钥原始yaml

# kubectl get secret mysql-alone -o yaml
apiVersion: v1
data:
  mysql-password: NDxxxxxxxx0tYOQ==
  mysql-root-password: xxxxxxxxxxxx
kind: Secret
metadata:
  creationTimestamp: "2021-06-30T23:49:17Z"
  labels:
    app: mysql-alone
    chart: mysql-1.1.1
    heritage: Helm
    release: mysql-alone
  name: mysql-alone
  namespace: default
  resourceVersion: "374990"
  selfLink: /api/v1/namespaces/default/secrets/mysql-alone
  uid: fc66f625-3fb6-4957-99ee-9786af38d6b0
type: Opaque

kubectl get serviceaccounts/sa 查看服务账号

kubectl get serviceaccounts/sa 查看全部服务账号

查看 namespace 下的服务账号

# kubectl get serviceAccounts
NAME                                           SECRETS   AGE
apimonitor-reader                              1         2d
default                                        1         2d
kubeapps-operator                              1         2d
prometheus-operator-alertmanager               1         2d
prometheus-operator-grafana                    1         2d
prometheus-operator-grafana-test               1         2d
prometheus-operator-kube-state-metrics         1         2d
prometheus-operator-operator                   1         2d
prometheus-operator-prometheus                 1         2d
prometheus-operator-prometheus-adapter         1         2d
prometheus-operator-prometheus-node-exporter   1         2d
tianniu-default                                1         32h
vitess-operator                                1         9h

kubectl get serviceaccounts xx -o yaml 查看服务账号原始yaml

# kubectl get serviceaccounts vitess-operator -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"vitess-operator","namespace":"default"}}
  creationTimestamp: "2021-06-30T18:50:26Z"
  name: vitess-operator
  namespace: default
  resourceVersion: "332313"
  selfLink: /api/v1/namespaces/default/serviceaccounts/vitess-operator
  uid: 2868977f-cd8d-4a79-be2e-1cc77c248a23
secrets:
- name: vitess-operator-token-nn4m5

kubectl describe pod name 看pod配置和事件

kubectl describe pod pod-name 查看 Pod 的事件
pod-name 来自 kubectl get pods 的第一列

# kubectl describe pod my-app-deployment-5bf87ff7f9-gnsdb
Name:               my-app-deployment-5bf87ff7f9-gnsdb
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               linode/127.0.0.1
Start Time:         Sat, 20 Feb 2021 11:06:26 +0800
Labels:             app=my-app
                    pod-template-hash=5bf87ff7f9
Annotations:        podpreset.admission.kubernetes.io/podpreset-allow-tz-env: 1137
Status:             Running
IP:                 127.0.0.1
Controlled By:      ReplicaSet/my-app-deployment-5bf87ff7f9
Containers:
  my-app:
    Container ID:  docker://6ea436028db8d732523b395effad18efb2a1c0414de59d796c6f3dc06d9ee0cc
    Image:         masikkk.com/ist/my-app:20210220_1613789493178
    Image ID:      docker-pullable://docker.masikkk.com/my-app@sha256:6035a91620701696ecdb5e7dc638782c16c52be35540dac7db077a5fe9fadb53
    Port:          <none>
    Host Port:     <none>
    Command:
      python
    Args:
      /root/apps/start.py
      --cluster_configFilesParams="[{ \"key\": \"db.datasource.url\", \"value\": \"jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false\" }, { \"key\": \"db.datasource.username\", \"value\": \"haha\" }]"
    State:          Running
      Started:      Sat, 20 Feb 2021 11:06:45 +0800
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     24
      memory:  16Gi
    Requests:
      cpu:     100m
      memory:  1Gi
    Liveness:  http-get http://localhost:8080/ delay=30s timeout=60s period=15s #success=1 #failure=8
    Environment:
      TZ:  Asia/Shanghai
    Mounts:
      /etc/localtime:ro from ro (rw)
      /home/aicu-tob/logs/my-app from log (rw)
      /root/data from data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-tj2ln (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  data:
    Type:          HostPath (bare host directory volume)
    Path:          /home/aicu-tob/data
    HostPathType:
  log:
    Type:          HostPath (bare host directory volume)
    Path:          /home/aicu-tob/logs/my-app
    HostPathType:
  ro:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/localtime
    HostPathType:
  default-token-tj2ln:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-tj2ln
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

kubectl create 创建资源

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#create
从文件或标准输入创建资源,这里的资源可以是 Service, Pod, ConfigMap 等等。
kubectl create -f FILENAME

例如
kubectl create -f helm-rbac.yaml 根据配置文件 helm-rbac.yaml 创建资源。


kubectl apply 应用配置文件

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply
应用指定的配置文件,如果指定的资源不存在会自动创建,相当于 kubectl create
kubectl apply (-f FILENAME | -k DIRECTORY)

--record 记录这条 kubectl 命令到资源注解中,默认是 false 不记录。

例如
kubectl apply -f helm-rbac.yaml 根据配置文件创建资源


kubectl run 运行容器

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run
在 pod 中运行指定的镜像,类似 docker run


kubectl exec 在容器中执行命令

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#exec
在容器中执行命令,类似 docker exec

进入容器 kubectl exec -it pod-name sh
-c, --container='' 指定容器名,如果忽略此参数,自动选择 pod 中的第一个容器

1、查找consul的pod名

# kubectl get pods |grep consul
consul-alone-0                                                   1/1     Running   0          22d

2、进入consul容器 kubectl exec -it consul-alone-0 sh
输入完pod名前缀后按tab键可以动补全完整的pod名,很方便

kubectl exec -c 在pod中的指定容器中执行命令

对于包含多个容器的 pod, 可以通过 -c 容器名 参数指定目标容器。

1、kubectl describe pod xxx 可以看到 pod 中的容器列表,在 Containers 属性中列出。

2、kubectl exec -it pod-name -c container-name sh 在pod的指定容器中执行命令


kubectl scale 扩缩容

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#scale
扩容或缩容 Deployment, ReplicaSet, Replication Controller 或 StatefulSet 中的 Pod 数量。

scale 也可以指定多个前提条件,如:当前副本数量 --current-replicas 或 版本号 --resource-version 进行伸缩比例设置前,系统会先验证前提条件是否成立。

1、将 deployment myapp-deployment 中的 pod 个数扩容为 3 个
kubectl scale --replicas=3 deployment/myapp-deployment

2、如果 mysql 当前副本数为 2 则将其扩展至 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

3、同时将 deployment app1 和 app2 的副本数扩展为 2
kubectl scale --replicas=2 deploy/app1-deployment deploy/app2-deployment

4、将由 foo.yaml 配置文件中指定的资源对象和名称标识的 Pod 资源副本设为 3
kubectl scale --replicas=3 -f foo.yaml

Kubernetes kubectl scale 命令详解
http://docs.kubernetes.org.cn/664.html


kubectl label 给资源打标签

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#label
更新资源的标签。

kubectl label node master node-role.kubernetes.io/edge= 给 master node 打上 edge label


kubectl drain

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#drain

从 k8s 集群删除节点


kubectl delete 删除资源

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#delete

根据文件名、标签等删除资源

kubectl delete pod 删除pod

kubectl delete pod myapp-deployment-64f855cd65-7dhc4 删除指定pod,输入 myapp 后按 tab 键可自动补全 pod 名

kubectl delete -f xx.yaml 根据描述文件删除资源

kubectl delete -f 101_initial_cluster.yaml 根据描述文件 101_initial_cluster.yaml 删除资源,这是一个 CRD 自定义资源,是通过 kubectl apply -f 101_initial_cluster.yaml 命令安装的。


kubectl taint 给node加污点标记

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#taint

更新一个或多个 node 上的污点。
kubectl taint NODE NAME KEY_1=VAL_1:TAINT_EFFECT_1 ... KEY_N=VAL_N:TAINT_EFFECT_N

一个污点(Taints) 包括 key, value 和 effect(效果),形式为 key=value:effect
注意:
1、effect 目前只能是 NoSchedule, PreferNoSchedule, NoExecute 三者之一,不能是别的值。
2、目前污点只能应用于 node
3、value 是可选的,没有value也可以。

例如
kubectl taint nodes foo dedicated=special-user:NoSchedule 给节点 foo 增加一个污点,它的键名是 dedicated, 键值是 special-user, 效果是 NoSchedule。 这表示只有拥有和这个污点相匹配的容忍度的 Pod 才能够被分配到 foo 这个节点。
如果在节点 foo 上键是 dedicated, 效果是 NoSchedule 的污点已经存在,则其 value 被替换为 special-user

kubectl taint nodes foo dedicated:NoSchedule- 删除节点 foo 上键是 dedicated, 效果是 NoSchedule 的污点(如果有的话)。

kubectl taint nodes foo bar:NoSchedule 给节点 foo 加上一个键是 bar, 效果是 NoSchedule 的污点,此污点没有value。


kubectl logs 查看日志

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs
打印 pod 的日志

kubectl get pods 列出所有 pods
然后 kubectl logs --tail=20 pod-name 显示 pod-name 的最近 20 行日志。

kubectl logs -c 查看pod中指定容器的日志

-c, --container='' 指定容器名
例如
kubectl logs -f example-vttablet-zone1-2469782763-bfadd780 -c mysqld

对于包含多个容器的 pod, kubectl logs 命令必须用 -c 参数指定要查看的具体容器名,否则报如下错误:

# kubectl logs -f example-vttablet-zone1-2469782763-bfadd780
Error from server (BadRequest): a container name must be specified for pod example-vttablet-zone1-2469782763-bfadd780, choose one of: [vttablet mysqld mysqld-exporter] or one of the init containers: [init-vt-root init-mysql-socket]

可以看到错误提示中还会给列出各个容器的名字。


kubectl api-resources 列出支持的资源类型

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#api-resources

列出k8s支持的所欲api资源

kubectl api-resources -o wide 可以多打印出 VERBS 列,说明此资源支持的动词(create delete deletecollection get list patch update watch)

# 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
localsubjectaccessreviews                      authorization.k8s.io           true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           false        SubjectAccessReview
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
dynamicappconfigs                              dynamicapps.det.baidu.com      false        DynamicAppConfig
events                            ev           events.k8s.io                  true         Event
ingresses                         ing          extensions                     true         Ingress
nodes                                          metrics.k8s.io                 false        NodeMetrics
pods                                           metrics.k8s.io                 true         PodMetrics
alertmanagers                                  monitoring.coreos.com          true         Alertmanager
podmonitors                                    monitoring.coreos.com          true         PodMonitor
prometheuses                                   monitoring.coreos.com          true         Prometheus
prometheusrules                                monitoring.coreos.com          true         PrometheusRule
servicemonitors                                monitoring.coreos.com          true         ServiceMonitor
thanosrulers                                   monitoring.coreos.com          true         ThanosRuler
ingresses                         ing          networking.k8s.io              true         Ingress
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
runtimeclasses                                 node.k8s.io                    false        RuntimeClass
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
podpresets                                     settings.k8s.io                true         PodPreset
apimonitors                       am           stable.example.com             false        apiMonitor
csidrivers                                     storage.k8s.io                 false        CSIDriver
csinodes                                       storage.k8s.io                 false        CSINode
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment

kubectl cp 拷贝文件

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#cp

kubectl cp <file-spec-src> <file-spec-dest>

拷贝 mysql-alone pod 中的 /usr/lib/mysql/plugin/libcompare.so 到当前目录的 libcompare.so

kubectl cp mysql-alone:usr/lib/mysql/plugin/libcompare.so libcompare.so

kubectl proxy 创建代理

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#proxy

在宿主机 localhost 和 Kubernetes API Server 之间创建一个代理服务或应用级网关。
还可以在指定的 path 上做静态资源服务器。

kubectl proxy --port=8001 在端口 8001 上创建代理。


kubectl port-forward 端口转发

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#port-forward

kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]

将一个或多个本地端口转发到 pod, 这个命令需要 node 节点上安装了 socat 工具。

通过 资源类型/资源名 来选择 pod, 例如 deployment/mydeployment, 也可只指定资源名,默认资源类型是 pod.

如果有多个 pod 匹配筛选条件,会自动选择一个 pod, 如果选中的 pod 结束,转发 session 也会自动终止。

kubectl port-forward service/vtctld 15000 15999 & 监听本地 15000 和 15999 端口转发到指定 service 的相同端口。
kubectl port-forward pod/mypod 8888:5000 监听本地 8888 端口转发到指定 pod 的 5000 端口。


kubectl version 查看版本

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#version

kubectl version 可以看客户端及服务端的版本,其中 GitVersion 即为 k8s 的版本号。

kubectl version
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.7", GitCommit:"132a687512d7fb058d0f5890f07d4121b3f0a2e2", GitTreeState:"clean", BuildDate:"2021-05-12T12:32:49Z", GoVersion:"go1.15.12", Compiler:"gc", Platform:"linux/amd64"}

上一篇 Linux-YUM

下一篇 Kubernetes/K8S-安装部署

阅读
评论
4.1k
阅读预计21分钟
创建日期 2021-07-04
修改日期 2021-07-04
类别

页面信息

location:
protocol:
host:
hostname:
origin:
pathname:
href:
document:
referrer:
navigator:
platform:
userAgent:

评论