Skip to content

Commit

Permalink
Merge pull request #25 from nacos-group/feature_support_grpc
Browse files Browse the repository at this point in the history
Feature support grpc
  • Loading branch information
JianweiWang authored Apr 25, 2023
2 parents 6d8e990 + a36af97 commit 0b02acf
Show file tree
Hide file tree
Showing 28 changed files with 1,071 additions and 389 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# nacos-coredns-plugin [中文](./README_CN.md) #
This project provides a **DNS-F** client based on [CoreDNS](https://coredns.io/), which can help export those registed services on Nacos as DNS domain. **DNS-F** client is a dedicated agent process(side car) beside the application's process to foward the service discovery DNS domain query request to Nacos.

## Quic Start
To build and run nacos coredns plugin, the OS must be Linux or Mac. And also, golang environments(GOPATH,GOROOT,GOHOME) must be configured correctly.
To build and run nacos coredns plugin, the OS must be Linux or Mac. And also, make sure your nacos version is 2.0 or higher and golang version is 1.17 or higher. And golang environments(GOPATH,GOROOT,GOHOME) must be configured correctly. Because it needs to support the gRPC connection feature of the nacos2.x version and the go mod function.

### Build
```
Expand All @@ -14,24 +15,25 @@ sh build.sh
To run nacos coredns plugin, you need a configuration file. A possible file may be as bellow:
```
. {
log
nacos {
upstream /etc/resolv.conf
nacos_server 127.0.0.1
nacos_server_port 8848
nacos_namespaceId public
nacos_server_host 127.0.0.1:8848
}
forward . /etc/resolv.conf
}
```
* upstream: domain names those not registered in nacos will be forwarded to upstream.
* nacos_server: Ips of nacos server, seperated by comma if there are two or more nacos servers
* nacos_server_port: Nacos server port
* forward: domain names those not registered in nacos will be forwarded to upstream.
* nacos_namespaceId: nacos namespaceId, defalut is public.
* nacos_server_host: Ip and Port of nacos server, seperated by comma if there are two or more nacos servers

### Run
* Firstly, you need to deploy nacos server. [Here](https://github.com/alibaba/nacos)
* Secondly, register service on nacos.
* Then run ```$GOPATH/src/coredns/coredns -conf $path_to_corefile -dns.port $dns_port```
![image](https://cdn.nlark.com/lark/0/2018/png/7601/1542623914418-f529409b-c229-4ef9-aec3-b9c5df23c906.png)
![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504581023-95437fee-0e3d-4b6a-851c-44a352dedd81.png)

### Test
dig $nacos_service_name @127.0.0.1 -p $dns_port

![image](https://cdn.nlark.com/lark/0/2018/png/7601/1542624023214-29cd9f71-0183-4231-b092-57535e8cfcfe.png)
![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504588231-341b38fe-da55-41eb-a24b-e3752b86faa4.png)
41 changes: 41 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# nacos-coredns-plugin [English](./README.md) #
本项目提供了一个基于CoreDNS的DNS-F客户端,可以将Nacos上注册的服务导出为DNS域名。 本DNS-F客户端是应用程序进程旁边的一个专用代理进程(side car),可以将服务名作为DNS域名查询请求转发到本客户端,提供服务发现的功能。
## 快速开始
要构建和运行本 nacos-coredns 插件,操作系统必须是 Linux 或 Mac。 另外,请确保您的 nacos 服务端版本为2.0或更高级版本,以及 golang 版本为 1.17 或更高级版本, 并且必须正确配置 golang 环境(GOPATH、GOROOT)。因为需要支持 nacos2.x 版本的gRPC连接功能和 go mod 功能。
### 构建
```
git clone https://github.com/nacos-group/nacos-coredns-plugin.git
cp nacos-coredns-plugin/bin/build.sh ~/
cd ~/
sh build.sh
```
### 配置
运行本 nacos-coredns 插件,您需要一个配置文件。 一个标准的配置文件如下:
```
. {
log
nacos {
nacos_namespaceId public
nacos_server_host console.nacos.io:8848
}
forward . /etc/resolv.conf
}
```
* forward:未在 nacos 注册的域名将被转发到upstream。
* nacos_namespaceId:nacos namespaceId,默认为public。
* nacos_server_host:nacos 服务端的IP地址和端口,如果有两个或多个 nacos 服务端,用逗号分隔

### 运行
* 首先需要部署一个nacos服务端。 [部署参考](https://github.com/alibaba/nacos)
* 其次,在nacos上注册服务。
* 然后输入配置文件 $path_to_corefile 和指定端口 $dns_port ,运行本项目

```$GOPATH/src/coredns/coredns -conf $path_to_corefile -dns.port $dns_port```

![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504581023-95437fee-0e3d-4b6a-851c-44a352dedd81.png)

### 服务发现例子
输入服务名 $nacos_service_name ,本项目部署的IP地址 $dns_ip 和端口 $dns_port

```dig $nacos_service_name @$dns_ip -p $dns_port ```
![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504588231-341b38fe-da55-41eb-a24b-e3752b86faa4.png)
34 changes: 34 additions & 0 deletions bin/build-mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# cd GOPATH
cd $GOPATH/src/

# remove codes
rm -rf coredns
rm -rf nacos-coredns-plugin

# clone current codes
git clone https://github.com/coredns/coredns.git
git clone https://github.com/nacos-group/nacos-coredns-plugin.git

# cd coredns directory
cd $GOPATH/src/coredns
git checkout -b v1.6.7 v1.6.7
go get github.com/cihub/seelog

# copy nacos plugin to coredns
cp -r ../nacos-coredns-plugin/nacos plugin/
cp -r ../nacos-coredns-plugin/forward/setup.go plugin/forward

# insert nacos into plugin
sed -i '' '/hosts/a\
"nacos",\
' core/dnsserver/zdirectives.go
sed -i '' '/coredns\/plugin\/hosts/a\
_ "github.com/coredns/coredns/plugin/nacos"\
' core/plugin/zplugin.go
sed -i '' '/hosts:hosts/a\
nacos:nacos\
' plugin.cfg
# modify import
# build
make
22 changes: 10 additions & 12 deletions bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ rm -rf coredns
rm -rf nacos-coredns-plugin

# clone current codes
git clone https://github.com/coredns/coredns.git
git clone https://github.com/nacos-group/nacos-coredns-plugin.git
git clone https://github.com/coredns/coredns.git


# cd coredns directory
cd $GOPATH/src/coredns
git checkout -b v1.2.6 v1.2.6
go get github.com/cihub/seelog
git checkout -b v1.9.3 v1.9.3

# copy nacos plugin to coredns
cp -r ../nacos-coredns-plugin/nacos plugin/
cp -r ../nacos-coredns-plugin/forward/setup.go plugin/forward
cp -r ../nacos-coredns-plugin/conf conf

# insert nacos into plugin
sed -i '/coredns\/core\/dnsserver/a\\t_ "coredns/plugin/nacos"' core/coredns.go
sed -i '/whoami/a\\t"nacos",' core/dnsserver/zdirectives.go
sed -i '/coredns\/plugin\/whoami/a\\t_ "coredns/plugin/nacos"' core/plugin/zplugin.go

# modify import
sed -i "s/github.com\/coredns\///g" `grep 'github.com/coredns/' -rl . | grep '.go'`
sed -i '/hosts/a\\t"nacos",' core/dnsserver/zdirectives.go
sed -i '/coredns\/plugin\/hosts/a\\t_ "github.com/coredns/coredns/plugin/nacos"' core/plugin/zplugin.go
sed -i '/hosts:hosts/a\nacos:nacos' plugin.cfg

cat Makefile | grep -v 'presubmit core/zplugin.go core/dnsserver/zdirectives.go godeps' > /tmp/Makefile
mv /tmp/Makefile .
go mod tidy

# build
make
make
10 changes: 6 additions & 4 deletions conf/nacos-coredns.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
. {
log
nacos {
upstream /etc/resolv.conf
nacos_server_port 8848
}
}
nacos_namespaceId public
nacos_server_host console.nacos.io:8848
}
forward . /etc/resolv.conf
}
Loading

0 comments on commit 0b02acf

Please sign in to comment.