kubernetes 实践四:Pod详解

本篇是关于k8s的Pod,主要包括Pod和容器的使用、Pod的控制和调度管理、应用配置管理等内容。

Pod的定义

Pod是k8s的核心概念一直,就名字一样,是k8s中一个逻辑概念。Pod是docekr容器的集合,每个Pod中至少有一个Pause容器和业务容器。和docker容器关注单个可用的资源不同,Pod更多在应用层的角度,将多个docker容器组合来实现作为一个应用,它是k8s中最小的资源单位。

结合docker本身容器的特性,Pod中所有容器都是共享资源,如磁盘、网络、CPU、内存等,同时,一个Pod共用一个网络。

以下的yaml格式的Pod定义文件:

apiVersion: v1kind: Podmetadata: name: string namespace: string labels: - name: string annotations: - name: stringspec: containers: - name: string image: string imagePullPolicy: [Always | Never | IfNotPresent command: [string] args: [string] workingDir: string volumeMounts: string - name: string mountPath: string readOnly: boolean ports: - name: string containerPort: int hostPort: int protocol: string env: - name: string value: string resources: limits: cpu: string memory: string requests: cpu: string memory: string livenessProbe: exec: command: [string] httpGet: path: string port: number scheme: string httpHeaders: - name: string value: string tcpSocket: port: number initialDelaySeconds: 0 timeoutSeconds: 0 periodSeconds: 0 successThreshold: 0 failureThreshold: 0 securityContext: privileged: false restartPolicy: [Always | Never | OnFailure] nodeSelector: object imagePullSecrets: - name: string hostNetwork: false volumes: - name: string emptyDir: {} hostPath: path: string secret: secretName: string items: - key: string value: string configMap: name: string items: - key: string - path: string

Pod定义文件模板中各属性的说明如下:






注:k8s的Pod启动命令不能是后台执行的,不然k8s会不断创建新的Pod而陷入无限循环中。如果docker镜像的命令无法改造为前台执行,可以使用开源工具Supervisor。或是 && tail -f xx 这样的组合命令。

相关文章