Kubernetes 是一个由主节点和工作节点组成的容器编排工具。它只允许通过作为控制平面核心组件的 API 服务器进行通信。API 服务器公开了一个 HTTP REST API,允许内部组件(如用户和集群)和外部组件之间的通信。
你可以将 API 服务器视为 Kubernetes 的主要用户界面或前端。它使您能够查询、更新或管理 Kubernetes 对象或资源的状态。为了建立这些交互,Kubernetes API 可以直接发出 REST 请求、使用客户端库或通过kubectl 命令行接收直接命令。
kubectl 可以帮助您执行各种操作,包括:
部署容器化应用
运行 Kubernetes 操作
监控任务
检查和管理集群资源
查看系统日志
kubectl 是kubernetes的一个管理工具,需要安装,并且一般安装在主节点上。
kubeclt 的语法:
kubectl [command] [TYPE] [NAME] [flags]
使用 kubectl get 操作列出一个或多个资源
kubectl get nodes
1、查看所有的pod
kubectl get pod -A
2、指定名称空间,查看对应名称空间的pod, -n 后面是名称空间
[root@k8s-master01 /]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-7cc8dd57d9-wrcgh 1/1 Running 0 138d
calico-node-9qpvq 1/1 Running 0 138d
calico-node-f8ww5 1/1 Running 0 138d
calico-node-gfrkb 1/1 Running 0 138d
coredns-7449ff9459-csbkf 1/1 Running 0 138d
nacos-7bb9bd6dc-g4665 1/1 Running 0 135d
sfnacos-94975b78d-zfvhn 1/1 Running 0 78d
3、指定名称空间,查看pod详情,加 -o wide
[root@k8s-master01 /]# kubectl get pod -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
calico-kube-controllers-7cc8dd57d9-wrcgh 1/1 Running 0 138d 10.88.0.2 k8s-master01 <none> <none>
calico-node-9qpvq 1/1 Running 0 138d 172.16.0.48 k8s-node01 <none> <none>
calico-node-f8ww5 1/1 Running 0 138d 172.16.0.150 k8s-master01 <none> <none>
calico-node-gfrkb 1/1 Running 0 138d 172.16.0.147 k8s-node02 <none> <none>
coredns-7449ff9459-csbkf 1/1 Running 0 138d 172.7.32.129 k8s-master01 <none> <none>
nacos-7bb9bd6dc-g4665 1/1 Running 0 135d 172.7.32.135 k8s-master01 <none> <none>
sfnacos-94975b78d-zfvhn 1/1 Running 0 78d 172.7.32.181 k8s-master01 <none> <none>
[root@k8s-master01 /]# kubectl get deployment -n kube-system
NAME READY UP-TO-DATE AVAILABLE AGE
calico-kube-controllers 1/1 1 1 138d
coredns 1/1 1 1 138d
nacos 1/1 1 1 138d
sfnacos 1/1 1 1 78d
[root@k8s-master01 /]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 139d <none>
[root@k8s-master01 /]# kubectl describe pod calico-kube-controllers -n kube-system
Name: calico-kube-controllers-7cc8dd57d9-wrcgh
Namespace: kube-system
Priority: 2000000000
Priority Class Name: system-cluster-critical
Node: k8s-master01/172.16.0.150
Start Time: Fri, 24 Feb 2023 10:03:31 +0800
Labels: k8s-app=calico-kube-controllers
pod-template-hash=7cc8dd57d9
Annotations: <none>
Status: Running
IP: 10.88.0.2
IPs:
IP: 10.88.0.2
IP: 2001:4860:4860::2
Controlled By: ReplicaSet/calico-kube-controllers-7cc8dd57d9
Containers:
calico-kube-controllers:
Container ID: containerd://d56f786700e356de6b4bf59af4f15c22949bc4caf1b4420ee31feb9d714556b0
Image: docker.io/calico/kube-controllers:v3.19.4
Image ID: docker.io/calico/kube-controllers@sha256:b15521e60d8bb04a501fe0ef4bf791fc8c164a175dd49a2328fb3f2b89838a68
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 24 Feb 2023 10:04:07 +0800
Ready: True
Restart Count: 0
Liveness: exec [/usr/bin/check-status -l] delay=10s timeout=1s period=10s #success=1 #failure=6
Readiness: exec [/usr/bin/check-status -r] delay=0s timeout=1s period=10s #success=1 #failure=3
Environment:
ENABLED_CONTROLLERS: node
DATASTORE_TYPE: kubernetes
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l9tcm (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-l9tcm:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: kubernetes.io/os=linux
Tolerations: CriticalAddonsOnly op=Exists
node-role.kubernetes.io/master:NoSchedule
node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
比如安装calico, 安装文件为calico.yaml, 直接apply 就可创建calico的资源
kubectl apply -f calico.yaml
kubectl delete -f calico.yaml
kubectl delete deployment nkafka-deployment-1 -n kafka
nkafka-deployment-1 :deployment 名称
-n kafka : 名称空间
kubectl delete pod calico-node-dmwpf -n kube-system
calico-node-dmwpf : pod 名称
-n kube-system : 名称空间
kubectl delete pod kube-system calico-kube-controllers-7cc8dd57d9-4l9kn --force --grace-period=0 -n kube-system
kube-system: 名称空间
calico-kube-controllers-7cc8dd57d9-4l9kn: pod名称