介绍

Kompose 是什么?它是个转换工具,可将 compose(即 Docker Compose)所组装的所有内容 转换成容器编排器(Kubernetes 或 OpenShift)可识别的形式。

官网http://kompose.io

安装

# Linux
curl -L https://github.com/kubernetes/kompose/releases/download/v1.24.0/kompose-linux-amd64 -o kompose

# macOS
curl -L https://github.com/kubernetes/kompose/releases/download/v1.24.0/kompose-darwin-amd64 -o kompose

# Windows
curl -L https://github.com/kubernetes/kompose/releases/download/v1.24.0/kompose-windows-amd64.exe -o kompose.exe

chmod +x kompose
sudo mv ./kompose /usr/local/bin/kompose

云计算-使用kompose将docker-compose文件转换为k8s资源_elastic

docker-compose部署elasticsearch

docker-compose文件目录结构如下

├── docker-compose-elasticsearch.yml
└── elasticsearch
    ├── config
    │   ├── elasticeearch.yml
    │   └── jvm.options
    ├── data
    ├── kibana
    │   └── config
    │       └── kibana.yml
    └── logs

docmpose文件如下

version: '3'

# 网桥es -> 方便相互通讯
networks:
  es:

services:
  elasticsearch:
    image: registry.cn-hangzhou.aliyuncs.com/zhengqing/elasticsearch:7.14.1      # 原镜像`elasticsearch:7.14.1`
    container_name: elasticsearch             # 容器名为'elasticsearch'
    restart: unless-stopped                           # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
    volumes:                                  # 数据卷挂载路径设置,将本机目录映射到容器目录
      - "./elasticsearch/data:/usr/share/elasticsearch/data"
      - "./elasticsearch/logs:/usr/share/elasticsearch/logs"
      - "./elasticsearch/config:/usr/share/elasticsearch/config"
#      - "./elasticsearch/config/jvm.options:/usr/share/elasticsearch/config/jvm.options"
#- "./elasticsearch/plugins/ik:/usr/share/elasticsearch/plugins/ik" # IK中文分词插件
    environment:                              # 设置环境变量,相当于docker run命令中的-e
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      TAKE_FILE_OWNERSHIP: "true"  # 权限
      discovery.type: single-node
      ES_JAVA_OPTS: "-Xmx512m -Xms512m"
      ELASTIC_PASSWORD: "123456" # elastic账号密码
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - es

  kibana:
    image: registry.cn-hangzhou.aliyuncs.com/zhengqing/kibana:7.14.1       # 原镜像`kibana:7.14.1`
    container_name: kibana
    restart: unless-stopped
    volumes:
      - ./elasticsearch/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
    ports:
      - "5601:5601"
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    networks:
      - es

config/elatiscsearch,yml文件

cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.port: 9200
# 开启es跨域
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
# 开启安全控制
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

config/jvm.options文件

## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly

## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30

## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
-XX:HeapDumpPath=data

# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log

## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m

/kibana/config/kibana.yml配置文件

server.name: kibana
server.host: "0.0.0.0"
server.publicBaseUrl: "http://kibana:5601"  # 这里地址改为你访问kibana的地址,不能以 / 结尾
elasticsearch.hosts: [ "http://elasticsearch:9200" ] # http://www.zhengqingya.com:9200 TODO 修改为自己的ip
xpack.monitoring.ui.container.elasticsearch.enabled: true
elasticsearch.username: "elastic"  # es账号
elasticsearch.password: "123456"   # es密码
i18n.locale: zh-CN # 中文

启动查看

#启动
docker-compose -f docker-compose-elasticsearch.yml -p elasticsearch up -d
#查看
docker-compose -f docker-compose-elasticsearch.yml ps

  • ES访问地址:ip地址:9200 默认账号密码:elastic/123456
  • kibana访问地址:ip地址:5601/app/dev_tools#/console 默认账号密码:elastic/123456

修改密码

# 进入容器
docker exec -it elasticsearch /bin/bash
# 设置密码-随机生成密码
# elasticsearch-setup-passwords auto
# 设置密码-手动设置密码
elasticsearch-setup-passwords interactive
# 访问
curl 127.0.0.1:9200 -u elastic:123456

Kompose转换

按照如下进行转换

kompose convert -f  kompose convert -f docker-compose-elasticsearch.yml               

云计算-使用kompose将docker-compose文件转换为k8s资源_docker_02

对比生成的k8s资源文件

  • 每个 docker-compose 容器,会生成为一个 deployment,并为你自动转换好 label 和 env 等字段
  • Docker compose 的 volumes 字段,会转换为 PVC

云计算-使用kompose将docker-compose文件转换为k8s资源_docker_03

如果pvc存储不够,可以后期自己手动调整

执行转换,应用,-n对应要部署的命名空间

kubectl apply -f elasticsearch-claim1-persistentvolumeclaim.yaml -n n9e
kubectl apply -f elasticsearch-cm2-configmap.yaml -n n9e
kubectl apply -f elasticsearch-deployment.yaml -n n9e
kubectl apply -f elasticsearch-service.yaml -n n9e
kubectl apply -f kibana-cm0-configmap.yaml -n n9e
kubectl apply -f kibana-deployment.yaml -n n9e
kubectl apply -f kibana-service.yaml -n n9e

#查看
kubectl get deploy,cm,svc,pvc -n n9e

云计算-使用kompose将docker-compose文件转换为k8s资源_elastic_04

云计算-使用kompose将docker-compose文件转换为k8s资源_elasticsearch_05