hmdm示例应用k8s部署

如果环境已全部搭好,请忽略前面3步!

部署jenkins

参考Jenkins部署

部署nginx文件服务器

参考nginx-file-server部署

部署gitlab

参考Gitlab部署

hmdm部署

  1. 首先,新建项目,选择自由风格的软件项目,输入名字: image

  2. 源码管理。这里使用git,Repository URL为仓库地址,Credentials为克隆代码的用户,Branch Specifier选择要构建的分支。 image

  3. 构建触发器,当勾选当前选项时,每当gitlab上有代码提交,就会出发构建。 image

  4. 点击构建,选择Excute shell image

# 修改redis地址
perl -p -i -e "s/redis.ip=localhost/redis.ip=redis/g" core/src/main/resources/config.properties
# 复制到dev
cp core/src/main/resources/config.properties core/src/main/resources/profiles/dev/config.properties
# 清除安装依赖和选择环境dev
mvn clean install -Pdev
# 初始化数据库,数据库地址为mysql服务的集群ip地址
mvn process-resources -D skipLiquibaseRun=false -D db.driver=com.mysql.jdbc.Driver -D db.url=jdbc:mysql://10.97.248.250:3306/hap_dev -Ddb.user=hap_dev -Ddb.password=hap_dev
# 将生存的war包,复制到文件服务器目录下
mkdir -p /var/jenkins_home/publicfiles/hmdmt1&&cp core/target/core.war /var/jenkins_home/publicfiles/hmdmt1/core.war
  1. 创建上一步中的,数据库和用户
# 修改mysql配置(见mysql部署)
lower_case_table_names=1
character_set_server=utf8
max_connections=500
# 用管理员登陆mysql,创建数据库hap_dev与hap_prod
create schema hap_dev default character set utf8;
create schema hap_prod default character set utf8; 
# 创建用户hap_dev,并将密码设置为hap_dev
CREATE USER hap_dev@'%' IDENTIFIED BY 'hap_dev';
CREATE USER hap_dev@'localhost' IDENTIFIED BY 'hap_dev';
# 将上面新建的hap_dev和hap_prod的权限全部赋予用户hap
GRANT ALL PRIVILEGES ON hap_dev.* TO hap_dev@'%';
GRANT ALL PRIVILEGES ON hap_dev.* TO hap_dev@'localhost';
GRANT ALL PRIVILEGES ON hap_prod.* TO hap_dev@'%';
GRANT ALL PRIVILEGES ON hap_prod.* TO hap_dev@'localhost';

flush privileges;
  1. 点击保存

  2. 再新建一个项目hmdmt1Dockerbuild

  3. 编写Dockerfilehmdmt1.yaml文件并提交到gitlab

FROM tomcat:8.5.2-jre8

MAINTAINER jianwu.bin@hand-china.com

ADD context.xml /usr/local/tomcat/conf/

ADD http://192.168.56.11:31187/hmdmt1/core.war /usr/local/tomcat/webapps/

context.xml配置(jndi)如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
    <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" name="jdbc/hap_dev" password="hap_dev" type="javax.sql.DataSource" url="jdbc:mysql://10.97.248.250:3306/hap_dev?characterEncoding=UTF-8" username="hap_dev"/>
</Context>

hmdmt1.yaml:

kind: Namespace
apiVersion: v1
metadata:
    name: hmdmt1

---

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: redis
  namespace: hmdmt1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: registry.saas.hand-china.com/tools/redis:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
          name: redis
        volumeMounts:
        - name: redis-home
          mountPath: /var/lib/redis:Z
      volumes:
         - name: redis-home
           hostPath:
            path: /srv/docker/hmdm/redis

---

kind: Service
apiVersion: v1
metadata:
  labels:
    app: redis
  name: redis
  namespace: hmdmt1
spec:
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
    protocol: TCP
  selector:
    app: redis

---

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: hmdmservice
  namespace: hmdmt1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: hmdmservice
    spec:
      containers:
      - name: hmdmservice
        image: registry.saas.hand-china.com/rdcjee/hmdmt1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: hmdmservice
       
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: hmdmservice
  name: hmdmservice
  namespace: hmdmt1
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP
  selector:
    app: hmdmservice

  1. 源码管理,选择git,并指向刚刚提交的项目 image

  2. 构建出发器,如图选择 image

  3. 构建,选择执行shell image

  4. 保存,返回点击hmdmt1,点击立即构建

如果无法kubectl create,请将master节点的/etc/kubernetes/admin.conf文件复制到jenkins节点的jenkins用户主目录.kube下,并命名为config

  1. 浏览
# 查询端口
kubectl get svc -n hmdmt1