K8S使用Deployment&Configmap操作示例

基于对当前系统理解及配置的规划,通过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
1.
命令创建成功,可以通过如下命令查看创建的service:

kubectl get svc -n wel
1.
删除service:

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

kubectl create configmap wel-config --from-file=./wel-config/ -n wel
1.
可以通过如下命令查看wel-config的信息,并以yaml格式展示:

kubectl get cm wel-config  -o yaml -n wel
1.
如果后续配置文件中的信息发生调整,可以通过下述命令编辑configMap:

kubectl edit cm wel-config -n wel
1.
删除创建的configMap:

kubectl delete cm wel-config -n wel
1.
创建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
1.
执行命令之后无报错,可以执行命令查看deployment:

kubectl get deployment -n wel
1.
可以查看在创建deployment时,创建的ReplicaSet用于管理pod:

kubectl get rs -n wel
1.
通过deployment创建的ReplicaSet的命名为:deployment名称-随机数。

可以通过命令删除deployment:

复制 
kubectl delete deployment web-wel -n wel
-----------------------------------
©著作权归作者所有:来自51CTO博客作者WEL测试的原创作品,请联系作者获取转载授权,否则将追究法律责任
K8S使用Deployment&Configmap操作示例
https://blog.51cto.com/u_15685799/5649583

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值