构建镜像
创建hive目录,把hived的tar包、hadoop的tar包以及dockerfile都放在目录下面
mkdir hive
编写dockerfile
FROM jdk:alpine
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk add openssh vim openrc bash bash-doc bash-completion --no-cache
RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
ADD apache-hive-2.3.4-bin.tar.gz /opt
ADD hadoop-2.7.2.tar.gz /opt
ENV HADOOP_CONF_DIR /opt/hadoop-2.7.2/etc/hadoop
ENV HIVE_HOME /opt/apache-hive-2.3.4-bin
ENV HADOOP_HOME /opt/hadoop-2.7.2
ENV PATH $JAVA_HOME/bin:$PATH
基础镜像是我自己基于alpine构建的jdk镜像
构建
docker build -t hive:alpine --squash .
#给镜像打标签
docker tag hive:alpine 192.168.0.16:5000/hive:alpine
#将镜像推送到私仓
docker push 192.168.0.16:5000/hive:alpine
构建DB tool镜像
安装gradle并编译DB tool
下载gradle:https://services.gradle.org/distributions/
将下载的gradle-5.1-bin.zip 上传到hive目录
unzip gradle-5.1-bin.zip
mkdir /opt/gradle
mv gradle-4.10.2 /opt/gradle/
add in /etc/profile
export PATH=.:/opt/gradle/gradle-4.10.2/bin:$PATH
source /etc/profile
gradle --version
拉取database-tools
git clone https://github.com/chenlein/database-tools.git
cd database-tools/
edit build.gradle to remove dm driver
#把下面这一行删掉,不然编译的时候会报错
"compile group: 'dm', name: 'Dm7JdbcDriver', version: '7.1', classifier: 'jdk17-20170808'"
ls build/distributions/database-tools-1.0-SNAPSHOT.tar
cp build/distributions/database-tools-1.0-SNAPSHOT.tar ./
编写Dockerfile
FROM jdk:alpine
MAINTAINER lbz
CMD ["mkdir", "-p", "/opt/db_tools"]
WORKDIR /opt/db_tools
ADD database-tools-1.0-SNAPSHOT.tar .
RUN ["chmod", "+x", "./database-tools-1.0-SNAPSHOT/bin/database-tools"]
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
RUN apk update && apk upgrade
RUN apk add openssh vim openrc bash bash-doc bash-completion procps --no-cache
RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
CMD ["./database-tools-1.0-SNAPSHOT/bin/database-tools"]
编译镜像
docker build -t database-tools:1.0-SNAPSHOT .
docker tag database-tools:1.0-SNAPSHOT 192.168.0.16:5000/database-tools:1.0-SNAPSHOT
docker push 192.168.0.16:5000/database-tools:1.0-SNAPSHOT
部署hive
创建pv和pvc
hivepv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: hive-data-1
labels:
type: local
app: hive
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /opt/hive
persistentVolumeReclaimPolicy: Recycle
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: hive-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
部署mysql服务
mysql.yaml
apiVersion: v1
kind: Secret
metadata:
name: hive-metadata-mysql-secret
labels:
app: hive-metadata-mysql
type: Opaque
data:
mysql-root-password: RGFtZW5nQDc3Nw==
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: hive-metadata-mysql
name: hive-metadata-mysql
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: hive-metadata-mysql
template:
metadata:
labels:
app: hive-metadata-mysql
spec:
initContainers:
- name: remove-lost-found
image: busybox:1.29.2
imagePullPolicy: IfNotPresent
command: ["rm", "-rf", "/var/lib/mysql/lost+found"]
volumeMounts:
- name: data
mountPath: /var/lib/mysql
containers:
- name: mysql
image: mysql:5.7
volumeMounts:
- name: data
mountPath: /var/lib/mysql
ports:
- containerPort: 3306
protocol: TCP
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: hive-metadata-mysql-secret
key: mysql-root-password
volumes:
- name: data
emptyDir: {}
---
kind: Service
apiVersion: v1
metadata:
labels:
app: hive-metadata-mysql
name: hive-metadata-mysql-service
spec:
ports:
- name: tcp
port: 3306
targetPort: 3306
selector:
app: hive-metadata-mysql
type: NodePort
部署hive配置
hive-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: hive-custom-config-cm
labels:
app: hive
data:
bootstrap.sh: |-
#!/bin/bash
set -x
cd /opt/bootstrap
# Apply custom config file context
for cfg in ./*; do
if [[ ! "$cfg" =~ bootstrap.sh ]]; then
echo $cfg
cat $cfg
cat $cfg > $HIVE_HOME/conf/${cfg##*/}
fi
done
# Replace hive metadata password
sed -i 's/${HIVE_METADATA_PASSWORD}/'$HIVE_METADATA_PASSWORD'/g' `grep '${HIVE_METADATA_PASSWORD}' -rl $HIVE_HOME/conf`
# initSchema
echo "step 1"
if [[ ! -e $HADOOP_CONF_DIR/hive-metastore-initialization.out ]]; then
echo "step 2"
$HADOOP_HOME/bin/hadoop fs -mkdir -p /tmp
$HADOOP_HOME/bin/hadoop fs -mkdir -p /user/hive/warehouse
$HADOOP_HOME/bin/hadoop fs -chmod g+w /tmp
$HADOOP_HOME/bin/hadoop fs -chmod g+w /user/hive/warehouse
$HIVE_HOME/bin/schematool -dbType mysql -initSchema --verbose &> $HADOOP_CONF_DIR/hive-metastore-initialization.out
fi
echo "step 3"
$HIVE_HOME/bin/hiveserver2 &
$HIVE_HOME/bin/hive --service metastore &
cp $HIVE_HOME/conf/hive-env.sh.template $HIVE_HOME/conf/hive-env.sh && echo "export HADOOP_CLIENT_OPTS=\"-Xmx512m -XX:MaxPermSize=1024m \$HADOOP_CLIENT_OPTS\"" >> $HIVE_HOME/conf/hive-env.sh
# keep running
sleep infinity
hive-site.xml: |-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>hive</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>${HIVE_METADATA_PASSWORD}</value>
</property>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hive-metadata-mysql-service:3306/metastore?createDatabaseIfNotExist=true&useSSL=false</value>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<property>
<name>system:java.io.tmpdir</name>
<value>/tmp</value>
</property>
<property>
<name>system:user.name</name>
<value>hive</value>
</property>
<property>
<name>hive.server2.authentication</name>
<value>NOSASL</value>
</property>
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<property>
<name>datanucleus.fixedDatastore</name>
<value>false</value>
</property>
<property>
<name>datanucleus.autoCreateSchema</name>
<value>true</value>
</property>
<property>
<name>datanucleus.autoCreateTables</name>
<value>true</value>
</property>
<property>
<name>datanucleus.autoCreateColumns</name>
<value>true</value>
</property>
<property>
<name>datanucleus.schema.autoCreateAll</name>
<value>true</value>
<description>creates necessary schema on a startup if one doesn't exist. set this to false, after creating it once</description>
</property>
</configuration>
部署hive
hive-deploy.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: hive-metastore-database
labels:
app: hive
data:
execute.sql: |-
-- create database
CREATE DATABASE IF not EXISTS metastore DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
-- create user and grant authorization
GRANT ALL ON metastore.* TO 'hive'@'%' IDENTIFIED BY '${IDENTIFIED}';
---
apiVersion: v1
kind: Secret
metadata:
name: hive-metastore-secret
labels:
app: hive
type: Opaque
data:
database-dba-password: RGFtZW5nQDc3Nw==
database-user-password: RGFtZW5nQDc3Nw==
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hive
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: hive
template:
metadata:
labels:
app: hive
spec:
initContainers:
- name: init-dababase
image: 192.168.0.16:5000/database-tools:1.0-SNAPSHOT
env:
- name: DRIVER_NAME
value: "com.mysql.jdbc.Driver"
- name: URL
value: "jdbc:mysql://hive-metadata-mysql-service:3306/mysql?useUnicode=true&characterEncoding=utf8&useSSL=false"
- name: USERNAME
value: "root"
- name: PASSWORD
valueFrom:
secretKeyRef:
name: hive-metastore-secret
key: database-dba-password
- name: IDENTIFIED
valueFrom:
secretKeyRef:
name: hive-metastore-secret
key: database-user-password
volumeMounts:
- name: init-dababase-volume
mountPath: /root/db_tools/script
containers:
- name: hive
image: 192.168.0.16:5000/hive:alpine
command: ["bash", "-c", "cp /opt/bootstrap/bootstrap.sh /tmp/ && chmod +x /tmp/bootstrap.sh && /tmp/bootstrap.sh"]
ports:
- containerPort: 10000
- containerPort: 10002
- containerPort: 9083
env:
- name: HADOOP_CONF_DIR
value: /opt/hadoop-2.7.2/etc/hadoop
- name: HIVE_METADATA_PASSWORD
valueFrom:
secretKeyRef:
name: hive-metastore-secret
key: database-user-password
volumeMounts:
- name: hadoop-config-volume
mountPath: /etc/hadoop
- name: hive-custom-config-volume
mountPath: /opt/bootstrap
readinessProbe:
initialDelaySeconds: 20
periodSeconds: 5
tcpSocket:
port: 10000
volumes:
- name: hadoop-config-volume
persistentVolumeClaim:
claimName: hive-data
- name: hive-custom-config-volume
configMap:
name: hive-custom-config-cm
- name: init-dababase-volume
configMap:
name: hive-metastore-database
---
kind: Service
apiVersion: v1
metadata:
labels:
app: hive
name: hive-service
spec:
ports:
- port: 10000
targetPort: 10000
name: thrift
- port: 10002
targetPort: 10002
name: webui
- port: 9083
targetPort: 9083
name: metastore
selector:
app: hive
type: NodePort
依次启动上面所配置的yaml文件
kubectl create -f hivepv.yaml
kubectl create -f mysql.yaml
kubectl create -f hive-config.yaml
kubectl create -f hive-deploy.yaml
查看部署结果
kubectl get pods
NAME READY STATUS RESTARTS AGE
hadoop-datanode-1 1/1 Running 0 39h
hadoop-datanode-2 1/1 Running 0 39h
hadoop-datanode-3 1/1 Running 0 39h
hdfs-master 1/1 Running 0 39h
hive-7d49bc9cd4-jrg6q 1/1 Running 0 150m
hive-metadata-mysql-f675498d5-gjdcf 1/1 Running 0 14h
yarn-master 1/1 Running 0 39h
yarn-node-1 1/1 Running 0 39h
yarn-node-2 1/1 Running 0 39h
yarn-node-3 1/1 Running 0 39h
参考
CSDN:在Kubernetes上部署Hive
https://blog.csdn.net/chenleiking/article/details/82492282