【容器应用系列教程】Kubernetes的Pod管理
【容器应用系列教程】Kubernetes的Pod管理
一、关于Pod
k8s
集群所能管理的最小单元- 一个
pod
只放一个容器 - 支持一个
pod
放多个容器,不建议实际使用
二、Pod管理操作
1.创建Pod
简单yaml
文件示例
[root@k8s-master wordpress]# vim centos.yml
apiVersion: v1 #api版本
kind: Pod #资源类型Pod
metadata: #元数据
name: centos #名字
namespace: wordpress #指定在哪个命名空间里
spec:
containers: #容器信息
- name: centos #容器名字
image: centos:7 #使用的镜像
tty: true #模拟一个终端
command: #启动容器时,执行的命令
- bash
apiVersion:
指定api server
版本,目前是v1
kind:
说明要创建的资源类型metadata:
指定Pod
的元数据信息name:
指定pod
名称namespace:
指定Pod
所在的命名空间,不写代表default
spec:
定义pod
中运行的容器的具体信息
[root@k8s-master wordpress]# kubectl create -f ./centos.yml #创建Pod
pod/centos created
2.查看Pod
[root@k8s-master wordpress]# kubectl get pods -n wordpress
NAME READY STATUS RESTARTS AGE
centos 1/1 Running 0 64s
-n
指定命名空间
查看Pod
的详细信息
[root@k8s-master wordpress]# kubectl get pods -n wordpress -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos 1/1 Running 0 6m59s 192.168.201.202 k8s-node01.linux.com <none> <none>
-o wide
显示详细信息
查看所有Pod
[root@k8s-master wordpress]# kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-5f6d4b864b-2pqz6 1/1 Running 1 3d
kube-system calico-node-fld77 1/1 Running 1 2d23h
kube-system calico-node-h2kxd 1/1 Running 1 2d23h
kube-system calico-node-vfltp 1/1 Running 1 2d23h
kube-system coredns-54d67798b7-cbq9r 1/1 Running 1 2d23h
kube-system coredns-54d67798b7-ch5hw 1/1 Running 1 2d23h
kube-system etcd-k8s-master.linux.com 1/1 Running 1 3d2h
kube-system kube-apiserver-k8s-master.linux.com 1/1 Running 1 3d2h
kube-system kube-controller-manager-k8s-master.linux.com 1/1 Running 1 3d2h
kube-system kube-proxy-6pq4t 1/1 Running 1 3d2h
kube-system kube-proxy-gb66k 1/1 Running 1 3d2h
kube-system kube-proxy-zcjrc 1/1 Running 1 3d2h
kube-system kube-scheduler-k8s-master.linux.com 1/1 Running 1 3d2h
wordpress centos 1/1 Running 0 5m17s
查看所有Pod
的详细信息
[root@k8s-master wordpress]# kubectl get pods -A -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system calico-kube-controllers-5f6d4b864b-2pqz6 1/1 Running 1 3d 192.168.140.10 k8s-master.linux.com <none> <none>
kube-system calico-node-fld77 1/1 Running 1 3d 192.168.140.11 k8s-node01.linux.com <none> <none>
kube-system calico-node-h2kxd 1/1 Running 1 3d 192.168.140.12 k8s-node02.linux.com <none> <none>
kube-system calico-node-vfltp 1/1 Running 1 3d 192.168.140.10 k8s-master.linux.com <none> <none>
kube-system coredns-54d67798b7-cbq9r 1/1 Running 1 3d 192.168.201.194 k8s-node01.linux.com <none> <none>
kube-system coredns-54d67798b7-ch5hw 1/1 Running 1 3d 192.168.242.130 k8s-node02.linux.com <none> <none>
kube-system etcd-k8s-master.linux.com 1/1 Running 1 3d2h 192.168.140.10 k8s-master.linux.com <none> <none>
kube-system kube-apiserver-k8s-master.linux.com 1/1 Running 1 3d2h 192.168.140.10 k8s-master.linux.com <none> <none>
kube-system kube-controller-manager-k8s-master.linux.com 1/1 Running 1 3d2h 192.168.140.10 k8s-master.linux.com <none> <none>
kube-system kube-proxy-6pq4t 1/1 Running 1 3d2h 192.168.140.11 k8s-node01.linux.com <none> <none>
kube-system kube-proxy-gb66k 1/1 Running 1 3d2h 192.168.140.10 k8s-master.linux.com <none> <none>
kube-system kube-proxy-zcjrc 1/1 Running 1 3d2h 192.168.140.12 k8s-node02.linux.com <none> <none>
kube-system kube-scheduler-k8s-master.linux.com 1/1 Running 1 3d2h 192.168.140.10 k8s-master.linux.com <none> <none>
wordpress centos 1/1 Running 0 8m53s 192.168.201.202 k8s-node01.linux.com <none> <none>
3.查看某一个Pod
创建信息和详细信息
也可以用来判断长时间处于创建状态的
Pod
或者异常状态的Pod
[root@k8s-master wordpress]# kubectl describe pods centos -n wordpress
Name: centos
Namespace: wordpress
Priority: 0
Node: k8s-node01.linux.com/192.168.140.11
Start Time: Mon, 12 Jun 2023 18:50:12 +0800
Labels: <none>
Annotations: <none>
Status: Running
IP: 192.168.201.202
IPs:
IP: 192.168.201.202
Containers:
centos:
Container ID: docker://e25dd6ee071b53d167c42148ae81b3f9ac26d5933b9025e04898c0a89d6212b7
Image: centos:7
Image ID: docker-pullable://centos@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
Port: <none>
Host Port: <none>
Command:
bash
State: Running
Started: Mon, 12 Jun 2023 18:50:13 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-f9cxl (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-f9cxl:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-f9cxl
Optional: false
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 10m default-scheduler Successfully assigned wordpress/centos to k8s-node01.linux.com
Normal Pulled 10m kubelet Container image "centos:7" already present on machine
Normal Created 10m kubelet Created container centos
Normal Started 10m kubelet Started container centos
4.连接Pod
[root@k8s-master wordpress]# kubectl exec -it centos -n wordpress -- /bin/bash
[root@centos /]#
5.删除Pod
[root@k8s-master wordpress]# kubectl delete -f ./centos.yml
pod "centos" deleted
或者使用下面的命令,直接删除
Pod
[root@k8s-master wordpress]# kubectl delete pod centos -n wordpress
pod "centos" deleted
三、containers
常用选项
apiVersion: v1
kind: 资源类型
metadata:
name: 自定义名字
namespace: 命名空间名
spec:
containers:
选项:参数
选项:参数
1.name
容器名称
spec:
containers:
- name: centos #自定义容器名称
2.image
镜像名字
spec:
containers:
- name: centos
image: centos:7 #使用Centos镜像7版本
3.imagePullPolicy: [Always | Never | IfNotPresent ]
拉取镜像策略
Always
: 每次执行yaml文件时,都重新拉取镜像Never
: 不拉取镜像IfNotPresent
: 如果本地没有,则拉取镜像,有则直接使用
spec:
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
4.restartPolicy [ Always | Never | OnFailure]
容器重启动策略
Always:
只要容器挂掉,都重启Never:
容器挂掉,忽略不管OnFailure:
只有在容器异常退出时,重启;如果是正常退出,则不管- 资源类型为
Pod
时无法使用,只有类型是Deployment
时才有效
5.command
启动容器时执行的命令
spec:
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
tty: true
command:
- bash
args
向command
传递参数
apiVersion: v1
kind: Pod
metadata:
name: centos
namespace: wordpress
spec:
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
command:
- ping
args:
- "baidu.com"
6.ports
说明容器要发布的端口
不是服务发布,仅仅是说明容器要发布的端口
apiVersion: v1
kind: Pod
metadata:
name: centos
namespace: wordpress
spec:
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
command:
- ping
- "baidu.com" #上面命令参数,也可以这么写
ports:
- containerPort: 22 #说明要发布22号端口
[root@k8s-master wordpress]# kubectl describe pod centos -n wordpress | grep -i "port"
Port: 22/TCP
Host Port: 0/TCP
7.env
向容器内传递变量
apiVersion: v1
kind: Pod
metadata:
name: centos
namespace: wordpress
spec:
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
command:
- ping
- "baidu.com"
ports:
- containerPort: 22
env:
- name: wsjj #定义变量wsjj
value: wangshengjj #变量值为wangshengjj
[root@k8s-master wordpress]# kubectl exec -it centos -n wordpress -- bash
[root@centos /]# echo $wsjj
wangshengjj
8.resources
资源限制
- 容器
cpu
,内存
资源限制 1000豪核 = 1CPU
apiVersion: v1
kind: Pod
metadata:
name: centos
namespace: wordpress
spec:
containers:
- name: centos
image: centos:7
imagePullPolicy: IfNotPresent
command:
- ping
- "baidu.com"
resources:
requests: #软限制
memory: "1G"
cpu: "500m" #1核的一半性能
limits: #硬限制
memory: "2G"
cpu: "1" #最多1核
9.健康状态检查、探针
检查方式
LivenessProbe
- 判断容器是否健康,如果不健康, kubelet将删除该容器并根据重启策略做相应的处理
ReadinessProbe
- 判断容器是否启动完成且准备接受请求;如果检测失败,则
endpoint
会被从service
对应的endpoint
中删除
- 判断容器是否启动完成且准备接受请求;如果检测失败,则
livenessProbe:
httpGet: #使用get方法检测
path: /check #指定uri地址
port: 80 #端口80
initialDelaySeconds: 3 #容器启动前3秒不检测
periodSeconds: 3 #检测3秒
10.案例
创建一个Mysql5.7
的Pod
[root@k8s-master wordpress]# vim ./mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
namespace: wordpress
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "WWW.1.com"
resources:
requests:
memory: "1G"
cpu: "1"
limits:
memory: "2G"
cpu: "2"
[root@k8s-master wordpress]# kubectl create -f ./mysql.yaml
pod/mysql created
[root@k8s-master wordpress]# kubectl exec -it mysql -n wordpress -- bash
bash-4.2# mysql -uroot -pWWW.1.com
mysql>
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WangShengJJのblog!