0
点赞
收藏
分享

微信扫一扫

K8S使用Deployment&Configmap操作示例


ps:有问题可以加微信:Lover_718,添加务必加上备注!
​无意中发现了一个很好的软件测试网站,忍不住分享一下给大家。觉得很实用,所以分享给大家。点这里可以跳转到教程。

基于对当前系统理解及配置的规划,通过K8S部署应用,需要三大利器。configMap用于统一管理配置信息,service用于对外提供服务,deployment用于部署pod及系统升级更新。

创建 Service

这里给出一个创建服务示例代码[web-test-service.yaml]:

apiVersion: v1
kind: Service
metadata:
name: web-test
namespace: wel
spec:
ports:
- name: test-8000
port: 8000
targetPort: 8000
- name: test-8005
port: 8005
targetPort: 8005
- name: test-8009
port: 8009
targetPort: 8009
selector:
app: web-wel
role:

针对各种属性字段这里不做过多解释,这里创建一个wel-test的service的资源,服务暴露的端口有:8000/8005/8009。通过selector选择使用该service的pod,这里选择标签为“app: web-wel、role: front”的pod使用该服务。

通过命令创建服务:

kubectl create -f web-test-service.yaml

命令创建成功,可以通过如下命令查看创建的service:

kubectl get svc -n wel

删除service:

kubectl delete svc web-test -n wel

创建configMap:

由于有好多配置相关的文件,可以把服务配置相关的文件都放在目录wel-config中,通过如下命令在namespace为wel下创建一个名为wel-config的configMap:

kubectl create configmap wel-config --from-file=./wel-config/ -n wel

可以通过如下命令查看wel-config的信息,并以yaml格式展示:

kubectl get cm wel-config  -o yaml -n wel

如果后续配置文件中的信息发生调整,可以通过下述命令编辑configMap:

kubectl edit cm wel-config -n wel

删除创建的configMap:

kubectl delete cm wel-config -n wel

创建Deployment:

给出创建deployment的代码示例文件【web-wel-deployment.yaml】:

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-wel
namespace: wel
spec:
replicas: 2
selector:
matchLabels:
app: web-wel
role: front
template:
metadata:
labels:
app: web-wel
role: front
spec:
containers:
- name: web-wel
image: wel/web:1.0.3
ports:
- containerPort: 8000
- containerPort: 8005
- containerPort: 8009
command: ['/bin/sh']
args: ["-c","su - wel -c "sh /home/bin/tomcat.sh start "; while true; do tailf /home/wel/logs/wel.log;sleep 10;done"]
volumeMounts:
- name: config
mountPath: /home/wel/config
- name: hosts
mountPath: /etc/hosts
subPath: hosts
volumes:
- name: config
configMap:
name: wel-config
items:
- key: config.properties
path: config.properties
- key: wel.conf
path:wels.conf
- name: hosts
configMap:
name: wel-config
items:
- key: hosts
path:

上述创建一个名为web-wel的Deployment资源。在模板中的数据卷模块引用Configmap中的内容给出解释:

volumes: --数据卷定义
- name: config --数据的名称,该名称要与volumeMounts中的name值保持一致
configMap: --数据来源与configMap
name: wel-config --configMap的名称与上述创建的configMap中的name保持一致
items: --元素
- key: config.properties --对应configMap中data下面的key值
path: config.properties --把key对应的值放入到path对应的文件中,进入pod中查看其实是链接

如何在容器中挂载数据,在下面给出解释:

volumeMounts: --数据卷挂载申明
- name: config --数据卷名称
mountPath: /home/wel/config --容器中的配置目录
- name: hosts --数据卷名称
mountPath: /etc/hosts --容器中的目录
subPath: hosts --子目录

  • 使用mountPath会使用数据卷中的数据覆盖容器中已有的文件,但好处是调整configMap中的数据时,pod会热更新。编辑过configMap之后,可进入pod查看配置数据已经更新。
  • 使用subPath不会覆盖目录中的数据,只会覆盖subpath中指定的文件。但不好之处在于,编辑配置文件中的hosts值,pod中对应配置值不会更新,只有重启pod之后或者重新部署pod之后,修改的配置才会生效

创建deployment的命令如下:

kubectl create -f web-wel-deployment.yaml

执行命令之后无报错,可以执行命令查看deployment:

kubectl get deployment -n wel

可以查看在创建deployment时,创建的ReplicaSet用于管理pod:

kubectl get rs -n wel

通过deployment创建的ReplicaSet的命名为:deployment名称-随机数。

可以通过命令删除deployment:

kubectl delete deployment web-wel -n wel


举报

相关推荐

K8s ConfigMap

K8S Deployment

k8s概念-ConfigMap

K8S 之 configmap

Kubernetes(K8s) Deployment

0 条评论