kubectl常用总结

IT
IT
IT
335
文章
1
评论
2020年5月13日11:29:13 评论 1,190 11125字阅读37分5秒

kubectl命令集合

kubectl常用总结

  • get查看一个或多个资源
[root@k8s-master1 ~]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
busybox                 1/1     Running   21         21h
http-797d8dcbc7-88czq   1/1     Running   1          21h
http-797d8dcbc7-ktkmn   1/1     Running   1          21h
http-797d8dcbc7-l8kbw   1/1     Running   1          21h
web-d86c95cc9-csf8n     1/1     Running   1          21h
[root@k8s-master1 ~]# kubectl get node
NAME        STATUS   ROLES    AGE   VERSION
k8s-node1   Ready    <none>   25h   v1.16.0
k8s-node2   Ready    <none>   25h   v1.16.0
  • get查看当前所有的资源
[root@k8s-master1 ~]# kubectl get all
NAME                        READY   STATUS    RESTARTS   AGE
pod/busybox                 1/1     Running   21         21h
pod/http-797d8dcbc7-88czq   1/1     Running   1          21h
pod/http-797d8dcbc7-ktkmn   1/1     Running   1          21h
pod/http-797d8dcbc7-l8kbw   1/1     Running   1          21h
pod/web-d86c95cc9-csf8n     1/1     Running   1          22h

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/http         NodePort    10.0.0.164   <none>        90:32738/TCP   21h
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        26h
service/web          NodePort    10.0.0.197   <none>        80:30484/TCP   22h

NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/http   3/3     3            3           21h
deployment.apps/web    1/1     1            1           22h

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/http-797d8dcbc7   3         3         3       21h
replicaset.apps/web-d86c95cc9     1         1         1       22h
  • get查看当前所有资源更详细的信息
[root@k8s-master1 ~]# kubectl get all -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
pod/busybox                 1/1     Running   21         21h   10.244.1.13   k8s-node2   <none>           <none>
pod/http-797d8dcbc7-88czq   1/1     Running   1          21h   10.244.1.10   k8s-node2   <none>           <none>
pod/http-797d8dcbc7-ktkmn   1/1     Running   1          21h   10.244.0.5    k8s-node1   <none>           <none>
pod/http-797d8dcbc7-l8kbw   1/1     Running   1          21h   10.244.1.12   k8s-node2   <none>           <none>
pod/web-d86c95cc9-csf8n     1/1     Running   1          22h   10.244.0.7    k8s-node1   <none>           <none>

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/http         NodePort    10.0.0.164   <none>        90:32738/TCP   21h   app=http
service/kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        26h   <none>
service/web          NodePort    10.0.0.197   <none>        80:30484/TCP   22h   app=web

NAME                   READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES   SELECTOR
deployment.apps/http   3/3     3            3           21h   httpd        httpd    app=http
deployment.apps/web    1/1     1            1           22h   nginx        nginx    app=web

NAME                              DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES   SELECTOR
replicaset.apps/http-797d8dcbc7   3         3         3       21h   httpd        httpd    app=http,pod-template-hash=797d8dcbc7
replicaset.apps/web-d86c95cc9     1         1         1       22h   nginx        nginx    app=web,pod-template-hash=d86c95cc9
  • 查看pod更详细的信息
[root@k8s-master1 ~]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
busybox                 1/1     Running   21         21h   10.244.1.13   k8s-node2   <none>           <none>
http-797d8dcbc7-88czq   1/1     Running   1          21h   10.244.1.10   k8s-node2   <none>           <none>
http-797d8dcbc7-ktkmn   1/1     Running   1          21h   10.244.0.5    k8s-node1   <none>           <none>
http-797d8dcbc7-l8kbw   1/1     Running   1          21h   10.244.1.12   k8s-node2   <none>           <none>
web-d86c95cc9-csf8n     1/1     Running   1          22h   10.244.0.7    k8s-node1   <none>           <none>
  • 查看不同命名空间的pod详细信息
[root@k8s-master1 ~]# kubectl get pod -o wide -n kube-system
NAME                          READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
coredns-65567c7b57-sskls      1/1     Running   1          21h   10.244.1.11   k8s-node2   <none>           <none>
kube-flannel-ds-amd64-96v9n   1/1     Running   1          22h   10.10.0.246   k8s-node2   <none>           <none>
kube-flannel-ds-amd64-nbx8g   1/1     Running   1          22h   10.10.0.245   k8s-node1   <none>           <none>

创建pod

  • 指定副本数和nginx版本以及端口
[root@k8s-master1 ~]# kubectl run nginx --replicas=3 --image=nginx:1.14 --port=80
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.
deployment.apps/nginx created
  • 查看pod
[root@k8s-master1 ~]# kubectl get deploy,pods
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           72s

NAME                         READY   STATUS    RESTARTS   AGE
pod/busybox                  1/1     Running   23         22h
pod/nginx-59d795d786-2bkqp   1/1     Running   0          72s
pod/nginx-59d795d786-bnn9r   1/1     Running   0          72s
pod/nginx-59d795d786-xlps4   1/1     Running   0          72s
  • 暴露端口
[root@k8s-master1 ~]# kubectl expose deployment nginx --port=80 --type=NodePort --target-port=80 --name=nginx-service
service/nginx-service exposed
  • 查看service
[root@k8s-master1 ~]# kubectl get service
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
http            NodePort    10.0.0.164   <none>        90:32738/TCP   22h
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        27h
nginx-service   NodePort    10.0.0.230   <none>        80:31299/TCP   34s
web             NodePort    10.0.0.197   <none>        80:30484/TCP   23h
  • 任意一个node节点做测试
[root@k8s-master1 ~]# curl 10.10.0.245:31299
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a rel="external nofollow" target="_blank" href="https://www.centoscn.vip/wp-content/themes/begin/go.php?url=aHR0cDovL25naW54Lm9yZy8=">nginx.org</a>.<br/>
Commercial support is available at
<a rel="external nofollow" target="_blank" href="https://www.centoscn.vip/wp-content/themes/begin/go.php?url=aHR0cDovL25naW54LmNvbS8=">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 更新版本
[root@k8s-master1 ~]# kubectl set image deployment/nginx nginx=nginx:1.15
deployment.apps/nginx image updated
  • 查看是否更新到了最新版本
[root@k8s-master1 ~]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
busybox                 1/1     Running   23         22h
nginx-dc5dc5865-grwbx   1/1     Running   0          87s
nginx-dc5dc5865-gzmdv   1/1     Running   0          119s
nginx-dc5dc5865-jrrkb   1/1     Running   0          110s
[root@k8s-master1 ~]# kubectl logs nginx-dc5dc5865-grwbx
[root@k8s-master1 ~]# kubectl describe pod nginx-dc5dc5865-jrrkb
Name:         nginx-dc5dc5865-jrrkb
Namespace:    default
Priority:     0
Node:         k8s-node2/10.10.0.246
Start Time:   Sat, 30 May 2020 14:48:44 +0800
Labels:       pod-template-hash=dc5dc5865
              run=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.1.21
IPs:
  IP:           10.244.1.21
Controlled By:  ReplicaSet/nginx-dc5dc5865
Containers:
  nginx:
    Container ID:   docker://4980776c80258e4c98b88663680bf6fd6b816225b814ab6a2c146c5e380e32f6
    Image:          nginx:1.15

回滚

  • 查看发版的历史版本
[root@k8s-master1 ~]# kubectl rollout history deployment/nginx
deployment.apps/nginx
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
  • 回滚到上一个版本
[root@k8s-master1 ~]# kubectl rollout undo deployment/nginx
deployment.apps/nginx rolled back

删除pod

  • 删除service
[root@k8s-master1 ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
http         NodePort    10.0.0.164   <none>        90:32738/TCP   23h
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP        27h
web          NodePort    10.0.0.197   <none>        80:30484/TCP   23h
[root@k8s-master1 ~]# kubectl delete svc/web
service "web" deleted
[root@k8s-master1 ~]# kubectl delete svc/http
service "http" deleted
[root@k8s-master1 ~]# kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   27h
  • 查询pod
[root@k8s-master1 ~]# kubectl get deploy,pods
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/http   3/3     3            3           21h
deployment.apps/web    1/1     1            1           22h

NAME                        READY   STATUS    RESTARTS   AGE
pod/busybox                 1/1     Running   21         21h
pod/http-797d8dcbc7-88czq   1/1     Running   1          21h
pod/http-797d8dcbc7-ktkmn   1/1     Running   1          21h
pod/http-797d8dcbc7-l8kbw   1/1     Running   1          21h
pod/web-d86c95cc9-csf8n     1/1     Running   1          22h
  • 删除
[root@k8s-master1 ~]# kubectl delete deployment.apps/http
deployment.apps "http" deleted
[root@k8s-master1 ~]# kubectl delete deployment.apps/web
deployment.apps "web" deleted
  • 验证
[root@k8s-master1 ~]# kubectl get deploy,pods
NAME          READY   STATUS    RESTARTS   AGE
pod/busybox   1/1     Running   21         21h

查看节点详细信息

[root@iZ2zei14p015t8wkktdhn2Z tools]# kubectl get nodes -o wide
NAME                      STATUS   ROLES    AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                CONTAINER-RUNTIME
iz2zei14p015t8wkktdhn1z   Ready    <none>   18h   v1.15.0   10.0.0.63     <none>        CentOS Linux 7 (Core)   3.10.0-1062.18.1.el7.x86_64   docker://18.6.1
iz2zei14p015t8wkktdhn2z   Ready    master   18h   v1.15.0   10.0.0.61     <none>        CentOS Linux 7 (Core)   3.10.0-1062.18.1.el7.x86_64   docker://18.6.1
iz2zei14p015t8wkktdhn3z   Ready    <none>   18h   v1.15.0   10.0.0.62     <none>        CentOS Linux 7 (Core)   3.10.0-1062.18.1.el7.x86_64   docker://18.6.1

查看node状态

[root@iZ2zei14p015t8wkktdhn2Z ~]# kubectl get node
NAME                      STATUS   ROLES    AGE   VERSION
iz2zei14p015t8wkktdhn1z   Ready    <none>   18h   v1.15.0
iz2zei14p015t8wkktdhn2z   Ready    master   18h   v1.15.0
iz2zei14p015t8wkktdhn3z   Ready    <none>   18h   v1.15.0

查看集群信息

[root@iZ2zei14p015t8wkktdhn2Z tools]# kubectl cluster-info
Kubernetes master is running at https://10.0.0.61:6443
KubeDNS is running at https://10.0.0.61:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

查看所有命名空间下的pod

[root@k8s-master ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGEdefault       nginx-f89759699-79kpb                1/1     Running   1          13m
default       nginx-f89759699-cg2xw                1/1     Running   1          10mdefault       nginx-f89759699-fg2qt                1/1     Running   1          10m
kube-system   coredns-7ff77c879f-28cbh             1/1     Running   1          36mkube-system   coredns-7ff77c879f-pg6hn             1/1     Running   1          36m
kube-system   etcd-k8s-master                      1/1     Running   1          37mkube-system   kube-apiserver-k8s-master            1/1     Running   1          37m
kube-system   kube-controller-manager-k8s-master   1/1     Running   1          37mkube-system   kube-flannel-ds-amd64-ntjbj          1/1     Running   2          23m
kube-system   kube-flannel-ds-amd64-nvw8x          1/1     Running   1          33mkube-system   kube-flannel-ds-amd64-vjb6h          1/1     Running   1          26m
kube-system   kube-proxy-6p98l                     1/1     Running   1          23mkube-system   kube-proxy-9v2hl                     1/1     Running   1          26m
kube-system   kube-proxy-xljd6                     1/1     Running   1          36mkube-system   kube-scheduler-k8s-master            1/1     Running   1          37m

查看运行的pod的详细信息

[root@k8s-master ~]# kubectl get pods --all-namespaces -o wideNAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
default       nginx-f89759699-79kpb                1/1     Running   1          15m   10.244.2.3    k8s-node2    <none>           <none>default       nginx-f89759699-cg2xw                1/1     Running   1          12m   10.244.1.5    k8s-node1    <none>           <none>
default       nginx-f89759699-fg2qt                1/1     Running   1          12m   10.244.1.4    k8s-node1    <none>           <none>kube-system   coredns-7ff77c879f-28cbh             1/1     Running   1          39m   10.244.0.6    k8s-master   <none>           <none>
kube-system   coredns-7ff77c879f-pg6hn             1/1     Running   1          39m   10.244.0.5    k8s-master   <none>           <none>kube-system   etcd-k8s-master                      1/1     Running   1          39m   10.10.0.244   k8s-master   <none>           <none>
kube-system   kube-apiserver-k8s-master            1/1     Running   1          39m   10.10.0.244   k8s-master   <none>           <none>kube-system   kube-controller-manager-k8s-master   1/1     Running   1          39m   10.10.0.244   k8s-master   <none>           <none>
kube-system   kube-flannel-ds-amd64-ntjbj          1/1     Running   2          25m   10.10.0.246   k8s-node2    <none>           <none>kube-system   kube-flannel-ds-amd64-nvw8x          1/1     Running   1          36m   10.10.0.244   k8s-master   <none>           <none>
kube-system   kube-flannel-ds-amd64-vjb6h          1/1     Running   1          28m   10.10.0.245   k8s-node1    <none>           <none>
kube-system   kube-proxy-6p98l                     1/1     Running   1          25m   10.10.0.246   k8s-node2    <none>           <none>
kube-system   kube-proxy-9v2hl                     1/1     Running   1          28m   10.10.0.245   k8s-node1    <none>           <none>
kube-system   kube-proxy-xljd6                     1/1     Running   1          39m   10.10.0.244   k8s-master   <none>           <none>
kube-system   kube-scheduler-k8s-master            1/1     Running   1          39m   10.10.0.244   k8s-master   <none>           <none>
  • 查看pod的详细信息
[root@iZ2zei14p015t8wkktdhn2Z ~]# kubectl describe pod nginx-554b9c67f9-2xcpx
Name:           nginx-554b9c67f9-2xcpx
Namespace:      default
Priority:       0
Node:           iz2zei14p015t8wkktdhn3z/10.0.0.62
Start Time:     Tue, 12 May 2020 17:48:50 +0800
Labels:         app=nginx
                pod-template-hash=554b9c67f9
Annotations:    <none>
Status:         Running
IP:             10.244.1.10
Controlled By:  ReplicaSet/nginx-554b9c67f9
Containers:
  nginx:
    Container ID:   docker://19e25802c1a34a940f92c36456a6daba1a2ea81b6d6d3461c0565cc11eca3565
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Wed, 13 May 2020 09:59:27 +0800
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 12 May 2020 18:50:46 +0800
      Finished:     Tue, 12 May 2020 18:54:46 +0800
    Ready:          True
    Restart Count:  2
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-txbwt (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-txbwt:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-txbwt
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From                              Message
  ----    ------     ----  ----                              -------
  Normal  Scheduled  18h   default-scheduler                 Successfully assigned default/nginx-554b9c67f9-2xcpx to iz2zei14p015t8wkktdhn3z
  Normal  Pulling    18h   kubelet, iz2zei14p015t8wkktdhn3z  Pulling image "nginx"
  Normal  Pulled     18h   kubelet, iz2zei14p015t8wkktdhn3z  Successfully pulled image "nginx"
  Normal  Created    18h   kubelet, iz2zei14p015t8wkktdhn3z  Created container nginx
  Normal  Started    18h   kubelet, iz2zei14p015t8wkktdhn3z  Started container nginx
继续阅读
  • 我的QQ
  • QQ扫一扫
  • weinxin
  • 我的头条
  • 头条扫一扫
  • weinxin
IT
  • 本文由 发表于 2020年5月13日11:29:13
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Pod Kubernetes

Pod

Pod基本概念 Pod存在的意义 Pod实现机制与设计模式 Pod镜像拉取策略
资源编排【YMAL】 Kubernetes

资源编排【YMAL】

YAML 基础 它的基本语法规则如下: 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格。 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 # 表示注释,从这个字符一直...
部署CoreDNS Kubernetes

部署CoreDNS

CoreDNS用于集群内部Service名称解析 下载配置文件 coredns.yaml 创建dns # kubectl apply -f coredns.yaml 查看pod # kubectl g...