Pod设计

Table of Contents

标签

    用键值对来指定某些”有意义的并和用户有关的“对象属性
apiVersion: v1
kind: Pod
metadata:
  name: my-production-label-pod
  labels:
    app: my-app
    environment: production
spec:
  containers:
  - name: nginx
    image: nginx

标签选择器

使用 -l 来过滤某些pod:

kubectl get pods -l app=my-app

kubectl get pods -l environment=production

kubectl get pods -l environment=development

”取反“选择:

kubectl get pods -l environment!=production

”集合“选择:

kubectl get pods -l 'environment in (development,production)'

”交集“选择:

kubectl get pods -l app=my-app,environment=production

注解

  注解和标签类似,区别是无法被用于选择器,只有客户端(如工具和库)才能检索
apiVersion: v1
kind: Pod
metadata:
  name: my-annotation-pod
  annotations:
    owner: terry@linuxacademy.com
    git-commit: bdab0c6
spec:
  containers:
  - name: nginx
    image: nginx

部署

  部署:用来管理同一个pod的多个副本

一个pod三个副本

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

查询部署信息:

kubectl get deployments

kubectl get deployment <deployment name>

kubectl describe deployment <deployment name>

直接编辑:

kubectl edit deployment <deployment name>

删除:

kubectl delete deployment <deployment name>

滚动升级和回滚

滚动升级:

kubectl set image deployment/rolling-deployment nginx=nginx:1.7.9 --record
--record : 记录在更新历史中

查询更新历史:

kubectl rollout history deployment/rolling-deployment

kubectl rollout history deployment/rolling-deployment --revision=2

回滚到某个版本:

kubectl rollout undo deployment/rolling-deployment --to-revision=1

以指定 maxUnavailablemaxSurge 来控制 rolling update 进程:

  • .spec.strategy.rollingUpdate.maxUnavailable: 可选配置项,用来指定在升级过程中不可用Pod的最大数量
  • .spec.strategy.rollingUpdate.maxSurge: 可选配置项,用来指定可以超过期望的Pod数量的最大个数

任务

  任务:执行完毕就自动停止的容器
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
  backoffLimit: 4

查询任务执行状态:

kubectl get jobs 

定时任务

每分钟执行一次的定时任务:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

查询定时任务的执行状态:

kubectl get cronjobs