Kubernetes基础教程-通过部署Nginx快速学习基本使用命令

艺帆风顺 发布于 2025-04-11 31 次阅读


 

1、验证节点信息

1.1、查看控制平面组件的状态

[root@linux-servertwo software]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}  

#controller-manager: 控制器管理器,负责维护集群的状态,例如复制控制器、部署控制器等。
#scheduler: 调度器,负责决定将哪个 Pod 安排到哪个节点。
#etcd-0: 分布式键值存储,用于存储 Kubernetes 集群的所有数据。

1.2、查看节点信息

1.2.1、查看节点信息

#查看节点信息
[root@linux-servertwo software]# kubectl get nodes
NAME              STATUS   ROLES                  AGE   VERSION
linux-servertwo   Ready    control-plane,master,node   26h   v1.20.9

1.2.2、查看更加充分的节点信息

#查看更加充分的节点信息
[root@linux-servertwo software]# kubectl get nodes -o wide 
NAME              STATUS   ROLES                  AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION                 CONTAINER-RUNTIME
linux-servertwo   Ready    control-plane,master,node   27h   v1.20.9   10.0.4.16            CentOS Linux 7 (Core)   3.10.0-1160.108.1.el7.x86_64   docker://26.1.3

1.2.3、查看节点详情

#查看节点详情:
[root@linux-servertwo software]# kubectl describe node linux-servertwo
Name:               linux-servertwo
Roles:              control-plane,master,node
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=linux-servertwo
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/control-plane=
                    node-role.kubernetes.io/master=
                    node-role.kubernetes.io/node=
......

1.3、查看系统组件

# 集群中的各个组件也都是以Pod方式运行的
[root@linux-servertwo software]# kubectl get pods -n kube-system
NAME                                      READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-2k74p                  1/1     Running   0          27h
coredns-7f89b7bc75-6xrvb                  1/1     Running   0          27h
etcd-linux-servertwo                      1/1     Running   0          27h
kube-apiserver-linux-servertwo            1/1     Running   0          27h
kube-controller-manager-linux-servertwo   1/1     Running   0          25h
kube-proxy-bmfzr                          1/1     Running   0          27h
kube-scheduler-linux-servertwo            1/1     Running   0          25h

1.4、查看所有Pod

1.4.1、查看所有Pod信息

[root@linux-servertwo software]# kubectl get pods --all-namespaces
NAMESPACE      NAME                                      READY   STATUS    RESTARTS   AGE
kube-flannel   kube-flannel-ds-lbmdr                     1/1     Running   0          5d
kube-system    coredns-7f89b7bc75-47d56                  1/1     Running   0          6d4h
kube-system    coredns-7f89b7bc75-h4c9b                  1/1     Running   0          6d4h
kube-system    etcd-linux-servertwo                      1/1     Running   0          6d4h
kube-system    kube-apiserver-linux-servertwo            1/1     Running   0          6d4h
kube-system    kube-controller-manager-linux-servertwo   1/1     Running   0          5d
kube-system    kube-proxy-j7d4v                          1/1     Running   0          6d4h
kube-system    kube-scheduler-linux-servertwo            1/1     Running   0          5d

1.4.2、查看更充分的所有pod信息

# 查看更充分的所有pod详情信息
[root@linux-servertwo software]# kubectl get pods --all-namespaces -o wide
NAMESPACE      NAME                                      READY   STATUS    RESTARTS   AGE    IP           NODE              NOMINATED NODE   READINESS GATES
kube-flannel   kube-flannel-ds-lbmdr                     1/1     Running   0          5d     10.0.4.16    linux-servertwo            
kube-system    coredns-7f89b7bc75-47d56                  1/1     Running   0          6d4h   172.17.0.3   linux-servertwo            
kube-system    coredns-7f89b7bc75-h4c9b                  1/1     Running   0          6d4h   172.17.0.2   linux-servertwo            
kube-system    etcd-linux-servertwo                      1/1     Running   0          6d4h   10.0.4.16    linux-servertwo            
kube-system    kube-apiserver-linux-servertwo            1/1     Running   0          6d4h   10.0.4.16    linux-servertwo            
kube-system    kube-controller-manager-linux-servertwo   1/1     Running   0          5d     10.0.4.16    linux-servertwo            
kube-system    kube-proxy-j7d4v                          1/1     Running   0          6d4h   10.0.4.16    linux-servertwo            
kube-system    kube-scheduler-linux-servertwo            1/1     Running   0          5d     10.0.4.16    linux-servertwo            

1.5、查看节点日志

使用 journalctl -u kubelet 命令查看日志信息

# 动态查看 kubelet 日志的命令
[root@linux-servertwo software]# journalctl -u kubelet -f
Mar 19 15:53:58 linux-servertwo kubelet[25155]: I0319 15:53:58.023328   25155 reconciler.go:224] operationExecutor.VerifyControllerAttachedVolume started for volume "config-volume" (UniqueName: "kubernetes.io/configmap/9d6647d1-caeb-448a-9628-c5ac14821995-config-volume") pod "coredns-7f89b7bc75-47d56" (UID: "9d6647d1-caeb-448a-9628-c5ac14821995")
Mar 19 15:53:58 linux-servertwo kubelet[25155]: map[string]interface {}{"cniVersion":"0.3.1""hairpinMode":true"ipMasq":false"ipam":map[string]interface {}{"ranges":[][]map[string]interface {}{[]map[string]interface {}{map[string]interface {}{"subnet":"172.17.0.0/24"}}}, "routes":[]types.Route{types.Route{Dst:net.IPNet{IP:net.IP{0xac, 0x11, 0x0, 0x0}, Mask:net.IPMask{0xff, 0xff, 0x0, 0x0}}, GW:net.IP(nil)}}, "type":"host-local"}, "isDefaultGateway":true"isGateway":true"mtu":(*uint)(0xc00001c928), "name":"cbr0""type":"bridge"}
Mar 19 15:53:58 linux-servertwo kubelet[25155]: {"cniVersion":"0.3.1","hairpinMode":true,"ipMasq":false,"ipam":{"ranges":[[{"subnet":"172.17.0.0/24"}]],"routes":[{"dst":"172.17.0.0/16"}],"type":"host-local"},"isDefaultGateway":true,"isGateway":true,"mtu":1450,"name":"cbr0","type":"bridge"}
Mar 19 15:53:58 linux-servertwo kubelet[25155]: map[string]interface {}{"cniVersion":"0.3.1""hairpinMode":true"ipMasq":false"ipam":map[string]interface {}{"ranges":[][]map[string]interface {}{[]map[string]interface {}{map[string]interface {}{"subnet":"172.17.0.0/24"}}}, "routes":[]types.Route{types.Route{Dst:net.IPNet{IP:net.IP{0xac, 0x11, 0x0, 0x0}, Mask:net.IPMask{0xff, 0xff, 0x0, 0x0}}, GW:net.IP(nil)}}, "type":"host-local"}, "isDefaultGateway":true"isGateway":true"mtu":(*uint)(0xc0000a28e8), "name":"cbr0""type":"bridge"}

2、Namespaces

2.1、介绍

命名空间Namespaces主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离。它能够将资源划分为不同的分组,帮助我们在一个集群中划分不同的项目、团队或者环境(如开发、测试和生产环境),从而提高资源的隔离性和管理效率。

2.2、查看创建的命名空间

[root@linux-servertwo software]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   24h
kube-flannel      Active   19h
kube-node-lease   Active   24h
kube-public       Active   24h
kube-system       Active   24h

2.3、创建命名空间

[root@linux-servertwo software]# kubectl create namespace blog-dev
namespace/blog-dev created

# 再次查看-命名空间 blog-dev 创建成功
[root@linux-servertwo software]# kubectl get namespaces
NAME              STATUS   AGE
blog-dev          Active   17s
default           Active   24h
kube-flannel      Active   19h
kube-node-lease   Active   24h
kube-public       Active   24h
kube-system       Active   24h

2.4、删除命名空间

#删除
[root@linux-servertwo software]# kubectl delete namespace blog-dev
namespace "blog-dev" deleted

#再次查看-发现已经删除
[root@linux-servertwo software]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   24h
kube-flannel      Active   20h
kube-node-lease   Active   24h
kube-public       Active   24h
kube-system       Active   24h

2.5、检索指定命名空间

# 查看-检索指定命名空间
[root@linux-servertwo software]# kubectl get namespaces blog-dev
NAME       STATUS   AGE
blog-dev   Active   39s

# 查看-指定命名空间详情
[root@linux-servertwo software]# kubectl describe namespace blog-dev
Name:         blog-dev
Labels:      
Annotations:  
Status:       Active

No resource quota.
No LimitRange resource.

2.6、查看命名空间中的资源

[root@linux-servertwo software]# kubectl get pods -n blog-dev
No resources found in blog-dev namespace.
[root@linux-servertwo software]# kubectl get pod -n kube-system
NAME                                      READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-47d56                  1/1     Running   0          6d4h
coredns-7f89b7bc75-h4c9b                  1/1     Running   0          6d4h
etcd-linux-servertwo                      1/1     Running   0          6d4h
kube-apiserver-linux-servertwo            1/1     Running   0          6d4h
kube-controller-manager-linux-servertwo   1/1     Running   0          5d1h
kube-proxy-j7d4v                          1/1     Running   0          6d4h
kube-scheduler-linux-servertwo            1/1     Running   0          5d1h

2.7、输出yaml格式

# 查看-输出yaml格式
[root@linux-servertwo software]# kubectl get namespaces blog-dev -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2024-12-10T03:58:55Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:phase: {}
    manager: kubectl-create
    operation: Update
    time"2024-12-10T03:58:55Z"
  name: blog-dev
  resourceVersion: "106051"
  uid: 406d2440-b725-409f-9a01-2bf8c606e5cf
spec:
  finalizers:
  - kubernetes
status:
  phase: Active

# 输出一个yaml创建命名空间的格式,但不运行 
[root@linux-servertwo software]# kubectl create namespace blog-dev-1 --dry-run=client -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: blog-dev-1
spec: {}
status: {}

# 输出一个yaml创建命名空间的格式,但不运行 ,输出到主机blog-dev-1.yaml文件中
[root@linux-servertwo software]# kubectl create namespace blog-dev-1 --dry-run=client -o yaml > blog-dev-1.yaml

#查看输出的文件信息
[root@linux-servertwo software]# ls
blog-dev-1.yaml
[root@linux-servertwo software]# cat blog-dev-1.yaml 
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: null
  name: blog-dev-1
spec: {}
status: {}

2.8、YAML创建

# 声明文件创建
[root@linux-servertwo software]# kubectl apply -f blog-dev-1.yaml 
namespace/blog-dev-1 created
[root@linux-servertwo software]# kubectl get namespaces
NAME              STATUS   AGE
blog-dev          Active   3h11m
blog-dev-1        Active   9s
default           Active   27h
kube-flannel      Active   23h
kube-node-lease   Active   27h
kube-public       Active   27h
kube-system       Active   27h
nginx             Active   37m

2.9、YAML删除

# 删除命名空间-以yml的形式
[root@linux-servertwo software]# kubectl delete -f blog-dev-1.yaml 
namespace "blog-dev-1" deleted
[root@linux-servertwo software]# kubectl get namespaces
NAME              STATUS   AGE
blog-dev          Active   3h13m
default           Active   27h
kube-flannel      Active   23h
kube-node-lease   Active   27h
kube-public       Active   27h
kube-system       Active   27h
nginx             Active   38m

2.10、切换命名空间

# 通过设置默认命名空间,可以在运行其他命令时避免每次都加上 -n 
[root@linux-servertwo software]# kubectl config set-context --current --namespace=kube-system
Context "kubernetes-admin@kubernetes" modified.

# 查看pod
[root@linux-servertwo software]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
coredns-7f89b7bc75-47d56                  1/1     Running   0          6d4h
coredns-7f89b7bc75-h4c9b                  1/1     Running   0          6d4h
etcd-linux-servertwo                      1/1     Running   0          6d4h
kube-apiserver-linux-servertwo            1/1     Running   0          6d4h
kube-controller-manager-linux-servertwo   1/1     Running   0          5d1h
kube-proxy-j7d4v                          1/1     Running   0          6d4h
kube-scheduler-linux-servertwo            1/1     Running   0          5d1h

# 查看当前使用的命名空间
[root@linux-servertwo software]# kubectl config view --minify | grep namespace:
namespace: kube-system

# 设置为默认
[root@linux-servertwo software]# kubectl config set-context --current --namespace=default
Context "kubernetes-admin@kubernetes" modified.

[root@linux-servertwo software]# kubectl config view --minify | grep namespace:
namespace: default
    
[root@linux-servertwo software]# kubectl get pods
No resources found in default namespace.

3、Pod命令使用

3.1、介绍

PodKubernetes中可以创建和管理的最小部署单元,也是Kubernetes应用的基本运行单位,它封装了一个或多个容器和存储资源。Pod中的所有容器共享相同的网络命名空间、IP 地址、端口空间以及存储卷。通常情况下不直接管理 Pod,而是通过更高层次的抽象(如 Deployment、StatefulSet 等)来管理 Pod 的生命周期。下面就以安装nginx为例,单独介绍一下Pod的相关使用。

3.2、创建命名空间

首先创建好一个nginx的命名空间,做好数据隔离。

# 创建命名空间nginx
[root@linux-servertwo software]#  kubectl create namespace nginx
namespace/nginx created

# 列出所有Pod: kubectl get pods -n
[root@linux-servertwo software]# kubectl get pods -n nginx
No resources found in nginx namespace.

3.3、创建Pod

# 创建Pod 通过Pod控制器创建Pod
# 命令格式: kubectl run (pod控制器名称) [参数] 
# --image  指定Pod的镜像
# --port   指定端口
# --namespace  指定namespace
[root@linux-servertwo nginx]# kubectl run nginx --image nginx:latest --port 80 --namespace nginx
pod/nginx created

3.4、查看Pod信息

# 查看Pods信息
[root@linux-servertwo nginx]# kubectl get pods -n nginx
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          9m6s

# 更详细得查看pods信息
[root@linux-servertwo nginx]#  kubectl get pods -n nginx -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          15m   172.17.0.68   linux-servertwo            

# 查看详情Pod信息
[root@linux-servertwo nginx]# kubectl describe pods nginx -n nginx
Name:         nginx
Namespace:    nginx
Priority:     0
Node:         linux-servertwo/10.0.4.16
Start Time:   Wed, 11 Dec 2024 16:16:48 +0800
Labels:       run=nginx
Annotations:  
Status:       Running
IP:           172.17.0.68
IPs:
  IP:  172.17.0.68

......
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  9m29s  default-scheduler  Successfully assigned nginx/nginx to linux-servertwo
  Normal  Pulling    9m28s  kubelet            Pulling image "nginx:latest"
  Normal  Pulled     8m36s  kubelet            Successfully pulled image "nginx:latest"in 52.683702042s
  Normal  Created    8m35s  kubelet            Created container nginx
  Normal  Started    8m35s  kubelet            Started container nginx

3.5、验证创建成功

# 根据IP访问nginx,发现nginx启动成功
[root@linux-servertwo nginx]# curl 172.17.0.68:80


head>
Welcome to nginx!

html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }



Welcome to nginx!


If you see this page, the nginx web server is successfully installed and


working. Further configuration is required.

For online documentation and support please refer to


"http://nginx.org/">nginx.org.

Commercial support is available at
"http://nginx.com/">nginx.com.

Thank you 

for using nginx.