k8s CLI 操作笔记

落花浮王杯 提交于 2020-01-26 00:18:17

k8s CLI 操作笔记

  1. k8s 获取所有的命名空间 namspace 可简写为 ns
[root@vic-master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   32d
development       Active   31d
kube-node-lease   Active   32d
kube-public       Active   32d
kube-system       Active   32d
  1. 查看所有的节点 node 可简写为 no
[root@vic-master ~]# kubectl get no
NAME         STATUS   ROLES    AGE   VERSION
vic-master   Ready    master   33d   v1.14.0
vic-node1    Ready    <none>   32d   v1.14.0
vic-node2    Ready    <none>   32d   v1.14.0
  1. 查询 pod , pod 简写为 po , 这里默认的命名空间的 default
[root@vic-master ~]# kubectl get po  
NAME                      READY   STATUS    RESTARTS   AGE  
frontend                  1/1     Running   0          31d  
myapp-54fc6bbfbd-f6cpk    1/1     Running   0          20h  
myapp-54fc6bbfbd-zmb2v    1/1     Running   0          20h  
ng-dep-679f4c7695-mw62d   1/1     Running   0          21h  
redis-php                 2/2     Running   0          30d
  1. 查询指定命名空间的 pod. 获取命名空间 kube-system 下的所有 pod
[root@vic-master ~]# kubectl get po -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-8686dcc4fd-gztm2             1/1     Running   1          33d
coredns-8686dcc4fd-xchp6             1/1     Running   1          33d
etcd-vic-master                      1/1     Running   0          33d
kube-apiserver-vic-master            1/1     Running   0          33d
kube-controller-manager-vic-master   1/1     Running   9          33d
kube-flannel-ds-amd64-6v4wb          1/1     Running   0          32d
kube-flannel-ds-amd64-nxw8v          1/1     Running   0          32d
kube-flannel-ds-amd64-xplm2          1/1     Running   0          32d
kube-proxy-fr2lv                     1/1     Running   0          32d
kube-proxy-m7qn9                     1/1     Running   0          33d
kube-proxy-tjwbw                     1/1     Running   0          32d
kube-scheduler-vic-master            1/1     Running   8          33d

  1. 查询所有资源
[root@vic-master ~]# kubectl get all
NAME                          READY   STATUS    RESTARTS   AGE
pod/frontend                  1/1     Running   0          31d
pod/myapp-54fc6bbfbd-f6cpk    1/1     Running   0          20h
pod/myapp-54fc6bbfbd-zmb2v    1/1     Running   0          21h
pod/ng-dep-679f4c7695-mw62d   1/1     Running   0          21h
pod/redis-php                 2/2     Running   0          30d

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        33d
service/myapp        NodePort    10.1.197.115   <none>        80:31611/TCP   20h
service/ng-dep       ClusterIP   10.1.115.230   <none>        80/TCP         21h

NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp    2/2     2            2           21h
deployment.apps/ng-dep   1/1     1            1           21h

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-54fc6bbfbd    2         2         2       21h
replicaset.apps/ng-dep-679f4c7695   1         1         1       21h
  1. 获取 service svc 简写, 类似获取 pod,同样可以用 -n <namespace> 来获取指定命名空间下的 service, --all-namepsaces 获取所有命名空间下的 service。
[root@vic-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        33d
myapp        NodePort    10.1.197.115   <none>        80:31611/TCP   20h
ng-dep       ClusterIP   10.1.115.230   <none>        80/TCP         21h
[root@vic-master ~]# kubectl get svc -n default
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        33d
myapp        NodePort    10.1.197.115   <none>        80:31611/TCP   20h
ng-dep       ClusterIP   10.1.115.230   <none>        80/TCP         21h
[root@vic-master ~]# kubectl get svc --all-namespaces
NAMESPACE     NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP                  33d
default       myapp        NodePort    10.1.197.115   <none>        80:31611/TCP             20h
default       ng-dep       ClusterIP   10.1.115.230   <none>        80/TCP                   21h
kube-system   kube-dns     ClusterIP   10.1.0.10      <none>        53/UDP,53/TCP,9153/TCP   33d

创建一个可访问的 Web 应用

  1. 创建一个 deloyment, vicccc/myapp 是我的 Web 镜像,访问 8080 端口会 返回 hello world
[root@vic-master ~]# kubectl create deploy myapp --image=vicccc/myapp
deployment.apps/myapp created

查看 pod 状态 myapp 的状态是 running

[root@vic-master ~]# kubectl get po
NAME                      READY   STATUS    RESTARTS   AGE
frontend                  1/1     Running   0          31d
myapp-688777bd55-nnvws    1/1     Running   0          7m47s
ng-dep-679f4c7695-mw62d   1/1     Running   0          23h
redis-php                 2/2     Running   0          31d
  1. 查看 pod 的详细信息
[root@vic-master ~]# kubectl get po -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
frontend                  1/1     Running   0          31d   10.244.2.2   vic-node2   <none>           <none>
myapp-688777bd55-nnvws    1/1     Running   0          10m   10.244.2.8   vic-node2   <none>           <none>
ng-dep-679f4c7695-mw62d   1/1     Running   0          23h   10.244.2.6   vic-node2   <none>           <none>
redis-php                 2/2     Running   0          31d   10.244.1.3   vic-node1   <none>           <none>
  1. 看到 myapp pod 的 ip 地址为 10.244.2.8 ,直接用 curl 访问 8080 端口,正常返回 hello world
[root@vic-master ~]# curl 10.244.2.8:8080
hello world
  1. 当然这个地址只能在本地主机能访问,需要外部机器访问的话需要创建 service, 只要 service name 和 deployment 同名 k8s 就能将两个关联起来
[root@vic-master ~]# kubectl create svc nodeport myapp --tcp=8080:8080
service/myapp created

查看 service 看到 8080 端口被映射到了 31540 端口, 通过本地主机的 IP 加端口便可以访问了。

[root@vic-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP          33d
myapp        NodePort    10.1.150.3     <none>        8080:31540/TCP   36s
ng-dep       ClusterIP   10.1.115.230   <none>        80/TCP           23h

我的机器的 ip 为 8.16.0.39 , 当然局域网中的主机 通过浏览器等 访问 8.16.0.39:31540 端口也可以获取对应结果

[root@vic-master ~]# curl 8.16.0.39:31540
hello world[root@vic-master ~]#
  1. 可以用 YAML 的形式查看资源
[root@vic-master ~]# kubectl get deploy myapp -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2020-01-16T08:02:20Z"
  generation: 1
  labels:
    app: myapp
  name: myapp
  namespace: default
  resourceVersion: "4133751"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/myapp
  uid: 85216899-3836-11ea-b983-000c291cf287
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: myapp
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: myapp
    spec:
      containers:
      - image: vicccc/myapp
        imagePullPolicy: Always
        name: myapp
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-01-16T08:02:37Z"
    lastUpdateTime: "2020-01-16T08:02:37Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-01-16T08:02:20Z"
    lastUpdateTime: "2020-01-16T08:02:37Z"
    message: ReplicaSet "myapp-688777bd55" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
  1. 扩缩容, 扩展为 3 个副本
[root@vic-master ~]# kubectl scale deploy/myapp --replicas=3
deployment.extensions/myapp scaled
[root@vic-master ~]#
[root@vic-master ~]# kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
myapp    1/3     3            1           79m
ng-dep   1/1     1            1           24h
[root@vic-master ~]# kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
myapp    3/3     3            3           80m
ng-dep   1/1     1            1           24h

其他

  1. 查看当前 k8s 支持的所有 API
[root@vic-master ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
  1. 查看当前 k8s 支持的所有资源
[root@vic-master ~]# 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
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
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
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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!