龙空技术网

kubectl 命令简单备录

MacLex 899

前言:

目前朋友们对“nginxenvsubst”大体比较注重,朋友们都需要了解一些“nginxenvsubst”的相关内容。那么小编也在网络上搜集了一些对于“nginxenvsubst””的相关文章,希望姐妹们能喜欢,各位老铁们一起来学习一下吧!

kubectl controls the Kubernetes cluster manager.

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

node

pods

services

///////////////////////////////////////////////////////////////////////////////////

kubectl describe 描述资源对象

kubectl describe nodes <node-name> # 显示Node的详细信息

kubectl describe pods <pod-name> # 显示Pod的详细信息

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

kubectl describe nodes

kubectl describe node

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

   ➜  ~ 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 <pod-name> -o wide      # 查看Pod详细信息kubectl get pod <pod-name> -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  is running at  further debug and diagnose cluster problems, use 'kubectl cluster-info dump'

查看创建的pod

查看所有 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:       <none>Selector:          <none>Type:              ClusterIPIP:                10.96.0.1Port:              https  443/TCPTargetPort:        6443/TCPEndpoints:         192.168.65.3:6443Session Affinity:  NoneEvents:            <none>

查看nodeport 所有的services

➜  ~  kubectl get services -ANAMESPACE              NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGEdefault                kubernetes                  ClusterIP   10.96.0.1        <none>        443/TCP                  17hkube-system            kube-dns                    ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   17hkubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   10.102.121.205   <none>        8000/TCP                 16hkubernetes-dashboard   kubernetes-dashboard        ClusterIP   10.98.196.76     <none>        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   <none>        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   <none>        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   <none>        8000/TCP   16hkubernetes-dashboard        ClusterIP   10.98.196.76     <none>        443/TCP    16h

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

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

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 <spec.yaml>➜  ~ 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 <pod-name> -c <container-name> -- 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/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/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 <POD名称> -n <NAMESPACE名称> -o yaml | kubectl replace --force -f -

标签: #nginxenvsubst