背景信息:

版本:kubernetes 1.17

部署方式:kubeadm

在尝试创建PodPreset 对象是,遇到如下关键字报错:settings.k8s.io/v1alpha1=true

然后谷歌搜索,发现,这是1.8版本加入的新特性,默认kube-api 没开启,需要手动开启,操作步骤如下:

1.修改kube-apiserver 配置文件

cat /etc/kubernetes/manifests/kube-apiserver.yaml

在spec.containers.command 段 增加如下内容:

- --enable-admission-plugins=NodeRestriction,PodPreset

- --runtime-config=settings.k8s.io/v1alpha1=true

2.保存文件后,kube-apiserver会自动重启。

3.创建PodPreset 对象。 

PodPreset 配置文件内容如下:


apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
  name: allow-database
spec:
  selector:
    matchLabels:
      role: frontend
  env:
    - name: DB_PORT
      value: "6379"
  volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
    - name: cache-volume
      emptyDir: {}

kubectl create -f preset.yaml
4.创建pod 

pod yaml 文件内容:


apiVersion: v1
kind: Pod
metadata:
  name: website
  labels:
    app: website
    role: frontend
spec:
  containers:
    - name: website
      image: nginx
      ports:
        - containerPort: 80

 kubectl create -f pod.yaml

5.查看结果

kubectl get pod website -o yaml

apiVersion: v1
kind: Pod
metadata:
  annotations:
    podpreset.admission.kubernetes.io/podpreset-allow-database: "3403888"
  creationTimestamp: "2020-04-15T02:03:40Z"
  labels:
    app: website
    role: frontend
  name: website
  namespace: default
  resourceVersion: "3404281"
  selfLink: /api/v1/namespaces/default/pods/website
  uid: 897b4619-3038-4932-99e7-7ce488eebb92
spec:
  containers:
  - env:
    - name: DB_PORT
      value: "6379"
    image: nginx
    imagePullPolicy: Always
    name: website
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-n46r8
      readOnly: true

发现在PodPreset 中的volume 和env 字段已经在新pod中增加。

其中PodPreset 与pod 匹配使用selector 标签。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐