k8s PodPreset
1. 在master中加入配置
如果有多个master, 每个master都需要加入
/etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-admission-plugins=NodeRestriction,PodPreset
- --service-node-port-range=1-65535
- --runtime-config=settings.k8s.io/v1alpha1=true
- 完整的配置文件
https://gitee.com/chen1219_1/k8s-install/raw/master/PodPreset/kube-apiserver.yaml
- 重启kubelet
systemctl restaet kubelet
2. 测试一个简单的podpreset
为default 名称空间内所有的pod 设置环境变量TZ
mkdir podpreset
cd podpreset
vim test.yaml
apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
name: setting-timezone
spec:
selector:
matchLabels:
env:
- name: TZ
value: Asia/Shanghai
kubectl apply -f test.yaml
kubectl get PodPreset
- 查看default名称空间内的pod是否有环境变量 TZ
3. 指定pod挂载一个pvc
3.1 搭建NFS服务
192.168.31.200
3.1.1 安装 nfs-utils
yum install nfs-utils -y
systemctl start rpcbind &&systemctl enable rpcbind
systemctl start nfs && systemctl enable nfs
3.1.2 建立nfs存储目录
echo "/data/kubernetes/ *(rw,no_root_squash,no_all_squash,sync)" >/etc/exports
exportfs -r
Copyecho "/data/kubernetes/ *(rw,no_root_squash,no_all_squash,sync)" >/etc/exports
exportfs -r
3.1.3 查看nfs挂载
exportfs -v
/data/kubernetes
<world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
3.1.4 使得配置生效
showmount -e
Export list for hdss200.host.com:
/data/kubernetes *
如果出现报错 clnt_create: RPC: Program not registered
systemctl stop rpcbind
systemctl stop nfs
systemctl start rpcbind
systemctl start nfs
3.2. 在master上创建PV
192.168.31.37
3.2.1 创建rbac
- rbac.yaml
https://gitee.com/chen1219_1/k8s-install/raw/master/nfs-client-provisioner/rbac.yaml
kubectl apply -f rbac.yaml
3.2.2 创建sc
- storageclass.yaml
CopyapiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: 200-nfs-storage
provisioner: 200-nfs-provisioner #这里要和第三个nfs-client-provisioner的env环境变量中的value值对应。
reclaimPolicy: Retain
3.3 创建PVC,绑定PV
3.3.1 创建nfs-client-provisioner容器
- deployment.yaml
CopyapiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
namespace: kube-system
spec:
replicas: 1 #副本数量为1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccount: nfs-client-provisioner #指定账户
containers:
- name: nfs-client-provisioner
image: harbor.od.com/public/nfs-client-provisioner:latest #使用的是这个镜像
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes #指定容器内的挂载目录
env:
- name: PROVISIONER_NAME #这是这个容器内置的变量
value: 200-nfs-provisioner #这是上面变量的值(名字)
- name: NFS_SERVER #内置变量,用于指定nfs服务的IP
value: 192.168.31.200
- name: NFS_PATH #内置变量,指定的是nfs共享的目录
value: /data/kubernetes
volumes: #这下面是指定上面挂载到容器内的nfs的路径及IP
- name: nfs-client-root
nfs:
server: 192.168.31.200
path: /data/kubernetes
kubectl apply -f deployment.yaml
3.3.2 创建pvc
- test2-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test2-nginx-pvc
namespace: ingress-nginx
spec:
storageClassName: 200-nfs-storage #定义存储类的名字,要和SC的名字对应
accessModes:
- ReadWriteMany #访问模式为RWM
resources:
requests:
storage: 500Mi
3.4 创建podPreset
在 ingress-nginx 名称空间中的pod带 role: java-inject 标签的 挂载/mymountpath目录, /mymountpath 挂载pvc test2-nginx-pvc
3.4.1 创建PodPreset 资源
- addmount.yaml
apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
name: mountpath
namespace: ingress-nginx
spec:
selector:
matchLabels:
role: java-inject
volumeMounts:
- mountPath: /mymountpath
name: mymount
volumes:
- name: mymount
persistentVolumeClaim:
claimName: test2-nginx-pvc
3.4.2 部署PodPreset资源
kubectl apply -f addmount.yaml
3.4.3 部署PodPreset资源
kubectl get PodPreset -n ingress-nginx
3.4.4 在pvc中创建文件
192.168.31.200
cd /data/kubernetes/ingress-nginx-test2-nginx-pvc-pvc-86f70012-07ed-4386-b99f-c76dac5c5ff4/
echo '小熊奶茶真好喝' > index.html
3.5 创建nginx
5.1 准备nginx部署文件
- nginx-configmap.yaml
https://gitee.com/chen1219_1/k8s-install/raw/master/PodPreset/nginx-configmap.yaml
- nginx-deployment.yaml
https://gitee.com/chen1219_1/k8s-install/raw/master/PodPreset/nginx-deployment.yaml
4.2 部署nginx
kubectl apply -f nginx-configmap.yaml
kubectl apply -f nginx-deployment.yaml