Pod管理一组容器,可以让容器有自恢复的能力

1 Pod

Pod 代表的是集群上处于运行状态的一组 容器的集合。

在从节点上使用systemctl status,可以查看Pod中的结构

image.png

Pod的容器类型有三种,应用容器,初始化容器和临时容器

1.1 多容器协同pod

将当前时间写入nginx的首页

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
  labels:
    app: multi-container-pod
spec:
  volumes:
    - name: nginxvol
      emptyDir: {}
  containers:
    - name: nginx-container
      image: nginx
      volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginxvol
    - name: content-container
      image: alpine
      command: ["/bin/sh","-c","while true; do sleep 1; date > /app/index.html; done;"]
      volumeMounts:
        - mountPath: /app
          name: nginxvol

验证结果

# 获取pod中的ip
kubectl get pod -o wide
# 访问nginx
curl 10.244.2.3
----------------------------------------------------------------------------------------
# 多容器时进入指定的容器
kubectl exec -it podName -c containerName -- /bin/sh

1.2 init容器(初始化容器)

启动应用容器之前需要运行完init容器,初始化容器必须有终结的时刻,一般不要用一直启动的镜像。

apiVersion: v1
kind: Pod
metadata:
  name: init-container-test
spec:
  containers:
  - name: web-app
    image: nginx
    volumeMounts:
    - name: workdir
      mountPath: /usr/share/nginx/html
  initContainers:
  - name: init
    image: alpine
    command: ['sh', '-c', 'echo "<h1>Hello, Kubernetes!</h1>" > /workdir/index.html']
    volumeMounts:
    - name: workdir
      mountPath: /workdir
  volumes:
  - name: workdir
    emptyDir: {}

验证结果

# 获取pod中的ip
kubectl get pod -o wide
# 访问nginx
curl 10.244.2.3