构建镜像

创建hive目录,把hived的tar包、hadoop的tar包以及dockerfile都放在目录下面

  1. mkdir hive

编写dockerfile

  1. FROM jdk:alpine
  2. RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
  3. RUN apk update && apk upgrade
  4. RUN apk add openssh vim openrc bash bash-doc bash-completion --no-cache
  5. RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
  6. ADD apache-hive-2.3.4-bin.tar.gz /opt
  7. ADD hadoop-2.7.2.tar.gz /opt
  8. ENV HADOOP_CONF_DIR /opt/hadoop-2.7.2/etc/hadoop
  9. ENV HIVE_HOME /opt/apache-hive-2.3.4-bin
  10. ENV HADOOP_HOME /opt/hadoop-2.7.2
  11. ENV PATH $JAVA_HOME/bin:$PATH

基础镜像是我自己基于alpine构建的jdk镜像

构建

  1. docker build -t hive:alpine --squash .
  2. #给镜像打标签
  3. docker tag hive:alpine 192.168.0.16:5000/hive:alpine
  4. #将镜像推送到私仓
  5. 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目录

  1. unzip gradle-5.1-bin.zip
  2. mkdir /opt/gradle
  3. mv gradle-4.10.2 /opt/gradle/
  4. add in /etc/profile
  5. export PATH=.:/opt/gradle/gradle-4.10.2/bin:$PATH
  6. source /etc/profile
  7. gradle --version

拉取database-tools

  1. git clone https://github.com/chenlein/database-tools.git
  2. cd database-tools/
  3. edit build.gradle to remove dm driver
  4. #把下面这一行删掉,不然编译的时候会报错
  5. "compile group: 'dm', name: 'Dm7JdbcDriver', version: '7.1', classifier: 'jdk17-20170808'"
  6. ls build/distributions/database-tools-1.0-SNAPSHOT.tar
  7. cp build/distributions/database-tools-1.0-SNAPSHOT.tar ./

编写Dockerfile

  1. FROM jdk:alpine
  2. MAINTAINER lbz
  3. CMD ["mkdir", "-p", "/opt/db_tools"]
  4. WORKDIR /opt/db_tools
  5. ADD database-tools-1.0-SNAPSHOT.tar .
  6. RUN ["chmod", "+x", "./database-tools-1.0-SNAPSHOT/bin/database-tools"]
  7. RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories
  8. RUN apk update && apk upgrade
  9. RUN apk add openssh vim openrc bash bash-doc bash-completion procps --no-cache
  10. RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
  11. CMD ["./database-tools-1.0-SNAPSHOT/bin/database-tools"]

编译镜像

  1. docker build -t database-tools:1.0-SNAPSHOT .
  2. docker tag database-tools:1.0-SNAPSHOT 192.168.0.16:5000/database-tools:1.0-SNAPSHOT
  3. docker push 192.168.0.16:5000/database-tools:1.0-SNAPSHOT

部署hive

创建pv和pvc

hivepv.yaml

  1. apiVersion: v1
  2. kind: PersistentVolume
  3. metadata:
  4. name: hive-data-1
  5. labels:
  6. type: local
  7. app: hive
  8. spec:
  9. capacity:
  10. storage: 20Gi
  11. accessModes:
  12. - ReadWriteOnce
  13. hostPath:
  14. path: /opt/hive
  15. persistentVolumeReclaimPolicy: Recycle
  16. ---
  17. kind: PersistentVolumeClaim
  18. apiVersion: v1
  19. metadata:
  20. name: hive-data
  21. spec:
  22. accessModes:
  23. - ReadWriteOnce
  24. resources:
  25. requests:
  26. storage: 20Gi

部署mysql服务

mysql.yaml

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: hive-metadata-mysql-secret
  5. labels:
  6. app: hive-metadata-mysql
  7. type: Opaque
  8. data:
  9. mysql-root-password: RGFtZW5nQDc3Nw==
  10. ---
  11. apiVersion: apps/v1
  12. kind: Deployment
  13. metadata:
  14. labels:
  15. app: hive-metadata-mysql
  16. name: hive-metadata-mysql
  17. spec:
  18. replicas: 1
  19. revisionHistoryLimit: 10
  20. selector:
  21. matchLabels:
  22. app: hive-metadata-mysql
  23. template:
  24. metadata:
  25. labels:
  26. app: hive-metadata-mysql
  27. spec:
  28. initContainers:
  29. - name: remove-lost-found
  30. image: busybox:1.29.2
  31. imagePullPolicy: IfNotPresent
  32. command: ["rm", "-rf", "/var/lib/mysql/lost+found"]
  33. volumeMounts:
  34. - name: data
  35. mountPath: /var/lib/mysql
  36. containers:
  37. - name: mysql
  38. image: mysql:5.7
  39. volumeMounts:
  40. - name: data
  41. mountPath: /var/lib/mysql
  42. ports:
  43. - containerPort: 3306
  44. protocol: TCP
  45. env:
  46. - name: MYSQL_ROOT_PASSWORD
  47. valueFrom:
  48. secretKeyRef:
  49. name: hive-metadata-mysql-secret
  50. key: mysql-root-password
  51. volumes:
  52. - name: data
  53. emptyDir: {}
  54. ---
  55. kind: Service
  56. apiVersion: v1
  57. metadata:
  58. labels:
  59. app: hive-metadata-mysql
  60. name: hive-metadata-mysql-service
  61. spec:
  62. ports:
  63. - name: tcp
  64. port: 3306
  65. targetPort: 3306
  66. selector:
  67. app: hive-metadata-mysql
  68. type: NodePort

部署hive配置

hive-config.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: hive-custom-config-cm
  5. labels:
  6. app: hive
  7. data:
  8. bootstrap.sh: |-
  9. #!/bin/bash
  10. set -x
  11. cd /opt/bootstrap
  12. # Apply custom config file context
  13. for cfg in ./*; do
  14. if [[ ! "$cfg" =~ bootstrap.sh ]]; then
  15. echo $cfg
  16. cat $cfg
  17. cat $cfg > $HIVE_HOME/conf/${cfg##*/}
  18. fi
  19. done
  20. # Replace hive metadata password
  21. sed -i 's/${HIVE_METADATA_PASSWORD}/'$HIVE_METADATA_PASSWORD'/g' `grep '${HIVE_METADATA_PASSWORD}' -rl $HIVE_HOME/conf`
  22. # initSchema
  23. echo "step 1"
  24. if [[ ! -e $HADOOP_CONF_DIR/hive-metastore-initialization.out ]]; then
  25. echo "step 2"
  26. $HADOOP_HOME/bin/hadoop fs -mkdir -p /tmp
  27. $HADOOP_HOME/bin/hadoop fs -mkdir -p /user/hive/warehouse
  28. $HADOOP_HOME/bin/hadoop fs -chmod g+w /tmp
  29. $HADOOP_HOME/bin/hadoop fs -chmod g+w /user/hive/warehouse
  30. $HIVE_HOME/bin/schematool -dbType mysql -initSchema --verbose &> $HADOOP_CONF_DIR/hive-metastore-initialization.out
  31. fi
  32. echo "step 3"
  33. $HIVE_HOME/bin/hiveserver2 &
  34. $HIVE_HOME/bin/hive --service metastore &
  35. 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
  36. # keep running
  37. sleep infinity
  38. hive-site.xml: |-
  39. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  40. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  41. <configuration>
  42. <property>
  43. <name>javax.jdo.option.ConnectionUserName</name>
  44. <value>hive</value>
  45. </property>
  46. <property>
  47. <name>javax.jdo.option.ConnectionPassword</name>
  48. <value>${HIVE_METADATA_PASSWORD}</value>
  49. </property>
  50. <property>
  51. <name>javax.jdo.option.ConnectionURL</name>
  52. <value>jdbc:mysql://hive-metadata-mysql-service:3306/metastore?createDatabaseIfNotExist=true&amp;useSSL=false</value>
  53. </property>
  54. <property>
  55. <name>javax.jdo.option.ConnectionDriverName</name>
  56. <value>com.mysql.jdbc.Driver</value>
  57. </property>
  58. <property>
  59. <name>system:java.io.tmpdir</name>
  60. <value>/tmp</value>
  61. </property>
  62. <property>
  63. <name>system:user.name</name>
  64. <value>hive</value>
  65. </property>
  66. <property>
  67. <name>hive.server2.authentication</name>
  68. <value>NOSASL</value>
  69. </property>
  70. <property>
  71. <name>hive.metastore.schema.verification</name>
  72. <value>false</value>
  73. </property>
  74. <property>
  75. <name>datanucleus.fixedDatastore</name>
  76. <value>false</value>
  77. </property>
  78. <property>
  79. <name>datanucleus.autoCreateSchema</name>
  80. <value>true</value>
  81. </property>
  82. <property>
  83. <name>datanucleus.autoCreateTables</name>
  84. <value>true</value>
  85. </property>
  86. <property>
  87. <name>datanucleus.autoCreateColumns</name>
  88. <value>true</value>
  89. </property>
  90. <property>
  91. <name>datanucleus.schema.autoCreateAll</name>
  92. <value>true</value>
  93. <description>creates necessary schema on a startup if one doesn't exist. set this to false, after creating it once</description>
  94. </property>
  95. </configuration>

部署hive

hive-deploy.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: hive-metastore-database
  5. labels:
  6. app: hive
  7. data:
  8. execute.sql: |-
  9. -- create database
  10. CREATE DATABASE IF not EXISTS metastore DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
  11. -- create user and grant authorization
  12. GRANT ALL ON metastore.* TO 'hive'@'%' IDENTIFIED BY '${IDENTIFIED}';
  13. ---
  14. apiVersion: v1
  15. kind: Secret
  16. metadata:
  17. name: hive-metastore-secret
  18. labels:
  19. app: hive
  20. type: Opaque
  21. data:
  22. database-dba-password: RGFtZW5nQDc3Nw==
  23. database-user-password: RGFtZW5nQDc3Nw==
  24. ---
  25. apiVersion: apps/v1
  26. kind: Deployment
  27. metadata:
  28. name: hive
  29. spec:
  30. replicas: 1
  31. revisionHistoryLimit: 10
  32. selector:
  33. matchLabels:
  34. app: hive
  35. template:
  36. metadata:
  37. labels:
  38. app: hive
  39. spec:
  40. initContainers:
  41. - name: init-dababase
  42. image: 192.168.0.16:5000/database-tools:1.0-SNAPSHOT
  43. env:
  44. - name: DRIVER_NAME
  45. value: "com.mysql.jdbc.Driver"
  46. - name: URL
  47. value: "jdbc:mysql://hive-metadata-mysql-service:3306/mysql?useUnicode=true&characterEncoding=utf8&useSSL=false"
  48. - name: USERNAME
  49. value: "root"
  50. - name: PASSWORD
  51. valueFrom:
  52. secretKeyRef:
  53. name: hive-metastore-secret
  54. key: database-dba-password
  55. - name: IDENTIFIED
  56. valueFrom:
  57. secretKeyRef:
  58. name: hive-metastore-secret
  59. key: database-user-password
  60. volumeMounts:
  61. - name: init-dababase-volume
  62. mountPath: /root/db_tools/script
  63. containers:
  64. - name: hive
  65. image: 192.168.0.16:5000/hive:alpine
  66. command: ["bash", "-c", "cp /opt/bootstrap/bootstrap.sh /tmp/ && chmod +x /tmp/bootstrap.sh && /tmp/bootstrap.sh"]
  67. ports:
  68. - containerPort: 10000
  69. - containerPort: 10002
  70. - containerPort: 9083
  71. env:
  72. - name: HADOOP_CONF_DIR
  73. value: /opt/hadoop-2.7.2/etc/hadoop
  74. - name: HIVE_METADATA_PASSWORD
  75. valueFrom:
  76. secretKeyRef:
  77. name: hive-metastore-secret
  78. key: database-user-password
  79. volumeMounts:
  80. - name: hadoop-config-volume
  81. mountPath: /etc/hadoop
  82. - name: hive-custom-config-volume
  83. mountPath: /opt/bootstrap
  84. readinessProbe:
  85. initialDelaySeconds: 20
  86. periodSeconds: 5
  87. tcpSocket:
  88. port: 10000
  89. volumes:
  90. - name: hadoop-config-volume
  91. persistentVolumeClaim:
  92. claimName: hive-data
  93. - name: hive-custom-config-volume
  94. configMap:
  95. name: hive-custom-config-cm
  96. - name: init-dababase-volume
  97. configMap:
  98. name: hive-metastore-database
  99. ---
  100. kind: Service
  101. apiVersion: v1
  102. metadata:
  103. labels:
  104. app: hive
  105. name: hive-service
  106. spec:
  107. ports:
  108. - port: 10000
  109. targetPort: 10000
  110. name: thrift
  111. - port: 10002
  112. targetPort: 10002
  113. name: webui
  114. - port: 9083
  115. targetPort: 9083
  116. name: metastore
  117. selector:
  118. app: hive
  119. type: NodePort

依次启动上面所配置的yaml文件

  1. kubectl create -f hivepv.yaml
  2. kubectl create -f mysql.yaml
  3. kubectl create -f hive-config.yaml
  4. kubectl create -f hive-deploy.yaml

查看部署结果

  1. kubectl get pods
  2. NAME READY STATUS RESTARTS AGE
  3. hadoop-datanode-1 1/1 Running 0 39h
  4. hadoop-datanode-2 1/1 Running 0 39h
  5. hadoop-datanode-3 1/1 Running 0 39h
  6. hdfs-master 1/1 Running 0 39h
  7. hive-7d49bc9cd4-jrg6q 1/1 Running 0 150m
  8. hive-metadata-mysql-f675498d5-gjdcf 1/1 Running 0 14h
  9. yarn-master 1/1 Running 0 39h
  10. yarn-node-1 1/1 Running 0 39h
  11. yarn-node-2 1/1 Running 0 39h
  12. yarn-node-3 1/1 Running 0 39h

参考

CSDN:在Kubernetes上部署Hive
https://blog.csdn.net/chenleiking/article/details/82492282