一些场景下,在开发时可能存在dubbo consumer需要访问k8s中部署的dubbo provider,尤其是对于自建的kubernetes集群环境,tcp的端口很难代理,这就导致了开发发链接集群内的服务比较麻烦,这里可以利用service将dubbo服务使用nodeport暴露出去,但是由于dubbo默认注册的ip地址是自动获取服务的ip地址来注册的,ip地址是pod的ip地址,集群外是无法访问的。幸运的是dubbo提供了自动义指定注册ip的配置,我们可以在部署开发联调服务时指定ip地址为宿主机的ip地址。部署例子如下:

# -------------------Service ---------------------- #

apiVersion: v1

kind: Service

metadata:

name: java-test-svc

namespace: default

spec:

type: NodePort

ports:

- port: 30011

targetPort: 30011

nodePort: 30011

selector:

app: java-test

tier: backend

---

# ------------------- Deployment ------------------- #

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

name: java-test-deployment

namespace: default

spec:

selector:

matchLabels:

app: java-test

tier: backend

replicas: 1

template:

metadata:

labels:

app: java-test

tier: backend

spec:

nodeSelector:

dubbo_provder: dubbo_provider

containers:

- name: java-test

image: [your image]

imagePullPolicy: Always

# resources:

# requests:

# cpu: 100m

# memory: 100Mi

# limits:

# cpu: 100m

# memory: 100Mi

env:

- name: APOLLO_META

value: http://172.31.205.22:8080

- name: DUBBO_IP_TO_REGISTRY

value: 172.31.205.23

- name: DUBBO_PORT_TO_REGISTRY

value: "30011"

- name: DUBBO_PORT_TO_BIND

value: "30011"

ports:

- containerPort: 30011

imagePullSecrets:

- name: harbor-key

---

# ------------------- ingress ---------------------- #

apiVersion: extensions/v1beta1

kind: Ingress

metadata:

name: java-test-ingress

namespace: default

spec:

rules:

- host: javatest.com

http:

#port: 80

paths:

- path: /

backend:

serviceName: java-test-svc

servicePort: 30011

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐