Net Namespace、IPC Namespace、UTS Namespace
静态pod
和控制器管理的pod
kubelet
守护进程管理, 不需要API 服务器看到它们kubelet
直接监控每个 Pod,并在其失效时重启之。由imagePullPolicy参数控制
默认的策略是:
# 创建
kubectl run nginx --image=nginx
pod/nginx created
# 查看
kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 13s
pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx1
spec:
containers:
- name: nginx
image: nginx
kubectl create -f pod.yaml
pod/nginx1 created
kubectl get pod|grep nginx1
nginx1 1/1 Running 0 2m10s
creat
或者apply
kubectl apply -f pod.yaml
pod/nginx1 configured
kubectl create -f pod.yaml
Error from server (AlreadyExists): error when creating "pod.yaml": pods "nginx1" already exists
kubectl get pod
来查看kubectl get pod
NAME READY STATUS RESTARTS AGE
pod 1/1 Running 0 21s
kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
pod 1/1 Running 0 28s
-o wide
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod 1/1 Running 0 38s 10.1.0.114 docker-desktop <none> <none>
kubectl describe pod pod-stress [-n namespace pod在非默认空间时需要指定pod所在的namespace]
Name: pod-stress # pod名称
Namespace: default # 所在命名空间
Priority: 0 # 优先级
Node: docker-desktop/192.168.65.4 # 所在节点
Start Time: Fri, 19 Aug 2022 11:24:15 +0800 # 创建时间
Labels: <none> # 标签
Annotations: <none> # 标签描述
Status: Running # pod状态
IP: 10.1.0.109 # Pod IP地址
IPs: # IP地址
IP: 10.1.0.109
Containers: # Pod中的容器信息
c1: # c1容器
Container ID: docker://b80d5e7b88cc207978da99ef508c0772a60ac693d94c0a59cd12266d6883aa57 # 容器ID
Image: polinux/stress # 镜像名
Image ID: docker-pullable://polinux/stress@sha256:b6144f84f9c15dac80deb48d3a646b55c7043ab1d83ea0a697c09097aaad21aa # 镜像ID
Port: <none> # 端口
Host Port: <none> # 主机端口
Command: # 启动命令
stress
Args: # 启动参数
--vm # CPU数
1
--vm-bytes # 内存数
100
--vm-hang # 进程数
1
State: Running # 容器状态
Started: Fri, 19 Aug 2022 11:24:33 +0800 # 容器启动时间
Ready: True # 是否就绪
Restart Count: 0 # 容器重启次数
Environment: <none> # 环境变量
Mounts: # 绑定位置
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-sqkts (ro)
Conditions:
Type Status
Initialized True # 初始化是否完成
Ready True # 是否ready
ContainersReady True # 容器是否ready
PodScheduled True # pod调度
Volumes:
kube-api-access-sqkts:
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 102s default-scheduler Successfully assigned default/pod-stress to k8s-worker1
Normal Pulling 102s kubelet Pulling image "polinux/stress"
Normal Pulled 83s kubelet Successfully pulled image "polinux/stress" in 18.944533343s
Normal Created 83s kubelet Created container c1
Normal Started 82s kubelet Started container c1
kubectl delete pod nginx
pod "nginx" deleted
kubectl delete -f pod.yaml
pod "nginx1" deleted
# 查看test空间下pod
kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
nginx1 1/1 Running 0 16m
nginx2 1/1 Running 0 16m
# 删除test空间
kubectl delete ns test
namespace "test" deleted
# 查看test空间下pod
kubectl get pod -n test
No resources found in test namespace.
kubectl get pod |awk 'NR >1 {print $1}' |xargs kubectl delete pod
pod "nginx1" deleted
pod "nginx2" deleted
kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx1 0/1 ContainerCreating 0 2s <none>
kubectl label pod nginx1 env=test zone=beijing
pod/nginx1 labeled
kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx1 1/1 Running 0 72s env=test,zone=beijing
apiVersion: v1
kind: Pod
metadata:
name: nginx2
labels:
env: dev
zone: A
spec:
containers:
- name: nginx
image: nginx
kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx1 1/1 Running 0 4m29s env=test,zone=beijing
nginx2 1/1 Running 0 46s env=dev,zone=A
kubectl get pod -l env=dev
NAME READY STATUS RESTARTS AGE
nginx2 1/1 Running 0 3m10s
kubectl get pod -l "env in(dev,test,prod)"
NAME READY STATUS RESTARTS AGE
nginx1 1/1 Running 0 7m31s
nginx2 1/1 Running 0 3m48s
kubectl label pod nginx1 env- zone-
pod/nginx1 labeled
kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx1 1/1 Running 0 8m59s <none>
nginx2 1/1 Running 0 5m16s env=dev,zone=A
kubectl exec pod名 -c 容器名 -- 命令
kubectl exec nginx1 -- date
Fri Aug 19 10:52:50 UTC 2022
kubectl exec -it nginx1 -c nginx -- /bin/bash
root@nginx1:/# ls
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@nginx1:/#
apiVersion: v1
kind: Namespace
metadata:
name: namespace1
---
apiVersion: v1
kind: Pod
metadata:
name: pod-stress2
namespace: namespace1
spec:
containers:
- name: c1
image: polinux/stress
imagePullPolicy: IfNotPresent
resources:
limits:
memory: "200Mi"
requests:
memory: "100Mi"
command: ["stress"] # 启动容器时执行的命令
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"] # 产生1个进程分配150M内存1秒后释放
apiVersion: v1
kind: Pod
metadata:
name: pod-share-network
spec:
containers:
- name: c1
image: nginx
- name: c2
image: nginx
imagePullPolicy: IfNotPresent
* 查看
```shell
kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-share-network 1/2 CrashLoopBackOff 13 44m