集群管理命令

—— kubectl是用于控制Kubernetes集群的命令行工具

语法格式:

—— kubectl [command] [TYPE] [NAME] [flags]

      commd: 子命令,如create,get,describe,delete

      type: 资源类型,可以表示为单数,复数或缩写形式  (所有类型都用kubectl get 查看)

      name: 资源的名称,如果省略,则显示所有资源信息

      flags: 指定可选标志,或附加的参数

集群信息管理命令

命令说明:

子命令

说明

help

用于查看命令及子命令的帮助信息

cluster-info

显示集群的相关配置信息

version

查看服务器及客户端的版本信息

api-resources

查看当前服务器上所有的资源对象

api-versions

查看当前服务器上所有资源对象的版本

config

管理当前节点上kubeconfig 的认证信息

help

# 查看帮助命令信息
[root@master ~]# kubectl help versionPrint the client and server version information for the current context.
Examples:  
# Print the client and server versions for the current context  kubectl version... ...

cluster-info

# 查看集群状态信息
[root@master ~]# kubectl cluster-info 
Kubernetes control plane is running at https://192.168.1.50:6443CoreDNS is running at https://192.168.1.50:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy... ...

version

# 查看服务端与客户端版本信息
[root@master ~]# kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:58:30Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.0", GitCommit:"b46a3f887ca979b1a5d14fd39cb1af43e7e5d12d", GitTreeState:"clean", BuildDate:"2022-12-08T19:51:45Z", GoVersion:"go1.19.4", Compiler:"gc", Platform:"linux/amd64"}

api-resources

# 查看资源对象类型
[root@master ~]# kubectl api-resources 
NAME             SHORTNAMES     APIVERSION      NAMESPACED      KIND
bindings                        v1              true            Binding
endpoints        ep             v1              true            Endpoints
events           ev             v1              true            Event... 
...

api-versions

# 查看资源对象版本
[root@master ~]# kubectl api-versions 
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1... ...

config

# 查看当前认证使用的用户及证书
[root@master ~]# kubectl config get-contexts 
CURRENT   NAME                          CLUSTER      AUTHINFO
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin
# 使用 view 查看详细配置
[root@master ~]# kubectl config view 
apiVersion: v1
clusters:
- cluster:
     certificate-authority-data: DATA+OMITTED    
     server: https://192.168.1.50:6443  
  name: kubernetes
contexts:
- context:    
     cluster: kubernetes    
     user: kubernetes-admin 
    name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin  
  user:    
    client-certificate-data: REDACTED    
    client-key-data: REDACTED

主机管理授权

 vim /etc/hosts
 dnf install -y kubectl
 mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

资源对象概述

  • 资源对象

—— k8s中把可以创建或配置的应用和服务称为资源对象(k8s不能直接创建容器,需要利用pod存放容器)

—— 我们在集群中创建的pod、负载均衡、存储、网络服务等等都是资源对象

  • 创建资源对象

—— 简单资源对象可以使用“kubectl”直接创建

—— 高级资源对象需要使用“资源对象文件”创建

  • 使用run创建一个pod资源对象

         kubeclt run pod名称 [选项/参数] --image=镜像名称:标签

[root@master ~]# kubectl run myweb --image=myos:httpd
pod/myweb created
[root@master ~]# kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP              NODE        NOMINATED NODE   READINESS GATES
myweb   1/1     Running   0          9m10s   10.244.21.129   node-0001   <none>           <none>
[root@master ~]# curl http://10.244.21.129
Welcome to The Apache.