一、配置文件方式配置Pod的名称和镜像
1.1 编写pod_base.yaml文件
如下,设置命名空间dev,pod中指定命名空间为dev,pod中有一个个容器,名称分别为nginx镜像分别使用1.17.1和1.17.3版本
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: pod-base
namespace: dev
labels:
user: redrose2100
spec:
containers:
- name: nginx
image: nginx:1.17.1
1.2 启动Pod
使用如下命令启动pod
[root@master pod]# kubectl apply -f pod_base.yaml
namespace/dev created
pod/pod-base created
[root@master pod]#
1.3 查看创建的Pod
使用如下命令查看pod的详细信息以及标签等
[root@master pod]# kubectl get pod -n dev -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pod-base 1/1 Running 0 12s 10.244.2.24 node2 <none> <none> user=redrose2100
[root@master pod]#
通过如下命令可以查看Pod的更详细的信息和创建详细过程
[root@master pod]# kubectl describe pod pod-base -n dev
Name: pod-base
Namespace: dev
Priority: 0
Node: node2/192.168.16.42
Start Time: Mon, 21 Mar 2022 15:19:17 +0800
Labels: user=redrose2100
Annotations: <none>
Status: Running
IP: 10.244.2.24
IPs:
IP: 10.244.2.24
Containers:
nginx:
Container ID: docker://5d2b7707956c7fc91dfec0b705917bfa64bb91e618450e946482d5621659c9aa
Image: nginx:1.17.1
Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 21 Mar 2022 15:19:18 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-42jtr (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-42jtr:
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 94s default-scheduler Successfully assigned dev/pod-base to node2
Normal Pulled 93s kubelet Container image "nginx:1.17.1" already present on machine
Normal Created 93s kubelet Created container nginx
Normal Started 93s kubelet Started container nginx
[root@master pod]#
1.4 删除创建的Pod
使用如下命令即可删除
[root@master pod]# kubectl delete -f pod_base.yaml
namespace "dev" deleted
pod "pod-base" deleted
[root@master pod]#