Skip to content

Commit

Permalink
[ISSUE #8309] Sync services and endpoints from k8s (#9220)
Browse files Browse the repository at this point in the history
* [ISSUE#8309] Nacos supports synchronizing service metadata from k8s service discovery. (#7211)  * Synchronize k8s services.  * Synchronize k8s endpoints.

* ApiClient uses the default configuration instead

* Start registered Informers after registering handlers

* Compare oldServicePorts and newServicePorts to determine portChange

* Delete the logic of service renaming judgment

* Fix error in the process of registering instances

* Use independent log

* add k8s sync switch and start informer optimization

* start informer optimization
  • Loading branch information
EmanuelGi authored Sep 29, 2022
1 parent 34bbd38 commit 3f3ea5e
Show file tree
Hide file tree
Showing 10 changed files with 642 additions and 0 deletions.
4 changes: 4 additions & 0 deletions console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>nacos-istio</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-k8s-sync</artifactId>
</dependency>

<!-- log -->
<!-- log4j通过slf4j来代理 -->
Expand Down
6 changes: 6 additions & 0 deletions console/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ nacos.core.auth.plugin.nacos.token.secret.key=SecretKey0123456789012345678901234
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false

#*************** K8s Related Configurations ***************#
### If turn on the K8s sync:
nacos.k8s.sync.enabled=false

### If use the Java API from an application outside a kubernetes cluster
#nacos.k8s.sync.outsideCluster=false
#nacos.k8s.sync.kubeConfig=/.kube/config

###*************** Add from 1.3.0 ***************###

Expand Down
8 changes: 8 additions & 0 deletions distribution/conf/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ nacos.core.auth.plugin.nacos.token.secret.key=SecretKey0123456789012345678901234
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false

#*************** K8s Related Configurations ***************#
### If turn on the K8s sync:
nacos.k8s.sync.enabled=false

### If use the Java API from an application outside a kubernetes cluster
#nacos.k8s.sync.outsideCluster=false
#nacos.k8s.sync.kubeConfig=/.kube/config

#*************** Core Related Configurations ***************#

### set the WorkerID manually
Expand Down
8 changes: 8 additions & 0 deletions distribution/conf/application.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ nacos.core.auth.server.identity.value=security
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false

#*************** K8s Related Configurations ***************#
### If turn on the K8s sync:
nacos.k8s.sync.enabled=false

### If use the Java API from an application outside a kubernetes cluster
#nacos.k8s.sync.outsideCluster=false
#nacos.k8s.sync.kubeConfig=/.kube/config

#*************** Core Related Configurations ***************#

### set the WorkerID manually
Expand Down
22 changes: 22 additions & 0 deletions distribution/conf/nacos-logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,23 @@
</encoder>
</appender>

<appender name="k8s-sync-main"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/k8s-sync-main.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/k8s-sync-main.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>2GB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>7GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<Pattern>%date %level %msg%n%n</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>


<logger name="com.alibaba.nacos.address.main" additivity="false">
<level value="INFO"/>
Expand Down Expand Up @@ -702,6 +719,11 @@
<level value="DEBUG"/>
<appender-ref ref="core-auth"/>
</logger>

<logger name="com.alibaba.nacos.k8s.sync.main" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="k8s-sync-main"/>
</logger>

<logger name="com.alibaba.nacos.core.protocol.raft" additivity="false">
<level value="INFO"/>
Expand Down
59 changes: 59 additions & 0 deletions k8s-sync/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2022 Alibaba Group Holding Ltd.
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-k8s-sync</artifactId>
<packaging>jar</packaging>

<name>nacos-k8s-sync ${project.version}</name>
<url>http://nacos.io</url>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-core</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-naming</artifactId>
</dependency>

<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-api</artifactId>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.20</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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.
*/

package com.alibaba.nacos.k8s.sync;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* Configurations for k8s integration.
*
* @author EmanuelGi
*/
@Component
public class K8sSyncConfig {
@Value("${nacos.k8s.sync.enabled:false}")
private boolean enabled = false;

@Value("${nacos.k8s.sync.outsideCluster:false}")
private boolean outsideCluster = false;

@Value("${nacos.k8s.sync.kubeConfig:}")
private String kubeConfig;

public boolean isEnabled() {
return enabled;
}

public boolean isOutsideCluster() {
return outsideCluster;
}

public String getKubeConfig() {
return kubeConfig;
}
}
Loading

0 comments on commit 3f3ea5e

Please sign in to comment.