kubectl apply_kubectl 命令简单备录

kubectl controls the Kubernetes cluster manager.

首先要理解,下面几个概念命令,kubectl命令基本也是针对这些概念而言的。

node

pods

services

///

kubectl describe 描述资源对象

kubectl describe nodes # 显示Node的详细信息

kubectl describe pods # 显示Pod的详细信息

首先 下面2个命令默认结果一致

kubectl describe nodes

kubectl describe node

页面中,也仅仅只有一个node docker-desktop

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/node?namespace=_all

   ➜  ~ kubectl describe nodeName:               docker-desktop这个名字是第一行的输出后翻可以看到namespace

kubectl describe node docker-desktop

这个输出很详细,有Namespace输出信息

kubernetes-dashboard

kube-system

个人觉得kubectl describe node比kubectl get nodes获取的信息更多一些

➜  ~ kubectl get nodes docker-desktopNAME             STATUS   ROLES    AGE   VERSIONdocker-desktop   Ready    master   17h   v1.18.8kubectl get 显示一个或更多resources资源kubectl get cs                          # 查看集群状态kubectl get nodes                       # 查看集群节点信息kubectl get ns                          # 查看集群命名空间kubectl get svc -n kube-system          # 查看指定命名空间的服务kubectl get pod  -o wide      # 查看Pod详细信息kubectl get pod  -o yaml      # 以yaml格式查看Pod详细信息kubectl get pods                        # 查看资源对象,查看所有Pod列表kubectl get rc,service                  # 查看资源对象,查看rc和service列表kubectl get pod,svc,ep --show-labels    # 查看pod,svc,ep能及标签信息kubectl get all --all-namespaces        # 查看所有的namespaces

kubectl clster-info 显示集群信息

➜  ~ kubectl cluster-infoKubernetes master is running at https://kubernetes.docker.internal:6443KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'

查看创建的pod

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/pod?namespace=_all

查看所有 pod 列表, -n 后跟 namespace, 查看指定的命名空间

通过界面能看到有2个namespace

kubernetes-dashboard

kube-system

Examples:  # List all pods in ps output format.  kubectl get pods➜  ~ kubectl get pods -n kubernetes-dashboardNAME                                         READY   STATUS    RESTARTS   AGEdashboard-metrics-scraper-694557449d-7h8k6   1/1     Running   0          16hkubernetes-dashboard-7d9ddf9f8f-wprd7        1/1     Running   0          16h➜  ~ kubectl get pods -n kube-systemNAME                                     READY   STATUS    RESTARTS   AGEcoredns-66bff467f8-5qhsc                 1/1     Running   0          17hcoredns-66bff467f8-wdzmj                 1/1     Running   0          17hetcd-docker-desktop                      1/1     Running   0          17hkube-apiserver-docker-desktop            1/1     Running   5          17hkube-controller-manager-docker-desktop   1/1     Running   5          17hkube-proxy-fj96d                         1/1     Running   0          17hkube-scheduler-docker-desktop            1/1     Running   0          17hstorage-provisioner                      1/1     Running   0          17hvpnkit-controller                        1/1     Running   0          17h➜  ~ kubectl get pods -n kube-system kube-scheduler-docker-desktopNAME                            READY   STATUS    RESTARTS   AGEkube-scheduler-docker-desktop   1/1     Running   0          16h➜  ~ kubectl get pods -n kubernetes-dashboardNAME                                         READY   STATUS    RESTARTS   AGEdashboard-metrics-scraper-694557449d-7h8k6   1/1     Running   0          16hkubernetes-dashboard-7d9ddf9f8f-wprd7        1/1     Running   0          16h

查看svc信息

➜  ~ kubectl describe svcName:              kubernetesNamespace:         defaultLabels:            component=apiserver                   provider=kubernetesAnnotations:       Selector:          Type:              ClusterIPIP:                10.96.0.1Port:              https  443/TCPTargetPort:        6443/TCPEndpoints:         192.168.65.3:6443Session Affinity:  NoneEvents:            

查看nodeport 所有的services

➜  ~  kubectl get services -ANAMESPACE              NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGEdefault                kubernetes                  ClusterIP   10.96.0.1                443/TCP                  17hkube-system            kube-dns                    ClusterIP   10.96.0.10               53/UDP,53/TCP,9153/TCP   17hkubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   10.102.121.205           8000/TCP                 16hkubernetes-dashboard   kubernetes-dashboard        ClusterIP   10.98.196.76             443/TCP                  16h-n后面跟 NAMESPACE➜  ~ kubectl get service -n kube-system -o wideNAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE   SELECTORkube-dns   ClusterIP   10.96.0.10           53/UDP,53/TCP,9153/TCP   16h   k8s-app=kube-dns➜  ~ kubectl get service -n kube-system -o wideNAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE   SELECTORkube-dns   ClusterIP   10.96.0.10           53/UDP,53/TCP,9153/TCP   15h   k8s-app=kube-dns➜  ~ kubectl get service -n kubernetes-dashboardNAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGEdashboard-metrics-scraper   ClusterIP   10.102.121.205           8000/TCP   16hkubernetes-dashboard        ClusterIP   10.98.196.76             443/TCP    16h

# 根据 yaml 创建资源, apply 可以重复执行,create 不行

kubectl create -f pod.yamlkubectl apply -f pod.yaml

kubectl run 在集群中运行一个指定的镜像

参考:

http://kubernetes.kansea.com/docs/user-guide/kubectl/kubectl_run/

run后面跟pods 为 nginx➜  ~ kubectl run nginx --image=centos --port=80 --replicas=1Flag --replicas has been deprecated, has no effect and will be removed in the future.pod/nginx created可以看到上面这个命令术不会被弃用并且只创建一个nginx 容器实例在K8S v1.18.0 以后,--replicas已弃用 ,推荐用 deployment 创建 pod。如果需要创建可以通过如下方式操作:kubectl apply -f nginx.yamlThis action is equivalent to:kubectl apply -f ➜  ~ kubectl run nginx --image=centos --port=80pod/nginx created➜  ~ kubectl logs nginxError from server (BadRequest): container "nginx" in pod "nginx" is waiting to start: ContainerCreating查看这个pods nginx的状态  kubectl describe pods nginxkubctl获取pod状态➜  ~ kubectl get pods nginxNAME    READY   STATUS             RESTARTS   AGEnginx   0/1     CrashLoopBackOff   37         168m查询异常pod名称为 nginx查看此状态pod详细情况➜  ~ kubectl describe pods nginx这里能看到获取的IP地址10.1.0.10查看此pod日志kubectl logs nginx这里日志是空的,所以我现在删除这个pods nginxkubectl delete pods nginxpod "nginx" deleted//下面是OK的过程/再次简单启动:➜  ~ kubectl run nginx --image=nginx

查看日志

➜  ~ kubectl logs nginx/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d//docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh/docker-entrypoint.sh: Configuration complete; ready for start up10.1.0.10 - - [14/Oct/2020:08:54:59 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.0" "-"10.1.0.10 - - [14/Oct/2020:08:55:07 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.64.0" "-"

下面请求下看看,而且日志已经记录了

执行 pod 的 命令比如curl

➜ ~ kubectl exec nginx -- curl -I 10.1.0.10

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

0 612 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

HTTP/1.1 200 OK

Server: nginx/1.19.3

Date: Wed, 14 Oct 2020 08:55:07 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Tue, 29 Sep 2020 14:12:31 GMT

Connection: keep-alive

ETag: "5f7340cf-264"

Accept-Ranges: bytes

通过bash获得 pod 中某个容器的TTY,相当于登录容器

kubectl exec -it -c -- bash

eg:

kubectl exec -it redis-master-cln81 -- bash

➜ ~ kubectl exec -it nginx -- bash

root@nginx:/# w

bash: w: command not found

root@nginx:/# curl -I http://localhost

HTTP/1.1 200 OK

Server: nginx/1.19.3

Date: Wed, 14 Oct 2020 09:01:44 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Tue, 29 Sep 2020 14:12:31 GMT

Connection: keep-alive

ETag: "5f7340cf-264"

Accept-Ranges: bytes

root@nginx:/# curl -I http://10.1.0.10

HTTP/1.1 200 OK

Server: nginx/1.19.3

Date: Wed, 14 Oct 2020 09:01:56 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Tue, 29 Sep 2020 14:12:31 GMT

Connection: keep-alive

ETag: "5f7340cf-264"

Accept-Ranges: bytes

重启 pod

kubectl get pod  -n  -o yaml | kubectl replace --force -f -
55823351f6e03575cea7f30bf9ae1c08.png
c363e81f4c29b6895fa2897ff1ce1deb.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值