一、检查Kubernetes各个组件运行状态

  1. kubectl get cs
  2. 输出如下:
  3. Warning: v1 ComponentStatus is deprecated in v1.19+
  4. NAME STATUS MESSAGE ERROR
  5. # scheduler:调度服务,主要作用是将 POD 调度到 Node节点
  6. scheduler Healthy ok
  7. # controller-manager:自动化修复服务,主要作用是 Node 宕机后自动修复 Node 回到正常的工作状态
  8. controller-manager Healthy ok
  9. # etcd:kv数据库,用于记录用于记录各个组件健康状态(类似服务注册与发现)
  10. etcd-0 Healthy {"health":"true","reason":""}

二、检查 Master 状态

kubectl cluster-info

# 输出如下
Kubernetes control plane is running at https://192.168.87.110:6443
CoreDNS is running at https://192.168.87.110:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

三、检查 Nodes 状态

kubectl get nodes

# 输出如下
NAME                 STATUS   ROLES                  AGE   VERSION
kubernetes-node-01   Ready    <none>                 27m   v1.23.6
kubernetes-node-02   Ready    <none>                 26m   v1.23.6
node                 Ready    control-plane,master   29m   v1.23.6

四、查看运行时容器Pod

# 查看正在运行的Pod
# -n表示查看的命名空间namespace
kubectl get pods -n kube-system

五、运行第一个容器实例

# 使用 kubectl 命令创建两个监听 80 端口的 Nginx Pod(Kubernetes 运行容器的最小单元)
kubectl create deployment mynginx --image=nginx --replicas=2 --port=80

# 输出如下内容
deployment.apps/mynginx created

六、查看全部 Pods 的状态

kubectl get pods

# 输出如下
NAME                       READY   STATUS    RESTARTS      AGE
mynginx-54984d766f-blfrr   1/1     Running   2 (50m ago)   57m
mynginx-54984d766f-drqvq   1/1     Running   0             57m

七、查看已部署的服务

kubectl get deployment

# 输出如下
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
mynginx   2/2     2            2           58m

八、发布服务

# 使用负载均衡模式发布服务,让用户可以访问
kubectl expose deployment mynginx --port=80 --type=LoadBalancer

# 输出如下内容
service/mynginx exposed

九、查看已发布的服务

kubectl get services

# 输出如下
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        4h47m
mynginx      LoadBalancer   10.100.139.192   <pending>     80:31260/TCP   31s

十、查看服务详情

# kubectl describe service 发布的服务名称
kubectl describe service mynginx

# 输出如下
Name:                     mynginx
Namespace:                default
Labels:                   app=mynginx
Annotations:              <none>
Selector:                 app=mynginx
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.100.139.192
IPs:                      10.100.139.192
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
# 发布到外网的服务端口
NodePort:                 <unset>  31260/TCP
Endpoints:                10.244.140.67:80,10.244.141.201:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

通过浏览器访问 Node 服务器,此时 Kubernetes 会以负载均衡的方式访问部署的 Nginx 服务,能够正常看到 Nginx 的欢迎页即表示成功!31260就是通过发布服务的方式将端口发布到外网服务的端口。
http://192.168.87.110:31260/

十一、停止服务

# kubectl delete service 发布服务名称
kubectl delete service mynginx

# 输出如下
service "mynginx" deleted
# kubectl delete deployment 部署服务的名称
kubectl delete deployment

# 输出如下
deployment.apps "mynginx" deleted