多个app节点如何在kubernetes中公开jmx?

时间:2016-02-03 18:13:11

标签: docker kubernetes kubernetes-service

  1. kubernetes我可以使用service公开服务。这很好。
  2. 假设我有1个Web实例和10个java服务器实例。
  3. 我有一个Windows网关我曾经通过安装在它上面的jconsole访问这10个java服务器实例。
  4. 显然我不会通过kubernetes服务公开所有应用jmx端口。
  5. 我有什么选择?我应该如何允许这个外部的kubernetes集群windows网关访问那些10个服务器的jmx端口?这里有什么做法吗?

4 个答案:

答案 0 :(得分:59)

另一个选择是使用 kubectl port-forward 将JMX端口从K8 pod转发到本地PC。

我这样做:

1)。将以下JVM选项添加到您的应用程序:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.port=1099
-Dcom.sun.management.jmxremote.rmi.port=1099
-Djava.rmi.server.hostname=127.0.0.1

这里的关键部分是:

  • 同一个端口应该用作'jmxremote.port'和'jmxremote.rmi.port'。这只需要转发一个端口。

  • 127.0.0.1应作为rmi服务器主机名传递。这是JMX连接通过端口转发工作所必需的。

2)。通过kubectl将JMX端口(1099)转发到本地PC:

kubectl port-forward <your-app-pod> 1099

3)。打开与本地端口1099的jconsole连接:

jconsole 127.0.0.1:1099

这种方式可以通过JMX调试任何Java pod,而无需通过K8服务公开公开JMX(从安全角度来看更好)。

另一个可能有用的选项是将Jolokia(https://jolokia.org/)代理附加到容器内的Java进程,以便它通过HTTP端口代理JMX并公开或端口转发此HTTP端口以查询JMX通过HTTP。

答案 1 :(得分:2)

我们是按照以下方式做到的

  1. 为每个广告连播添加唯一标签。例如:podid = asdw23443
  2. 使用podid = asdw23443的选择器创建新服务。确保在服务中通过nodeport或loadbalancer在pod上公开jmx端口。
  3. 如果您在服务中选择nodeport,因为您正在执行NAT操作,您可能必须为需要通过jconsole连接的每个jvm提供以下JVM参数

    -Djava.rmi.server.hostname=<your-ip-address>
    

答案 2 :(得分:1)

我认为一种方法是使用唯一的字符串\ id为pod添加标签,例如pod_name,并使用expose命令使用此唯一id \ string的选择器创建新服务。

# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed 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.

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 1.8.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-c&qt=71oi^e5s8(ene*$b89^#%*0xeve$x_trs91veok9#0h0'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'coursework',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

# [START db_setup]

# set this if you want to run django commands from development against CloudSQL eg to set up database etc
#make_cloudsql = 'prod'

import os
"""
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/quizalitious:myfcacoursedb',
            'NAME': 'mydb',
            'USER': 'root',
        }
    }
elif os.getenv('SETTINGS_MODE') == 'prod':
"""    # Running in development, but want to access the Google Cloud SQL instance in production.
DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'INSTANCE': '173.194.228.69',
            'NAME': 'mydb',
            'USER': 'thwaites',
            'PASSWORD' : 'secret',
        }
    }
"""else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mydb',
            'USER': 'root',
            'PASSWORD': 'secret',
            'HOST': 'localhost',
            'PORT': '3306',
            'OPTIONS': {
                'autocommit': True,
                }
        }
    }
"""
# [END db_setup]

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_ROOT='static'
STATIC_URL = '/static/'

MEDIA_ROOT='media'
MEDIA_URL = '/media/'

答案 3 :(得分:1)

除了https://stackoverflow.com/a/39927197/1387184外,我想一次监视同一个Pod的所有实例,因为我们将端口硬编码为1099,所以很难做到,因为我只能使用该端口将一个Portforward转发到一个Pod。

在运行docker时,我使用Shell脚本动态分配了pod

Dockerfile CMD /run.sh

run.sh

JMX_PORT=$(((RANDOM % 20)+1099))

echo "Running JMX on Port $JMX_PORT"

java ``eval echo $JAVA_OPTS`` ...

deployment.yml env: - name: JAVA_OPTS value: "-Xms256m -Xmx6144m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.port=$JMX_PORT -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT -Djava.rmi.server.hostname=127.0.0.1"

评估将评估JMX_PORT的bash值,启动时每个吊舱可能会获得不同的吊舱。我