Kubernetes深入Pod

原创
半兽人 发表于: 2018-10-09   最后更新时间: 2021-09-28 10:19:13  
{{totalSubscript}} 订阅, 7,041 游览

本节将对kubernetes如何发布和管理应用进行说明和示例,主要包括Pod和容器的使用、Pod的控制和调度、应用配置管理等内容。

1.Pod定义详解

yaml格式的Pod定义文件的完整内容:

apiVersion: v1
kind: Pod
metadata:
  name: string
  namespace: string
  labels:
    - name: string
  annotations:
    - name: string
spec:
  containers:
  - name: string
    image: string
    imagePullPolicy: [Always | Never | IfNotPresent]
    command: [string]
    args: [string]
    workingDir: string
    volumeMounts:
    - name: string
      mountPath: string
      readOnly: boolean
    ports:
    - name: string
      containerPort: int
      hostPort: ing
      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
        host: string
        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
        path: string
      configMap:
        name: string
        items:
        - key: string
          path: string

screenshot

screenshot

screenshot

screenshot

screenshot

screenshot

2. Pod的基本用法

Pod可用由1个或多个容器组合而成,例如名为frontendPod只由一个容器组成:

apiVersion: v1
kind: Pod
metadata:
  name: frontend
  labels:
    name: frontend
spec:
  containers:
  - name: frontend
    image: kubeguide/guestbook-php-frontend
    env:
    - name: GET_HOSTS_FROM
      value: env
    ports:
    - containerPort: 80

这个Pod启动后,将启动1个Docker容器另一种场景是,当两个容器为紧耦合关系,应将两个容器打包为一个Pod,如下:

screenshot

配置文件frontend-localredis-pod.yaml如下:

apiVersion: v1
kind: Pod
metadata:
  name: redis-php
  labels:
    name: redis-php
spec:
  containers:
    - name: frontend
      image: nginx
      ports:
        - containerPort: 80
    - name: redis
      image: 'redis:6.2.5'
      ports:
        - containerPort: 6379

运行命令创建Pod:

kubectl create -f frontend-localredis-pod.yaml
# kubectl get pods
NAME |Ready |status |Restarts |Age
redis-php |2/2 |Running |0 |10m

可以看到Ready信息为2/2,表示Pod中有两个容器在运行

查看这个Pod的详细信息,可以看到两个容器的定义及创建过程(Event事件信息)

kubectl describe pod redis-php
更新于 2021-09-28

查看kubernetes更多相关的文章或提一个关于kubernetes的问题,也可以与我们一起分享文章