Kubernetes挂载常用资源
介绍下用k8s挂载一些常用的资源
当前版本Kubernetes版本:1.12.2
env
env
env: - name: GIT_REPO value: 'ssh://git@127.0.0.1:22/a/b.git'
嵌套env
env: - name: spring.profiles.active value: 'product' - name: MY_POD_IP valueFrom: fieldRef: fieldPath: status.podIP - name: GOMS_API_HTTP_ADDR value: 'http://$(MY_POD_IP):9090'
configMap
注意一下,修改configmap不会导致容器里的挂载的configmap文件/环境变量发生改变;删除configmap也不会影响到容器内部的环境变量/文件,但是删除configmap之后,被挂载的pod上面会出现一个warnning的事件
Events: Type Reason Age From Message ---- ------ ---- ---- ------- Warning FailedMount 64s (x13 over 11m) kubelet, cn-shenzhen.i-wz9498k1n1l7sx8bkc50 MountVolume.SetUp failed for volume "nginx" : configmaps "nginx" not found
config map写的很清楚了,这里恬不知耻得copy一下
注意,configmap有1M的限制,一般用来挂载小型配置,大量配置建议上配置中心
挂载单一项
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "env" ] env: # Define the environment variable - name: SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY name: special-config # Specify the key associated with the value key: special.how restartPolicy: Never
表示挂载special-config
这个configmap的special.how
项
挂载整个configmap
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "env" ] envFrom: - configMapRef: name: special-config restartPolicy: Never
参考:
fieldRef
可以挂载pod的一些属性
env: - name: MY_POD_IP valueFrom: fieldRef: fieldPath: status.podIP
Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.
resourceFieldRef
Selects a resource of the container: only resources limits and requests (limits.cpu, limits.memory, limits.ephemeral-storage, requests.cpu, requests.memory and requests.ephemeral-storage) are currently supported.
英文介绍得很明白,用来挂载当前yaml里面container的资源(CPU/内存)限制,用得比较少啦其实.此外还可以结合downloadAPI
注意containerName
不能配错,不然pod状态会变成CreateContainerConfigError
env: - name: a valueFrom: resourceFieldRef: containerName: nginx-test2 resource: limits.cpu
secretKeyRef
Selects a key of a secret in the pod's namespace
env: - name: WORDPRESS_DB_USER valueFrom: secretKeyRef: name: mysecret key: username - name: WORDPRESS_DB_PASSWORD valueFrom: secretKeyRef: name: mysecret key: password
参考:
- Kubernetes中Secret使用详解
- https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.12/#envvarsource-v1-core
目录/文件类挂载
k8s可以挂载的资源实在是太多,这里挑一些比较有代表性的来讲一下
这一类资源一般要先在spec层级定义volumes
,然后在containers
定义volumeMounts
,有种先声明,再使用的意思
hostPath(宿主机目录/文件)
- 既有目录/文件用
Directory
/File
+nodeSelector
但是用了nodeSelector
之后,以后的伸缩都会在匹配的节点上,如果节点只有1个,副本集设置得超出实际节点可承受空间,最终将导致单点问题,这个要注意下 - 应用启用时读写空文件用
DirectoryOrCreate
或者FileOrCreate
以下演示第一种方案
#给节点打上标签(这里省略) kubectl get node --show-labels
apiVersion: apps/v1beta2 kind: Deployment metadata: labels: app: nginx-test2 name: nginx-test2 namespace: test spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 2 selector: matchLabels: app: nginx-test2 strategy: rollingUpdate: maxSurge: 1 maxUnavailable: 1 type: RollingUpdate template: metadata: labels: app: nginx-test2 spec: containers: - image: 'nginx:1.15.4-alpine' imagePullPolicy: Always name: nginx-test2 resources: {} terminationMessagePolicy: File volumeMounts: - name: host1 mountPath: /etc/nginx/sites-enabled - name: host2 mountPath: /etc/nginx/sites-enabled2/a.com.conf nodeSelector: kubernetes.io/hostname: cn-shenzhen.i-wz9aabuytimkomdmjabq dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: {} terminationGracePeriodSeconds: 30 volumes: - name: host1 hostPath: path: /root/site type: Directory - name: host2 hostPath: path: /root/site/a.com.conf type: File
configMap
单项挂载(第1种)
这种挂载会热更新,更改后大约10秒后能看到变化
volumeMounts: - name: config-vol mountPath: /etc/config volumes: - name: config-vol configMap: name: log-config items: - key: log_level path: log_level
单项挂载(第2种)
这种挂载方式不会热更新
volumeMounts: - name: nginx mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: nginx configMap: name: amiba-nginx
完全挂载
这种挂载会热更新,更改后大约10秒后能看到变化
volumeMounts: - name: config-vol mountPath: /etc/config volumes: - name: config-vol configMap: name: log-config
secret
单项挂载
volumes: - name: secrets secret: secretName: mysecret items: - key: password mode: 511 path: tst/psd - key: username mode: 511 path: tst/usr
完全挂载
这里用了特定权限去挂载文件,默认好像是777
volumeMounts: - name: sshkey mountPath: /root/.ssh volumes: - name: sshkey secret: secretName: pull-gitea defaultMode: 0400
kubectl create secret generic pull-gitea \ --from-file=id_rsa=/Volumes/D/temp/id_rsa \ --from-file=id_rsa.pub=/Volumes/D/temp/id_rsa.pub \ --from-file=known_hosts=/Volumes/D/temp/known_hosts \
比如这个模式创建出来的secret,容器里面/root/.ssh目录就会有id_rsa
,id_rsa.pub
,known_hosts
3个文件
downwardAPI
参考链接:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
你的第一次轻量级K8S体验 —— 记一次Rancher 2.2 + K3S集成部署过程
i> 此文章为 Rancher+K3S 部署指南 系列中的一篇文章,更多教程请点击 >> 在生产环境中,很多时候我们都会有这样的需求: 业务弹性需求较大,反复安装卸载修改配置文件会对环境造成极大破坏,甚至需要重装系统以清理环境 将业务迁移到Docker架构,但使用传统的Docker CLI (Docker命令行) 方式进行管理,效率低下还容易误操作 将业务迁移到K8S架构,但因为K8S过于占用系统资源,往往在开发环境或者小型的生产环境,K8S刚跑起来系统就没可用性能资源了 K8S手动部署起来一路尽是无底深坑,好不容易配好了稍稍改动下配置文件整个集群全炸了 需要个简单直观管理工具,能够简单快速的管理各个K8S集群,灵活创建Pod 在我接触K8S架构之初,也遇到了类似的问题:使用Rancher2+K8S这样的架构,在一台1C2G的阿里云上面部署,部署完成后发现系统内存早就被吃的一干二净,无从谈起业务部署;使用传统的Docker Swarm,感觉用起来没有K8S那样顺手舒服。 2019年2月26日,业界领先的容器管理软件提供商Rancher Labs(以下简称Rancher...
- 下一篇
K8s学习进阶月刊第三期 Kubernetes and Cloud Native Meetup 上海站
欢迎订阅K8s学习进阶月刊 视频干货资料尽情下载 Kubernetes and Cloud Native Meetup 上海站 资料下载GitOps:Kubernetes多集群环境下的高效CICD实践 资料下载阿里云容器 AHAS Sentinel 网关流控揭秘 资料下载 推荐阅读 像Google一样构建机器学习系统2 - 开发你的机器学习工作流阿里云 PB 级 Kubernetes 日志平台建设实践容器安全拾遗 - Rootless Container初探Serverless助力AI计算:阿里云ACK Serverless/ECI发布GPU容器实例阿里云Kubernetes服务上使用Tekton完成应用发布初体验Knative Tracing 介绍阿里云Kubernetes服务上从零搭建GitLab+Jenkins+GitOps应
相关文章
文章评论
共有0条评论来说两句吧...