查看node状态
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
centos7906 Ready <none> 3h42m v1.25.2
centos7907 Ready <none> 3h42m v1.25.2
centos7908 Ready <none> 3h42m v1.25.2
node Ready control-plane 3h43m v1.25.2
查看服务状态
$ kubectl get componentstatuses
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health":"true","reason":""}
查看pod
#查看默认命名空间的pod
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
filehttp 1/1 Running 0 8m26s
#查看详细的
$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
filehttp 1/1 Running 0 8m31s 10.244.86.130 centos7907 <none> <none>
#查看指定命名空间的详细的
$ kubectl get pod -o wide -n kube-system
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
calico-kube-controllers-58dbc876ff-68bhz 1/1 Running 0 3h43m 10.244.167.130 node <none> <none>
calico-node-bjg58 1/1 Running 0 3h43m 10.0.0.8 centos7908 <none> <none>
calico-node-bn48b 1/1 Running 0 3h43m 10.0.0.7 centos7907 <none> <none>
calico-node-gbqpz 1/1 Running 0 3h43m 10.50.34.106 node <none> <none>
calico-node-j2l5m 1/1 Running 0 3h43m 10.0.0.6 centos7906 <none> <none>
coredns-c676cc86f-27nl4 1/1 Running 0 3h45m 10.244.167.129 node <none> <none>
coredns-c676cc86f-5gmfm 1/1 Running 0 3h45m 10.244.167.131 node <none> <none>
etcd-node 1/1 Running 0 3h45m 10.50.34.106 node <none> <none>
kube-apiserver-node 1/1 Running 0 3h45m 10.50.34.106 node <none> <none>
kube-controller-manager-node 1/1 Running 0 3h45m 10.50.34.106 node <none> <none>
kube-proxy-fz2ll 1/1 Running 0 3h44m 10.0.0.7 centos7907 <none> <none>
kube-proxy-grqvz 1/1 Running 0 3h43m 10.0.0.8 centos7908 <none> <none>
kube-proxy-wmrvd 1/1 Running 0 3h44m 10.0.0.6 centos7906 <none> <none>
kube-proxy-wxcpv 1/1 Running 0 3h45m 10.50.34.106 node <none> <none>
kube-scheduler-node 1/1 Running 0 3h45m 10.50.34.106 node <none> <none>
#查看指定标签的
$ kubectl get pod -o wide -l app=http
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
filehttp 1/1 Running 0 10m 10.244.86.130 centos7907 <none> <none>
查看pod详细信息
$ kubectl describe pod filehttp
Name: filehttp
Namespace: default
Priority: 0
Service Account: default
Node: centos7907/10.0.0.7
Start Time: Thu, 13 Oct 2022 04:26:08 +0800
Labels: app=http
Annotations: cni.projectcalico.org/containerID: bd3a03d6a2cf0a89675e912be783fa1470a129be6059641a5d82c0f0ac51d1c5
cni.projectcalico.org/podIP: 10.244.86.130/32
cni.projectcalico.org/podIPs: 10.244.86.130/32
Status: Running
IP: 10.244.86.130
IPs:
IP: 10.244.86.130
Containers:
http1:
Container ID: containerd://da80b41fe55aac4a16518ff1531f3ec32e28b331ccc7a9e9d67b606d3748a4dd
Image: httpd:latest
Image ID: docker.io/library/httpd@sha256:4400fb49c9d7d218d3c8109ef721e0ec1f3897028a3004b098af587d565f4ae5
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 13 Oct 2022 04:26:43 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-lxz78 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-lxz78:
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: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 11m default-scheduler Successfully assigned default/filehttp to centos7907
Normal Pulling 11m kubelet Pulling image "httpd:latest"
Normal Pulled 10m kubelet Successfully pulled image "httpd:latest" in 33.820714165s
Normal Created 10m kubelet Created container http1
Normal Started 10m kubelet Started container http1
查看所有命名空间
$ kubectl get namespaces
NAME STATUS AGE
default Active 3h49m
kube-node-lease Active 3h49m
kube-public Active 3h49m
kube-system Active 3h49m
查看对应控制器的字段
查询pod中metadata中的字段
$ kubectl explain pod.metadata
KIND: Pod
VERSION: v1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations
.............................................
uid <string>
UID is the unique in time and space value for this object. It is typically
generated by the server on successful creation of a resource and is not
allowed to change on PUT operations.
Populated by the system. Read-only. More info:
http://kubernetes.io/docs/user-guide/identifiers#uids
进入pod
filehttp是对应的pod名 http1是pod中对应的容器名
#进入pod中,执行命令bash
$ kubectl exec -it filehttp -- bash
#进入pod中指定的容器,并执行命令bash
$ kubectl exec -it filehttp -c http1 -- bash
查看pod的日志
$ kubectl logs filehttp
删除资源
这里以pod为例
直接删除
$ kubectl delete pod filehttp
通过配置文件删除
$ kubectl delete -f 1.yaml
查看pod具有哪些标签
$ kubectl get pod filehttp --show-labels
NAME READY STATUS RESTARTS AGE LABELS
filehttp 1/1 Running 0 21s app=http
$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
filehttp 1/1 Running 0 28s app=http
强制删除pod
带上--force --grace-period 0
表示强制删除,并且不等待
$ kubectl delete -f 2.yaml --force --grace-period 0
补充编写资源文件
可以通过kubectl explain ...
来查询字段
例如
查询pod的字段
$ kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
查询pod中metadata中的字段
$ kubectl explain pod.metadata
KIND: Pod
VERSION: v1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations
.............................................
uid <string>
UID is the unique in time and space value for this object. It is typically
generated by the server on successful creation of a resource and is not
allowed to change on PUT operations.
Populated by the system. Read-only. More info:
http://kubernetes.io/docs/user-guide/identifiers#uids
一个简单的pod配置文件
---
apiVersion: v1
kind: Pod
metadata:
name: filehttp
labels:
app: http
spec:
containers:
- name: http1
ports:
- containerPort: 80
image: httpd:latest
imagePullPolicy: IfNotPresent
注意:
- 如果字段后面带有 -required- 标记,则表示该字段为必需有的字段