【docker】elasticsearch镜像制作

1.环境准备 

mkdir /data/es 
cd /data/es 

2.使用harbor进行运行一个容器

docker run -it -d --name es9200 -h es9200 --privileged 192.168.1.11:443/myharbor/centos:7.9


chmod -R 775 /esdb 
groupadd -g 60001 esadmin
useradd -u 61001 -g esadmin esadmin
chown -R esadmin:esadmin /esdb
chmod -R 775 /esdb
echo "esadmin" | passwd --stdin esadmin

3.容器内对环境初始化

--容器内操作。
echo "session required pam_limits.so" >> /etc/pam.d/login
cat /etc/pam.d/login
--容器内操作。

vi /etc/pam.d/su
#这一行加上注释,否则后面esadmin 不能连接
#session include system-auth


sysctl -p
--容器内操作
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
hwclock

echo "SELINUX=disabled" > /etc/selinux/config
echo "#SELINUXTYPE=targeted " >> /etc/selinux/config
cat /etc/selinux/config
systemctl disable firewalld.service


4.容器外部上传es和JDK 

192.168.1.7:
docker cp /data/es/jdk-14.0.2_linux-x64_bin.tar.gz es9200:/esdb/soft/
docker cp /data/es/elasticsearch-7.9.0-linux-x86_64.tar.gz es9200:/esdb/soft/

5.容器内环境设置

docker exec -it es9200 bash
su - esadmin
cd /esdb/
tar zxvf /esdb/soft/jdk-14.0.2_linux-x64_bin.tar.gz
export LANG=en_US.UTF8

cat >> ~/.bash_profile << EOF
export JAVA_HOME=/esdb/jdk-14.0.2
export PATH=/esdb/jdk-14.0.2/bin:\$PATH
export LANG=en_US.UTF8
EOF


source ~/.bash_profile
cd /esdb/soft/
tar zxvf elasticsearch-7.9.0-linux-x86_64.tar.gz
mv elasticsearch-7.9.0/* /esdb/esapp/esapp9200/

*/
--docker内。
vi /esdb/esapp/esapp9200/bin/elasticsearch-env


--docker内。

vi /esdb/esapp/esapp9200/config/elasticsearch.yml


--docker外。
root:
su - esadmin -c "nohup /esdb/esapp/esapp9200/bin/elasticsearch >/esdb/eslog/eslog9200/nohup.es &"
su - esadmin -c "/esdb/esapp/esapp9200/bin/elasticsearch"

tail -100f /esdb/eslog/eslog9200/nohup.es
[2024-07-06T10:57:56,048][INFO ][o.e.l.LicenseService     ] [es9200] license [9809c584-8db0-4507-a207-4ac2474817eb] mode [basic] - valid
[2024-07-06T10:57:56,052][INFO ][o.e.x.s.s.SecurityStatusChangeListener] [es9200] Active license is now [BASIC]; Security is disabled


--docker内。
curl http://es9200:9200/_cat/health?pretty -H 'Content-Type: application/json'
[root@es9200 /]# curl http://es9200:9200/_cat/health?pretty -H 'Content-Type: application/json'
1720234761 02:59:21 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%

curl -XPOST http://es9200:9200/sspudb/_doc/?pretty -H 'Content-Type:application/json' -d '
{
"id" : "1",
"name" : "sspu001",
"city" : "深圳",
"course" : "oracle",
"teacher" : "xueshuangqi",
"pxdate": "20240829"
}'
--回显 
{
  "_index" : "sspudb",
  "_type" : "_doc",
  "_id" : "LIf8hZABHdiVjxWtH2Rd",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

--sspudb 数据库查询 
curl -XPOST 'http://es9200:9200/sspudb/_search?pretty' -H 'Content-Type:application/json'
[root@es9200 /]# curl -XPOST 'http://es9200:9200/sspudb/_search?pretty' -H 'Content-Type:application/json'
{
  "took" : 93,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sspudb",
        "_type" : "_doc",
        "_id" : "LIf8hZABHdiVjxWtH2Rd",
        "_score" : 1.0,
        "_source" : {
          "id" : "1",
          "name" : "sspu001",
          "city" : "深圳",
          "course" : "oracle",
          "teacher" : "xueshuangqi",
          "pxdate" : "20240829"
        }
      }
    ]
  }
}

--插入数据 
curl -XPOST http://es9200:9200/sspudb/_doc/?pretty -H 'Content-Type:application/json' -d '
{
"id" : "2",
"name" : "sspu002",
"city" : "广州",
"course" : "oracle",
"teacher" : "xueshuangqi",
"pxdate": "20240830"
}'
--回显 
{
  "_index" : "sspudb",
  "_type" : "_doc",
  "_id" : "Lof9hZABHdiVjxWtO2SP",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

--数据查询 
curl -XPOST 'http://es9200:9200/sspudb/_search?pretty' -H 'Content-Type: application/json'
[root@es9200 /]# curl -XPOST 'http://es9200:9200/sspudb/_search?pretty' -H 'Content-Type: application/json'
{
  "took" : 151,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sspudb",
        "_type" : "_doc",
        "_id" : "LIf8hZABHdiVjxWtH2Rd",
        "_score" : 1.0,
        "_source" : {
          "id" : "1",
          "name" : "sspu001",
          "city" : "深圳",
          "course" : "oracle",
          "teacher" : "xueshuangqi",
          "pxdate" : "20240829"
        }
      },
      {
        "_index" : "sspudb",
        "_type" : "_doc",
        "_id" : "Lof9hZABHdiVjxWtO2SP",
        "_score" : 1.0,
        "_source" : {
          "id" : "2",
          "name" : "sspu002",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "xueshuangqi",
          "pxdate" : "20240830"
        }
      }
    ]
  }
}

6.容器外部进行制作

--容器外部。
[root@es1 ~]# docker ps
CONTAINER ID   IMAGE                                  COMMAND            CREATED          STATUS          PORTS     NAMES
f3fbc0cfb60f   192.168.1.11:443/myharbor/centos:7.9   "/usr/sbin/init"   26 minutes ago   Up 26 minutes             es9200

docker stop es9200
docker start es9200

[root@es1 ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED             SIZE
192.168.1.11:443/myharbor/mongodb   5.0.9     8b0373bbbea0   About an hour ago   979MB
mongodb                             5.0.9     8b0373bbbea0   About an hour ago   979MB
192.168.1.11:443/myharbor/redis     latest    19ca367715ea   About an hour ago   113MB
redis                               latest    19ca367715ea   About an hour ago   113MB
192.168.1.11:443/myharbor/mysql     5.7.37    677788b528f9   15 hours ago        10.4GB
mysql                               5.7.37    677788b528f9   15 hours ago        10.4GB
192.168.1.11:443/myharbor/centos    7.9       77596ed99450   23 hours ago        490MB
redis                               <none>    7614ae9453d1   2 years ago         113MB

--向本地提交一个新的镜像。
docker commit es9200 es9200:7.9.0
[root@es1 ~]# docker commit es9200 es9200:7.9.0
sha256:0f71870265b248e4968b1f4ca0a43c6f722d471959640ceb2ff5522c8f470778
[root@es1 ~]# 
[root@es1 ~]# docker images
REPOSITORY                          TAG       IMAGE ID       CREATED             SIZE
es9200                              7.9.0     0f71870265b2   5 seconds ago       1.88GB
mongodb                             5.0.9     8b0373bbbea0   About an hour ago   979MB
192.168.1.11:443/myharbor/mongodb   5.0.9     8b0373bbbea0   About an hour ago   979MB
redis                               latest    19ca367715ea   About an hour ago   113MB
192.168.1.11:443/myharbor/redis     latest    19ca367715ea   About an hour ago   113MB
192.168.1.11:443/myharbor/mysql     5.7.37    677788b528f9   15 hours ago        10.4GB
mysql                               5.7.37    677788b528f9   15 hours ago        10.4GB
192.168.1.11:443/myharbor/centos    7.9       77596ed99450   23 hours ago        490MB
redis                               <none>    7614ae9453d1   2 years ago         113MB

docker rm -f es9200
--在容器 es9200 的基础上提交新的镜像:es9200:7.9.0


--本地环境:让容器持久化到本地。
cd /data/es

cat > dockerfile-esdb.yml << EOF
FROM es9200:7.9.0
MAINTAINER xueshuangqi
WORKDIR /esdb
VOLUME ["/esdb/esdata/esdata9200"]
EXPOSE 9200
CMD ["su","-","esadmin","-c","/esdb/esapp/esapp9200/bin/elasticsearch"]
EOF

--创建一个新的镜像。
cat > docker-build-esdb.sh << EOF
#!/bin/bash
docker build --rm -t esdb:7.9.0 -f dockerfile-esdb.yml .
EOF

chmod u+x docker-build-esdb.sh
./docker-build-esdb.sh
........................
Removing intermediate container d43efd5ad6be
 ---> f699bf624651
Successfully built f699bf624651
Successfully tagged esdb:7.9.0

--创建本地物理卷。
docker volume create esdata
ls /data/docker/data/volumes/esdata/_data/

docker run -it -d --name es9200 -h es9200 \
--mount src=esdata,dst=/esdb/esdata/esdata9200 \
-p 9200:9200 esdb:7.9.0

[root@es1 es]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS         PORTS                                       NAMES
f506137e4d71   esdb:7.9.0   "su - esadmin -c /es…"   11 seconds ago   Up 9 seconds   0.0.0.0:9200->9200/tcp, :::9200->9200/tcp   es9200


docker exec -it es9200 bash


--物理主机操作。
--192.168.1.7:
ls /data/docker/data/volumes/esdata/_data/
[root@es1 es]# ls /data/docker/data/volumes/esdata/_data/
nodes
--查询数据 
curl -XPOST 'http://192.168.1.7:9200/sspudb/_search?pretty' -H 'Content-Type: application/json'
[root@es1 es]# curl -XPOST 'http://192.168.1.7:9200/sspudb/_search?pretty' -H 'Content-Type: application/json'
{
  "took" : 108,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "sspudb",
        "_type" : "_doc",
        "_id" : "LIf8hZABHdiVjxWtH2Rd",
        "_score" : 1.0,
        "_source" : {
          "id" : "1",
          "name" : "sspu001",
          "city" : "深圳",
          "course" : "oracle",
          "teacher" : "xueshuangqi",
          "pxdate" : "20240829"
        }
      },
      {
        "_index" : "sspudb",
        "_type" : "_doc",
        "_id" : "Lof9hZABHdiVjxWtO2SP",
        "_score" : 1.0,
        "_source" : {
          "id" : "2",
          "name" : "sspu002",
          "city" : "广州",
          "course" : "oracle",
          "teacher" : "xueshuangqi",
          "pxdate" : "20240830"
        }
      }
    ]
  }
}

7.镜像上传到harbor 

--将制作好的容器镜像上传到:远程harbor;
docker rm -f es9200
docker tag esdb:7.9.0 192.168.1.11:443/myharbor/esdb:7.9.0
docker push 192.168.1.11:443/myharbor/esdb:7.9.0
[root@es1 es]# docker push 192.168.1.11:443/myharbor/esdb:7.9.0
The push refers to repository [192.168.1.11:443/myharbor/esdb]
acdc5b0f5108: Pushed 
53c61a520c85: Mounted from myharbor/mongodb 
88765da43086: Mounted from myharbor/mongodb 
2ca40431ab7d: Mounted from myharbor/mongodb 
caceb42f09c2: Mounted from myharbor/mongodb 
5b1d42135d46: Mounted from myharbor/mongodb 
174f56854903: Mounted from myharbor/mongodb 
7.9.0: digest: sha256:9f202995efdd61f86851fdf6f6a7d0dd1cb869097322263d6d451800b108227a size: 1786

8.总结 
上面的镜像=
(1)centos7.9 
(2)jdk14
(3)es7.9 
(4)环境配置(目录+参数)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值