[root@master ~]# kubectl get namespaces
NAME STATUS AGE
default Active 2d1h
kube-flannel Active 2d1h
kube-node-lease Active 2d1h
kube-public Active 2d1h
kube-system Active 2d1h
[root@master ~]# kubectl create namespace timinglee
namespace/timinglee created
[root@master ~]# kubectl get namespaces
NAME STATUS AGE
default Active 2d1h
kube-flannel Active 2d1h
kube-node-lease Active 2d1h
kube-public Active 2d1h
kube-system Active 2d1h
timinglee Active 22s
[root@master ~]# kubectl delete namespaces timinglee
[root@master ~]# kubectl get namespaces
NAME STATUS AGE
default Active 2d1h
kube-flannel Active 2d1h
kube-node-lease Active 2d1h
kube-public Active 2d1h
kube-system Active 2d1h
2.pod管理
bash复制代码
[root@master ~]# ls
anaconda-ks.cfg flannel kube-flannel-v0.28.9.tar.gz nginx-latest.tar.gz y y.pub
[root@master ~]# docker load -i nginx-latest.tar.gz
Loaded image: nginx:latest
[root@master ~]# kubectl run lee --image nginx:latest
pod/lee created
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
lee 0/1 ContainerCreating 0 7s <none> node2 <none> <none>
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
lee 0/1 ImagePullBackOff 0 11m 10.244.2.3 node2 <none> <none>
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
lee 0/1 ImagePullBackOff 0 12m 10.244.2.3 node2 <none> <none>
[root@master ~]# kubectl describe pods lee
Name: lee
Namespace: default
Priority: 0
Service Account: default
Node: node2/192.168.106.183
Start Time: Tue, 25 Aug 2026 19:21:52 +0800
Labels: run=lee
Annotations: <none>
Status: Pending
IP: 10.244.2.3
IPs:
IP: 10.244.2.3
Containers:
lee:
Container ID:
Image: nginx:latest
Image ID:
Port: <none>
Host Port: <none>
State: Waiting
Reason: ImagePullBackOff
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7c9jn (ro)
Conditions:
Type Status
PodReadyToStartContainers True
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
kube-api-access-7c9jn:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
Optional: false
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 12m default-scheduler Successfully assigned default/lee to node2
Normal SandboxChanged 12m kubelet Pod sandbox changed, it will be killed and re-created.
Normal Pulling 8m19s (x5 over 12m) kubelet spec.containers{lee}: Pulling image "nginx:latest"
Warning Failed 7m58s (x5 over 12m) kubelet spec.containers{lee}: Failed to pull image "nginx:latest": Error response from daemon: failed to resolve reference "docker.io/library/nginx:latest": docker.io/library/nginx:latest: not found
Warning Failed 7m58s (x5 over 12m) kubelet spec.containers{lee}: Error: ErrImagePull
Normal BackOff 2m20s (x35 over 12m) kubelet spec.containers{lee}: Back-off pulling image "nginx:latest"
Warning Failed 2m6s (x36 over 12m) kubelet spec.containers{lee}: Error: ImagePullBackOff
[root@master ~]# kubectl delete pods lee
pod "lee" deleted from default namespace
[root@master ~]# kubectl delete pods --all
No resources found
[root@master ~]# kubectl create deployment webcluster --replicas 2 --image myapp:v1
deployment.apps/webcluster created
[root@master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
webcluster 1/2 2 1 5s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-kntt6 1/1 Running 0 12s
webcluster-77c87d9946-t87hg 1/1 Running 0 12s
[root@master ~]# kubectl delete deployments.apps webcluster
deployment.apps "webcluster" deleted from default namespace
[root@master ~]# kubectl get pods
No resources found in default namespace.
2.edit
bash复制代码
[root@master ~]# kubectl create deployment webcluster --image myapp:v1
deployment.apps/webcluster created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-dkmgs 1/1 Running 0 4s
[root@master ~]# kubectl edit deployments.apps webcluster
deployment.apps/webcluster edited
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-dkmgs 1/1 Running 0 38s
webcluster-77c87d9946-hk8fb 1/1 Running 0 3s
3.patch
bash复制代码
[root@master ~]# kubectl patch deployments.apps webcluster -p '{"spec":{"replicas":1}}'
deployment.apps/webcluster patched
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-dkmgs 1/1 Running 0 2m40s
[root@k8s-master ~]# docker load -i busybox-latest.tar.gz
Loaded image: busybox:latest
[root@k8s-master ~]# docker tag busybox:latest reg.timinglee.org/library/busybox:latest
[root@k8s-master ~]#docker push reg.timinglee.org/library/busybox:latest
[root@k8s-master ~]# kubectl run -it testpod --image busybox:latest
All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.
If you don't see a command prompt, try pressing enter.
/ #
/ #
/ # <ctrl+pq>
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 55s
[root@k8s-master ~]# kubectl attach pods/testpod -it
All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.
If you don't see a command prompt, try pressing enter.
/ #
/ #
/ #
7.exec
bash复制代码
[root@master ~]# docker load -i nginx-latest.tar.gz
Loaded image: nginx:latest
[root@master ~]# docker tag nginx:latest reg.timinglee.org/library/nginx:latest
[root@master ~]# docker push reg.timinglee.org/library/nginx:latest
The push refers to repository [reg.timinglee.org/library/nginx]
ec1a2ca4ac87: Pushed
9853575bc4f9: Pushed
72db5db515fd: Pushed
8b87c0c66524: Pushed
55e54df86207: Pushed
f4f00eaedec7: Pushed
5f0272c6e96d: Pushed
latest: digest: sha256:7d2ee22b1699d37a35181e47d42a275803f3acf547c22a41bb8a53891c7b4f2c size: 1307
[root@master ~]# kubectl run testpod --image nginx:latest
pod/testpod created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 4s
webcluster-77c87d9946-85gz2 1/1 Running 0 6m18s
[root@master ~]# kubectl exec -it pods/testpod -c testpod -- /bin/bash
root@testpod:/#
8.cp
bash复制代码
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 4s
webcluster-77c87d9946-85gz2 1/1 Running 0 8m30s
[root@master ~]# kubectl cp testpod:/usr/share/nginx/html/index.html /mnt/test
tar: Removing leading `/' from member names
[root@master ~]# kubectl cp testpod:/usr/share/nginx/html /mnt/
tar: Removing leading `/' from member names
[root@master ~]# ls /mnt/
50x.html hgfs index.html test
[root@master ~]# echo timinglee > /mnt/index.html
[root@master ~]# kubectl cp /mnt/index.html testpod:/usr/share/nginx/html/index.html
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 38s 10.244.1.8 node1 <none> <none>
webcluster-77c87d9946-85gz2 1/1 Running 0 9m4s 10.244.2.6 node2 <none> <none>
[root@master ~]# curl 10.244.1.8
timinglee
9.rollout
bash复制代码
[root@master ~]# kubectl create deployment webcluster --image myapp:v1 --replicas 2 --dry-run=client -o yaml > webcluster.yml
[root@master ~]# vim webcluster.yml
[root@master ~]# vim webcluster.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: webcluster
name: webcluster
spec:
replicas: 2
selector:
matchLabels:
app: webcluster
template:
metadata:
labels:
app: webcluster
spec:
containers:
- image: myapp:v1
name: myapp
[root@master ~]# kubectl apply -f webcluster.yml
Warning: resource deployments/webcluster is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
deployment.apps/webcluster configured
[root@master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
webcluster 2/2 2 2 27m
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 4m26s
webcluster-77c87d9946-85gz2 1/1 Running 0 12m
webcluster-77c87d9946-t9djf 1/1 Running 0 46s
[root@master ~]# kubectl rollout status deployment webcluster
deployment "webcluster" successfully rolled out
[root@master ~]# kubectl rollout restart deployment webcluster
deployment.apps/webcluster restarted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 4m51s
webcluster-858c7856b7-kwrh5 1/1 Running 0 6s
webcluster-858c7856b7-lxjpl 1/1 Running 0 5s
[root@master ~]# kubectl rollout restart deployment webcluster
deployment.apps/webcluster restarted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 5m
webcluster-858c7856b7-lxjpl 0/1 Completed 0 14s
webcluster-949947d88-pkwjq 1/1 Running 0 4s
webcluster-949947d88-vkt5b 1/1 Running 0 2s
10.scale
bash复制代码
[root@master ~]# kubectl scale deployment webcluster --replicas 4
deployment.apps/webcluster scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 6m59s
webcluster-949947d88-bpc8p 1/1 Running 0 4s
webcluster-949947d88-l6qkh 1/1 Running 0 4s
webcluster-949947d88-pkwjq 1/1 Running 0 2m3s
webcluster-949947d88-vkt5b 1/1 Running 0 2m1s
[root@master ~]# kubectl scale deployment webcluster --replicas 1
deployment.apps/webcluster scaled
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 7m9s
webcluster-949947d88-vkt5b 1/1 Running 0 2m11s
11.label
bash复制代码
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
testpod 1/1 Running 0 12m run=testpod
webcluster-949947d88-vkt5b 1/1 Running 0 7m37s app=webcluster,pod-template-hash=949947d88
[root@master ~]# kubectl get deployments.apps webcluster --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
webcluster 1/1 1 1 35m app=webcluster
[root@master ~]# kubectl label pods webcluster-9787d97f6-zl6q2 app-
Error from server (NotFound): pods "webcluster-9787d97f6-zl6q2" not found
[root@master ~]# kubectl label pods webcluster-949947d88-vkt5b app-
pod/webcluster-949947d88-vkt5b unlabeled
[root@master ~]# kubectl label pods webcluster-949947d88-vkt5b app=webcluster
pod/webcluster-949947d88-vkt5b labeled