常用kubectl命令

本文详述了如何使用Kubernetes命令行工具kubectl进行集群组件状态检查、环境信息查看、节点管理、服务部署与暴露、命名空间操作、日志查看以及配置文件的查看和修改。内容涵盖从基础的容器运行到高级的集群配置调整,是理解Kubernetes集群运维的重要参考资料。
摘要由CSDN通过智能技术生成

查看组件状态

kubectl get cs

eric@server1:/usr/local/kubernetes/cluster$ kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok
controller-manager   Healthy   ok
etcd-0               Healthy   {"health":"true"}

查看环境信息

kubectl cluster-info

eric@server1:/usr/local/kubernetes/cluster$ kubectl cluster-info
Kubernetes master is running at https://192.168.90.100:6444
KubeDNS is running at https://192.168.90.100:6444/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

查看 Node

kubectl get nodes -o wide

eric@server1:/usr/local/kubernetes/cluster$ kubectl get nodes -o wide
NAME      STATUS   ROLES    AGE     VERSION    INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
server1   Ready    master   7d13h   v1.14.10   192.168.90.31   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
server2   Ready    master   7d13h   v1.14.10   192.168.90.32   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
server3   Ready    master   7d14h   v1.14.10   192.168.90.33   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
server4   Ready    <none>   7d13h   v1.14.10   192.168.90.34   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
server5   Ready    <none>   7d13h   v1.14.10   192.168.90.35   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8
server6   Ready    <none>   7d13h   v1.14.10   192.168.90.36   <none>        Ubuntu 18.04.5 LTS   4.15.0-154-generic   docker://20.10.8

查看集群配置

kubectl -n kube-system get cm kubeadm-config -oyaml

eric@server1:/usr/local/kubernetes/cluster$ kubectl -n kube-system get cm kubeadm-config -oyaml
apiVersion: v1
data:
  ClusterConfiguration: |
    apiServer:
      extraArgs:
        authorization-mode: Node,RBAC
      timeoutForControlPlane: 3m0s
    apiVersion: kubeadm.k8s.io/v1beta1
    certificatesDir: /etc/kubernetes/pki
    clusterName: kubernetes
    controlPlaneEndpoint: 192.168.90.100:6444
    controllerManager: {}
    dns:
      type: CoreDNS
    etcd:
      local:
        dataDir: /var/lib/etcd
    imageRepository: registry.aliyuncs.com/google_containers
    kind: ClusterConfiguration
    kubernetesVersion: v1.14.10
    networking:
      dnsDomain: cluster.local
      podSubnet: 10.244.0.0/16
      serviceSubnet: 10.96.0.0/12
    scheduler: {}
  ClusterStatus: |
    apiEndpoints:
      server1:
        advertiseAddress: 192.168.90.31
        bindPort: 6443
      server2:
        advertiseAddress: 192.168.90.32
        bindPort: 6443
      server3:
        advertiseAddress: 192.168.90.33
        bindPort: 6443
    apiVersion: kubeadm.k8s.io/v1beta1
    kind: ClusterStatus
kind: ConfigMap
metadata:
  creationTimestamp: "2021-08-14T15:52:28Z"
  name: kubeadm-config
  namespace: kube-system
  resourceVersion: "6537"
  selfLink: /api/v1/namespaces/kube-system/configmaps/kubeadm-config
  uid: a0c0399d-fd17-11eb-b5df-000c29958fba

运行容器

kubectl run nginx --image=nginx --replicas=2 --port=80
kubectl run 会创建一个 Deployment 并且默认会在后台运行,以上面的代码为例它的名称为 nginx。默认情况 Deployment 并不会将端口暴露出去,所以我们还需要使用 kubectl expose 暴露端口以供访问,此时还会创建一个同名的 Service

暴露服务

kubectl expose deployment nginx --port=80 --type=LoadBalancer

查看命名空间

kubectl get namespace

eric@server1:/usr/local/kubernetes/cluster$ kubectl get namespace
NAME                   STATUS   AGE
default                Active   7d14h
kube-node-lease        Active   7d14h
kube-public            Active   7d14h
kube-system            Active   7d14h
kubernetes-dashboard   Active   36m

创建命名空间

apiVersion: v1
kind: Namespace
metadata:
  name: development

查看容器

kubectl get pods -o wide 同docker ps
kubectl get deployment -o wide 同docker ps

eric@server1:/usr/local/kubernetes/cluster$ kubectl get deployment -o wide
NAME           READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS     IMAGES   SELECTOR
mysql-myshop   0/1     1            0           79m    mysql-myshop   mysql    name=mysql-myshop
tomcat-app     2/2     2            2           7d2h   tomcat         tomcat   name=tomcat
eric@server1:/usr/local/kubernetes/cluster$ kubectl get pod
NAME                           READY   STATUS             RESTARTS   AGE
mysql-myshop-86b6c55bf-m69ht   0/1     CrashLoopBackOff   20         80m
tomcat-app-55686c8fd9-b7tdh    1/1     Running            2          7d2h
tomcat-app-55686c8fd9-r89tz    1/1     Running            2          7d2h

默认值查看默认命名空间下的内容,如果要看所有命名空间,加参数 -A

eric@server1:/usr/local/kubernetes/cluster$ kubectl get pod -A
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
default                mysql-myshop-86b6c55bf-m69ht                 1/1     Running   23         102m
default                tomcat-app-55686c8fd9-b7tdh                  1/1     Running   2          7d3h
default                tomcat-app-55686c8fd9-r89tz                  1/1     Running   2          7d3h
kube-system            calico-kube-controllers-f6ff9cbbb-2lwzt      1/1     Running   3          7d13h
kube-system            calico-node-49lqn                            1/1     Running   8          7d14h
kube-system            calico-node-jmp28                            1/1     Running   4          7d13h
kube-system            calico-node-kszl7                            1/1     Running   4          7d13h
kube-system            calico-node-njz8v                            1/1     Running   9          7d14h
kube-system            calico-node-sb2kb                            1/1     Running   7          7d15h
kube-system            calico-node-sn874                            1/1     Running   4          7d13h
kube-system            coredns-7b7df549dd-qnd6f                     1/1     Running   3          7d13h
kube-system            coredns-7b7df549dd-s6mgf                     1/1     Running   3          7d13h
kube-system            etcd-server1                                 1/1     Running   8          7d14h
kube-system            etcd-server2                                 1/1     Running   4          7d14h
kube-system            etcd-server3                                 1/1     Running   5          7d15h
kube-system            kube-apiserver-server1                       1/1     Running   11         7d14h
kube-system            kube-apiserver-server2                       1/1     Running   8          7d14h
kube-system            kube-apiserver-server3                       1/1     Running   8          7d15h
kube-system            kube-controller-manager-server1              1/1     Running   6          7d14h
kube-system            kube-controller-manager-server2              1/1     Running   6          7d14h
kube-system            kube-controller-manager-server3              1/1     Running   6          7d15h
kube-system            kube-proxy-5hl76                             1/1     Running   5          7d14h
kube-system            kube-proxy-gt6bj                             1/1     Running   3          7d13h
kube-system            kube-proxy-nxx9l                             1/1     Running   3          7d13h
kube-system            kube-proxy-q42pg                             1/1     Running   4          7d15h
kube-system            kube-proxy-qfkth                             1/1     Running   4          7d14h
kube-system            kube-proxy-zc5c2                             1/1     Running   3          7d13h
kube-system            kube-scheduler-server1                       1/1     Running   6          7d14h
kube-system            kube-scheduler-server2                       1/1     Running   4          7d14h
kube-system            kube-scheduler-server3                       1/1     Running   7          7d15h
kubernetes-dashboard   dashboard-metrics-scraper-5c6486bf56-pjzpv   1/1     Running   0          60m
kubernetes-dashboard   kubernetes-dashboard-5757d9f8bc-4jbxv        1/1     Running   0          60m

查看服务

kubectl get service -o wide 同docker ps

eric@server1:/usr/local/kubernetes/cluster$ kubectl get service -o wide
NAME           TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE     SELECTOR
kubernetes     ClusterIP      10.96.0.1        <none>        443/TCP          7d15h   <none>
mysql-myshop   LoadBalancer   10.104.140.183   <pending>     3306:32036/TCP   88m     name=mysql-myshop
tomcat-http    LoadBalancer   10.98.183.59     <pending>     8080:32168/TCP   7d2h    name=tomcat

查看详情

kubectl describe pod
kubectl describe deployment
kubectl describe service

eric@server1:/usr/local/kubernetes/cluster$ kubectl describe service  mysql-myshop
Name:                     mysql-myshop
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 name=mysql-myshop
Type:                     LoadBalancer
IP:                       10.104.140.183
Port:                     <unset>  3306/TCP
TargetPort:               3306/TCP
NodePort:                 <unset>  32036/TCP
Endpoints:
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

查看日志

kubectl logs -f 同 docker logs

eric@server1:/usr/local/kubernetes/cluster$ kubectl logs -f mysql-myshop-86b6c55bf-m69ht
2021-08-22 06:58:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
2021-08-22 06:58:42+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-08-22 06:58:42+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
2021-08-22 06:58:43+00:00 [Note] [Entrypoint]: Initializing database files
2021-08-22T06:58:43.037515Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 44
2021-08-22T06:58:43.040297Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2021-08-22T06:58:43.040303Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2021-08-22T06:58:43.040633Z 0 [ERROR] [MY-010119] [Server] Aborting
2021-08-22T06:58:43.040823Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26)  MySQL Community Server - GPL.

删除容器和服务

kubectl delete deployment 同 codker rm 容器id
kubectl delete service

配置方式运行 容器

kubectl create -f

配置方式删除

kubectl delete -f

查看配置

kubeadm config view
kubectl config view

查看 Ingress

kubectl get ingress

查看持久卷

kubectl get pv

查看持久卷消费者

kubectl get pvc

查看 ConfigMap

kubectl get cm

eric@server1:/usr/local/kubernetes/cluster$ kubectl get cm 
NAME                  DATA   AGE
mysql-myshop-config   1      94m

修改 ConfigMap

kubectl edit cm

修改服务暴露类型

 kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kubernetes-dashboard
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

catch that elf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值