Pod是k8s集群进行管理的最小单元,程序要运行必须部署在容器中,而容器必须存在于Pod中。
Pod可以认为是容器的封装,一个Pod中可以存在一个或者多个容器。
k8s在集群启动之后,集群中的各个组件也都是以Pod方式运行的。可以通过下面命令查看:
[root@master ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-9d85f5447-k5cp6 0/1 Running 4 39h
coredns-9d85f5447-tjdfj 0/1 Running 3 39h
etcd-master 1/1 Running 3 39h
kube-apiserver-master 1/1 Running 7 39h
kube-controller-manager-master 1/1 Running 7 39h
kube-flannel-ds-amd64-blp6c 1/1 Running 2 28h
kube-flannel-ds-amd64-t949t 1/1 Running 2 28h
kube-flannel-ds-amd64-xfcn9 1/1 Running 2 28h
kube-proxy-6f4xl 1/1 Running 1 28h
kube-proxy-869gd 1/1 Running 2 28h
kube-proxy-qrc65 1/1 Running 3 39h
kube-scheduler-master 1/1 Running 7 39h
1 创建并运行
k8s没有提供单独运行pod的命令,都是通过Pod控制器来实现的。
# 命令格式: kubectl run Pod控制器名称 [参数]
# --image 指定pod的镜像
# --port 指定端口
# --namespace 指定namespace
[root@master ~]#kubectl run nginx --image=nginx:latest --port=80 --namespace dev
deployment.apps/nginx created
2 查看pod信息
[root@master ~]# kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
nginx-6867cdf567-966sx 1/1 Running 1 27h
[root@master ~]# kubectl get pod -n default -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6867cdf567-966sx 1/1 Running 1 27h 10.244.2.4 slave2 <none> <none>
[root@master ~]# kubectl describe pod nginx-6867cdf567-966sx
Name: nginx-6867cdf567-966sx
Namespace: default
Priority: 0
Node: slave2/192.168.247.138
Start Time: Wed, 29 Dec 2021 11:20:43 +0800
Labels: app=nginx
pod-template-hash=6867cdf567
Annotations: <none>
Status: Running
IP: 10.244.2.4
IPs:
IP: 10.244.2.4
Controlled By: ReplicaSet/nginx-6867cdf567
Containers:
nginx:
Container ID: docker://f07a9555828ab596071358f7de928f5ce4280a6521c4278dbb4a8b3b39c6a9c6
Image: nginx:1.14-alpine
Image ID: docker-pullable://nginx@sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
Port: <none>
Host Port: <none>
State: Running
Started: Wed, 29 Dec 2021 19:56:37 +0800
Last State: Terminated
Reason: Error
Exit Code: 255
Started: Wed, 29 Dec 2021 11:20:57 +0800
Finished: Wed, 29 Dec 2021 19:55:44 +0800
Ready: True
Restart Count: 1
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-ndkph (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-ndkph:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-ndkph
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
2 访问pod
# 获取pod的IP地址
[root@master ~]#kubectl get pods -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE ...
nginx 1/1 Running 0 190s 10.244.1.23 node1 ...
# 访问pod
[root@master ~]# curl http://10.244.1.23:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
3 删除指定pod
# 删除指定pod
[root@master ~]# kubectl delete pod nginx-6867cdf567-966sx
pod "nginx-6867cdf567-966sx" deleted
# 此时显示删除成功,但是再查询,发现又产生了一个
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6867cdf567-5777l 1/1 Running 0 7s
# 这是因为当前pod是由pod控制器创建的,控制器会监控pod状况,一旦发现pod死亡,会立即重建
# 此时若想删除pod,必须删除pod控制器
# 先查询一下当前namespace下的pod控制器
[root@master ~]# kubectl get deploy -n default
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 28h
# 接下来,删除刺pod的控制器
[root@master ~]# kubectl delete deploy nginx -n default
deployment.apps "nginx" deleted
# 稍等片刻,重新查询pod,发现pod被删除了
[root@master ~]# kubectl get pod
No resources found in default namespace.
# 也可以直接删除整个namespace
[root@master ~]# kubectl delete ns dev
namespace "dev" deleted
[root@master ~]# kubectl get pods -n dev
No resources found in dev namespace.
4 配置操作
创建一个pod-nginx.yaml,内容如下
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod # 注意这里是大写的P
metadata:
name: nginx
namespace: dev
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
然后就可以执行对应的创建和删除命令了:
创建: kubectl create -f pod-nginx.yaml
删除: kubectl delete -f pod-nginx.yaml