kubectl是什么

kubernetes API是管理其各种资源对象的唯一入口,它提供了一个RESTful风格的CRUD(create,read,Update,delete)接口用于查询和修改集群状态,并且将结果存储于etcd中,API Server也是用于更新ETCD中资源对象的唯一途径,整个集群的其他所有组件都需要通过它来完成查询和修改操作,kubectl的核心功能在于通过API Server操作k8s的各种资源对象,它支持三种操作方式,其中命令是的使用最为简单,是了解K8S集群管理的一种有效途径

kubectl常用的命令

命令语法格式

  • kubectl [command] [TYPE] [NAME] [flags]
  • 上面的命令是: kubectl命令行中,指定执行什么操作(command),指定什么类型资源对象(type),指定此类型的资源对象名称(name),指定可选参数(flags),后面的参数就是为了修饰那个唯一的对象
  • 属于典型的英文语法,比如你是老师,你说,小明(kubectl)买(command)方便面(type)老坛酸菜方便面(name)桶装的(flag)。命令的根本原则是准确性,不能有异议!

查看集群信息

  • kubectl cluster-info
  1. [root@k8s-master01 nginx]# kubectl cluster-info
  2. Kubernetes master is running at https://172.18.15.116:6443
  3. CoreDNS is running at https://172.18.15.116:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  4. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
  5. [root@k8s-master01 nginx]#

查看集群版本信息

  • kubectl version
  • kubectl version —short=tre
    其中第二条命令能更精简的显示集群版本信息
  1. [root@k8s-master01 nginx]# kubectl version
  2. Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:44:51Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
  3. Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:36:15Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
  4. [root@k8s-master01 nginx]# kubectl version --short=true
  5. Client Version: v1.16.9
  6. Server Version: v1.16.9
  7. [root@k8s-master01 nginx]#

查看集群上运行了哪些资源

  • 语法格式 kubectl get - “-“表示代表资源名称,其中资源名称后面可用跟上选项”-o wide”表示可用查看一些具体的信息
  • 可以加上-n选项跟上pod所在的名称空间,如果不加则默认只会打印出default名称空间里面的Pod
  • 也可以加上—all-namespaces选项打印出所有名称空间的pod资源信息

列出有哪些Pod资源

  • -n选项可以打印出你指定的名称空间的资源
  • —all-namespaces可以打印出所有的名称的资源
    kubectl get pods
  1. [root@k8s-master01 nginx]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. busybox 1/1 Running 0 31m
  4. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d
  5. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d
  6. [root@k8s-master01 nginx]# kubectl get pods -o wide
  7. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  8. busybox 1/1 Running 0 31m 10.244.59.2 172.18.15.113 <none> <none>
  9. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d 10.244.38.3 172.18.15.114 <none> <none>
  10. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d 10.244.38.7 172.18.15.114 <none> <none>
  11. [root@k8s-master01 nginx]#
  12. #打印出所有名称空间的Pod资源信息
  13. [root@k8s-master01 nginx]# kubectl get pods --all-namespaces
  14. NAMESPACE NAME READY STATUS RESTARTS AGE
  15. default my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  16. default my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  17. kube-system coredns-59c6ddbf5d-bdr92 1/1 Running 0 2d3h
  18. [root@k8s-master01 nginx]#
  19. #只打印出kube-system名称空间的Pod
  20. [root@k8s-master01 nginx]# kubectl get pods -n kube-system
  21. NAME READY STATUS RESTARTS AGE
  22. coredns-59c6ddbf5d-bdr92 1/1 Running 0 2d3h
  23. [root@k8s-master01 nginx]#

列出有哪些deployment资源,默认也是打印出default名称空间的deployment资源

  • -n选项可以打印出你指定的名称空间的资源
  • —all-namespaces可以打印出所有的名称的资源
    kubectl get deployment
  1. [root@k8s-master01 nginx]# kubectl get deployment
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. my-nginx 2/2 2 2 2d2h
  4. [root@k8s-master01 nginx]#
  5. [root@k8s-master01 nginx]# kubectl get deployment -o wide
  6. NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
  7. my-nginx 2/2 2 2 2d2h my-nginx daocloud.io/library/nginx:1.13.0-alpine app=my-nginx
  8. [root@k8s-master01 nginx]#
  9. #打印出所有的名称空间的deployment
  10. [root@k8s-master01 nginx]# kubectl get deployment --all-namespaces
  11. NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
  12. default my-nginx 2/2 2 2 2d3h
  13. kube-system coredns 1/1 1 1 2d3h
  14. [root@k8s-master01 nginx]#

列出有哪些Service资源,默认也是打印出default名称空间的

  • -n选项可以打印出你指定的名称空间的资源
  • —all-namespaces可以打印出所有的名称的资源
    kubectl get svc
  1. [root@k8s-master01 nginx]# kubectl get svc
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
  4. my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d2h
  5. [root@k8s-master01 nginx]#
  6. [root@k8s-master01 nginx]# kubectl get svc -o wide
  7. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  8. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>
  9. my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d2h app=my-nginx
  10. [root@k8s-master01 nginx]#
  11. #只打印出kube-system名称空间的资源
  12. [root@k8s-master01 nginx]# kubectl get svc -n kube-system
  13. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  14. kube-dns ClusterIP 10.99.110.110 <none> 53/UDP,53/TCP,9153/TCP 2d3h
  15. [root@k8s-master01 nginx]#
  16. #打印出所有名称空间的资源
  17. [root@k8s-master01 nginx]# kubectl get svc --all-namespaces
  18. NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  19. default kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
  20. default my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
  21. kube-system kube-dns ClusterIP 10.99.110.110 <none> 53/UDP,53/TCP,9153/TCP 2d3h
  22. [root@k8s-master01 nginx]#

列出集群上面有哪些名称空间 kubectl get ns

  1. [root@k8s-master01 nginx]# kubectl get ns
  2. NAME STATUS AGE
  3. default Active 8d
  4. kube-node-lease Active 8d
  5. kube-public Active 8d
  6. kube-system Active 8d
  7. [root@k8s-master01 nginx]#

查看某一个资源更详细的信息

  • 语法格式:kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [options]

查看某一个Pod资源的详细信息 kubectl describe pod podname

  1. [root@k8s-master01 nginx]# kubectl describe pod my-nginx-854bbd7557-phvsl
  2. Name: my-nginx-854bbd7557-phvsl
  3. Namespace: default
  4. Priority: 0
  5. Node: 172.18.15.114/172.18.15.114
  6. Start Time: Mon, 01 Jun 2020 10:58:41 +0800
  7. Labels: app=my-nginx
  8. pod-template-hash=854bbd7557
  9. Annotations: <none>
  10. Status: Running
  11. IP: 10.244.38.3
  12. IPs:
  13. IP: 10.244.38.3
  14. Controlled By: ReplicaSet/my-nginx-854bbd7557
  15. Containers:
  16. my-nginx:
  17. Container ID: docker://56d6ff87faf07a332dcf625b86301946c6f7ef956634ea1f0bf147767af6df7b
  18. Image: daocloud.io/library/nginx:1.13.0-alpine
  19. Image ID: docker-pullable://daocloud.io/library/nginx@sha256:5c36f962c506c379bd63884976489c9c5e700c1496a6e8ea13dc404b1d258f76
  20. Port: 80/TCP
  21. Host Port: 0/TCP
  22. State: Running
  23. Started: Mon, 01 Jun 2020 10:58:44 +0800
  24. Ready: True
  25. Restart Count: 0
  26. Environment: <none>
  27. Mounts:
  28. /var/run/secrets/kubernetes.io/serviceaccount from default-token-gvn55 (ro)
  29. Conditions:
  30. Type Status
  31. Initialized True
  32. Ready True
  33. ContainersReady True
  34. PodScheduled True
  35. Volumes:
  36. default-token-gvn55:
  37. Type: Secret (a volume populated by a Secret)
  38. SecretName: default-token-gvn55
  39. Optional: false
  40. QoS Class: BestEffort
  41. Node-Selectors: <none>
  42. Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
  43. node.kubernetes.io/unreachable:NoExecute for 300s
  44. Events: <none>
  45. [root@k8s-master01 nginx]#

查看某一个service资源的详细信息,如果不加Service资源明,则默认会把所有的service资源都显示出来 kubectl describe svc servicename

  1. [root@k8s-master01 nginx]# kubectl describe svc
  2. Name: kubernetes
  3. Namespace: default
  4. Labels: component=apiserver
  5. provider=kubernetes
  6. Annotations: <none>
  7. Selector: <none>
  8. Type: ClusterIP
  9. IP: 10.99.0.1
  10. Port: https 443/TCP
  11. TargetPort: 6443/TCP
  12. Endpoints: 172.18.15.116:6443
  13. Session Affinity: None
  14. Events: <none>
  15. Name: my-nginx
  16. Namespace: default
  17. Labels: app=my-nginx
  18. Annotations: kubectl.kubernetes.io/last-applied-configuration:
  19. {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...
  20. Selector: app=my-nginx
  21. Type: NodePort
  22. IP: 10.99.44.234
  23. Port: http 80/TCP
  24. TargetPort: 80/TCP
  25. NodePort: http 13359/TCP
  26. Endpoints: 10.244.38.3:80,10.244.38.7:80
  27. Session Affinity: None
  28. External Traffic Policy: Cluster
  29. Events: <none>
  30. [root@k8s-master01 nginx]#
  31. # 加资源命访问
  32. [root@k8s-master01 nginx]# kubectl describe svc my-nginx
  33. Name: my-nginx
  34. Namespace: default
  35. Labels: app=my-nginx
  36. Annotations: kubectl.kubernetes.io/last-applied-configuration:
  37. {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...
  38. Selector: app=my-nginx
  39. Type: NodePort
  40. IP: 10.99.44.234
  41. Port: http 80/TCP
  42. TargetPort: 80/TCP
  43. NodePort: http 13359/TCP
  44. Endpoints: 10.244.38.3:80,10.244.38.7:80
  45. Session Affinity: None
  46. External Traffic Policy: Cluster
  47. Events: <none>
  48. [root@k8s-master01 nginx]#

打印出Pod容器的日志,默认就是Pod类型

  • 语法格式:kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
  1. [root@k8s-master01 nginx]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. busybox 1/1 Running 0 164m
  4. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  5. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  6. [root@k8s-master01 nginx]# kubectl logs my-nginx-854bbd7557-phvsl
  7. 10.244.38.1 - - [02/Jun/2020:03:32:22 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
  8. [root@k8s-master01 nginx]#

如何进入到Pod的容器

  • 语法格式:kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] — COMMAND [args…] [options]
  1. [root@k8s-master01 nginx]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. busybox 1/1 Running 0 167m
  4. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  5. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  6. [root@k8s-master01 nginx]# kubectl exec -ti my-nginx-854bbd7557-phvsl /bin/sh
  7. / #
  8. / #
  9. / #

如何删除一个Pod,删除的方法有多种,通常我们应该是怎么创建的Pod就怎么删除,因为Pod有两种运行方式,一种是基于命令创建的,一种是基于配置清单创建的,但是结果都是需要使用kubectl命令来删除,只是使用的选项不同而已,

  • 语法格式:kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | —all)]) [options]

命令式创建的Pod删除方法,因为命令创建的Pod通常是没有配置文件清单的,所以需要先get出来需要删除的Pod

  • kubectl delete Podname
  1. [root@k8s-master01 nginx]# kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. busybox 1/1 Running 0 171m
  4. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  5. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  6. [root@k8s-master01 nginx]#
  7. [root@k8s-master01 nginx]# kubectl delete pod busybox
  8. pod "busybox" deleted
  9. [root@k8s-master01 nginx]#
  10. [root@k8s-master01 nginx]# kubectl get pods
  11. NAME READY STATUS RESTARTS AGE
  12. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  13. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  14. [root@k8s-master01 nginx]#

配置清单式的Pod删除方法,这种方法删除Pod非常简单,怎么创建的就怎么删除,只需要把apply或者create换成delete

  1. #我们先使用配置清单创建出一个基于配置清单的Pod
  2. [root@k8s-master01 nginx]# kubectl apply -f busybox.yml
  3. pod/busybox created
  4. [root@k8s-master01 nginx]# kubectl get pods
  5. NAME READY STATUS RESTARTS AGE
  6. busybox 0/1 ContainerCreating 0 5s
  7. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  8. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  9. [root@k8s-master01 nginx]# kubectl get pods
  10. NAME READY STATUS RESTARTS AGE
  11. busybox 1/1 Running 0 10s
  12. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  13. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  14. [root@k8s-master01 nginx]#
  15. #删除基于配置清单的Pod
  16. [root@k8s-master01 nginx]# kubectl delete -f busybox.yml
  17. pod "busybox" deleted
  18. [root@k8s-master01 nginx]# kubectl get pods
  19. NAME READY STATUS RESTARTS AGE
  20. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  21. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  22. [root@k8s-master01 nginx]#

创建资源对象

  • 语法格式:kubectl run NAME —image=image [—env=”key=value”] [—port=port] [—replicas=replicas] [—dry-run=bool] [—overrides=inline-json] [—command] — [COMMAND] [args…] [options]
  • 创建资源对象有run,create,apply等命令
  • 直接通过kubectl run命令及相关选项创建的资源对象被称为直接命令式操作
  • 用户也可以直接根据配置清单来创建资源对象
    下面是基于命令式创建一个名称为web的deployment和一个名称为web-svc的Service,镜像使用的是Nginx1.13的alpine版本,并且创建了5个副本的示例
  1. [root@k8s-master01 nginx]# kubectl run web --image=nginx:1.13.0-alpine --replicas=5
  2. kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
  3. deployment.apps/web created
  4. [root@k8s-master01 nginx]#
  5. [root@k8s-master01 nginx]# kubectl get pods -o wide
  6. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  7. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h 10.244.38.3 172.18.15.114 <none> <none>
  8. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h 10.244.38.7 172.18.15.114 <none> <none>
  9. web-6ff4758d4d-5zkpb 1/1 Running 0 47s 10.244.38.4 172.18.15.114 <none> <none>
  10. web-6ff4758d4d-666wt 1/1 Running 0 47s 10.244.59.4 172.18.15.113 <none> <none>
  11. web-6ff4758d4d-n8plp 1/1 Running 0 47s 10.244.59.2 172.18.15.113 <none> <none>
  12. web-6ff4758d4d-qk4mv 1/1 Running 0 47s 10.244.59.3 172.18.15.113 <none> <none>
  13. web-6ff4758d4d-vmmbc 1/1 Running 0 47s 10.244.38.5 172.18.15.114 <none> <none>
  14. [root@k8s-master01 nginx]#
  15. #创建一个名称为web-svc的Service资源,并且将web这个deployment里面的Pod资源包含进去
  16. [root@k8s-master01 nginx]# kubectl expose deployment/web --name=web-svc --port=80
  17. service/web-svc exposed
  18. [root@k8s-master01 nginx]#
  19. [root@k8s-master01 nginx]# kubectl get svc
  20. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  21. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
  22. my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
  23. web-svc ClusterIP 10.99.221.103 <none> 80/TCP 72s
  24. [root@k8s-master01 nginx]#
  25. #查看一下之前创建的5个Pod是否被包含到了这个Service资源里面
  26. [root@k8s-master01 nginx]# kubectl describe svc web-svc
  27. Name: web-svc
  28. Namespace: default
  29. Labels: run=web
  30. Annotations: <none>
  31. Selector: run=web
  32. Type: ClusterIP
  33. IP: 10.99.221.103
  34. Port: <unset> 80/TCP
  35. TargetPort: 80/TCP
  36. Endpoints: 10.244.38.4:80,10.244.38.5:80,10.244.59.2:80 + 2 more...
  37. Session Affinity: None
  38. Events: <none>
  39. [root@k8s-master01 nginx]#
  40. #删除,因为我们没有配置清单,所以这里删除就比较烦,如果你的操作步骤多了,很有可能自己也会忘记自己创建的对象名称叫什么
  41. #先删除Pod相关资源步骤:
  42. #1.找出刚刚创建的deployment
  43. [root@k8s-master01 nginx]# kubectl get deployment
  44. NAME READY UP-TO-DATE AVAILABLE AGE
  45. my-nginx 2/2 2 2 2d3h
  46. web 5/5 5 5 15m
  47. [root@k8s-master01 nginx]#
  48. #2.删除这个deployment
  49. [root@k8s-master01 nginx]# kubectl delete deployment web
  50. deployment.apps "web" deleted
  51. [root@k8s-master01 nginx]#
  52. #查看Pod,可以看到Pod资源已经是Terminating状态,虽然我这里比较方便,但是生产环境可能会有很多类似的资源,查找起来就没有这么方便了,所以推荐使用配置清单的方式去创建相关资源
  53. [root@k8s-master01 nginx]# kubectl get pods
  54. NAME READY STATUS RESTARTS AGE
  55. my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
  56. my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
  57. web-6ff4758d4d-5zkpb 0/1 Terminating 0 16m
  58. [root@k8s-master01 nginx]#
  59. #删除service
  60. [root@k8s-master01 nginx]# kubectl get svc
  61. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  62. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
  63. my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
  64. web-svc ClusterIP 10.99.221.103 <none> 80/TCP 15m
  65. [root@k8s-master01 nginx]#
  66. [root@k8s-master01 nginx]# kubectl delete svc web-svc
  67. service "web-svc" deleted
  68. [root@k8s-master01 nginx]# kubectl get svc
  69. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  70. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
  71. my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
  72. [root@k8s-master01 nginx]#

基于配置清单创建出一个名称为my-nginx的deployment,里面运行了5个副本,并且再创建出一个名称为my-nginx的Service资源

  1. [root@k8s-master01 nginx]# cat my-nginx.yml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-nginx
  6. spec:
  7. replicas: 5
  8. selector:
  9. matchLabels:
  10. app: my-nginx
  11. template:
  12. metadata:
  13. labels:
  14. app: my-nginx
  15. spec:
  16. containers:
  17. - name: my-nginx
  18. image: daocloud.io/library/nginx:1.13.0-alpine
  19. ports:
  20. - containerPort: 80
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: my-nginx
  26. labels:
  27. app: my-nginx
  28. spec:
  29. type: NodePort
  30. selector:
  31. app: my-nginx
  32. ports:
  33. - name: http
  34. port: 80
  35. targetPort: 80
  36. [root@k8s-master01 nginx]#
  37. [root@k8s-master01 nginx]# kubectl apply -f my-nginx.yml
  38. deployment.apps/my-nginx created
  39. service/my-nginx created
  40. [root@k8s-master01 nginx]#
  41. [root@k8s-master01 nginx]# kubectl get pods -o wide
  42. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  43. my-nginx-854bbd7557-9c9tq 1/1 Running 0 23s 10.244.59.2 172.18.15.113 <none> <none>
  44. my-nginx-854bbd7557-rvtkb 1/1 Running 0 23s 10.244.38.5 172.18.15.114 <none> <none>
  45. my-nginx-854bbd7557-w676k 1/1 Running 0 23s 10.244.38.3 172.18.15.114 <none> <none>
  46. my-nginx-854bbd7557-wbvjf 1/1 Running 0 23s 10.244.38.4 172.18.15.114 <none> <none>
  47. my-nginx-854bbd7557-wfx8k 1/1 Running 0 23s 10.244.59.3 172.18.15.113 <none> <none>
  48. [root@k8s-master01 nginx]#
  49. [root@k8s-master01 nginx]# kubectl get svc -o wide
  50. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  51. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>
  52. my-nginx NodePort 10.99.238.73 <none> 80:21954/TCP 43s app=my-nginx
  53. [root@k8s-master01 nginx]#
  54. [root@k8s-master01 nginx]# kubectl get svc -o wide
  55. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  56. kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>
  57. my-nginx NodePort 10.99.238.73 <none> 80:21954/TCP 43s app=my-nginx
  58. [root@k8s-master01 nginx]# kubectl describe svc my-nginx
  59. Name: my-nginx
  60. Namespace: default
  61. Labels: app=my-nginx
  62. Annotations: kubectl.kubernetes.io/last-applied-configuration:
  63. {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...
  64. Selector: app=my-nginx
  65. Type: NodePort
  66. IP: 10.99.238.73
  67. Port: http 80/TCP
  68. TargetPort: 80/TCP
  69. NodePort: http 21954/TCP
  70. Endpoints: 10.244.38.3:80,10.244.38.4:80,10.244.38.5:80 + 2 more...
  71. Session Affinity: None
  72. External Traffic Policy: Cluster
  73. Events: <none>
  74. [root@k8s-master01 nginx]#
  75. 关于如何删除前面有些,可以参考

小结

  • 每个子命令下面还有众多的子命令,大家可以去网上找相关的资料查看,这里写的常用的也并不全,这里写的只是告诉大家一个基本的用法,使用的是实话可以使用kubectl Type —help来获取相关的帮助信息,一定要多动手去敲一敲才能深刻的理解