Skip to content
Permalink
Browse files
Add 3.0 Kubernetes demos (#263)
  • Loading branch information
AlbumenJ committed Jan 21, 2021
1 parent 1a99e0b commit d0bd96513ba7f647f9dbed5285b95e8395f547fb
Show file tree
Hide file tree
Showing 22 changed files with 1,081 additions and 0 deletions.
@@ -0,0 +1,179 @@
# Steps to run the samples

### Config Kubernetes

There are two ways to use Kubernetes as registry in dubbo.

1. API Server

`dubbo-samples-kubernetes-apiserver` is the sample of API server.

1.1 Token

In order to access Api Server, you need to set up a Service Account with the following permissions in your Kubernetes Cluster.

- Read and Write permission to Pods
- Read permission to Services
- Read permission to Endpoints

Here is an example to create this.

``` yaml
apiVersion: v1
kind: Namespace
metadata:
name: dubbo-demo
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: dubbo-demo
name: dubbo-role
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list", "update", "patch"]
- apiGroups: [""]
resources: ["services", "endpoints"]
verbs: ["get", "watch", "list"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: dubbo-sa
namespace: dubbo-demo
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: dubbo-sa-bind
namespace: dubbo-demo
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: dubbo-role
subjects:
- kind: ServiceAccount
name: dubbo-sa
```

Then, Retrieve the token for the `dubbo-sa` service account.

```
kubectl -n dubbo-demo describe secret $(kubectl -n dubbo-demo get secret | grep dubbo-sa | awk '{print $1}')
```

Copy the <authentication_token> value from the output and replace with `oauthToken` property in properties file:

```
Name: dubbo-sa-token-2g5c6
Namespace: dubbo-demo
Labels: <none>
Annotations: kubernetes.io/service-account.name: dubbo-sa
kubernetes.io/service-account.uid: 963e68f3-738d-4f10-bf32-92a3fbf44774
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 10 bytes
token: <authentication_token>
```

1.2 API URL

Get the API URL by running this command:

```
kubectl cluster-info | grep 'Kubernetes master' | awk '/http/ {print $NF}'
```

You can get the output like:

```
https://<API Server ip>:<API Server port>
```

Copy <API Server ip> and <API Server port> to properties.

Notice: protocol is needless to specify, dubbo will use `https` protocol as default.

2. DNS

`dubbo-samples-kubernetes-dns` is the sample of DNS.

In order to use DNS as registry, you need to retrieve DNS host from Kubernetes.
You can get it by this.

```
kubectl get service -n kube-system | grep kube-dns
```

```
kube-dns ClusterIP <DNS ip> <none> 53/UDP,53/TCP,9153/TCP 68d
```

Copy <DNS ip> to properties.

### Start demo

1. Build maven project and deploy to docker registry with `Dockerfile`.

2. Deploy the provider to kubernetes cluster.

``` yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: kubernetes-apiserver-demo-provider
spec:
replicas: 3
selector:
matchLabels:
tier: apiserver-demo-provider
template:
metadata:
labels:
tier: apiserver-demo-provider
io.dubbo: MyTest
spec:
containers:
- name: server
image: < Your image path >
ports:
- containerPort: 20880
livenessProbe:
httpGet:
path: /live
port: 22222
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 22222
initialDelaySeconds: 5
periodSeconds: 5
startupProbe:
httpGet:
path: /startup
port: 22222
failureThreshold: 30
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: kubernetes-apiserver-demo-provider
spec:
clusterIP: None
selector:
io.dubbo: MyTest
ports:
- protocol: TCP
port: 20880
targetPort: 20880
```

3. Run Dubbo consumer demo `org.apache.dubbo.samples.ConsumerBootstrap.main`
@@ -0,0 +1,3 @@
FROM openjdk:8-jre
ADD ./target/dubbo-samples-kubernetes-apiserver-1.0-SNAPSHOT.jar /dubbo-samples-kubernetes-apiserver-1.0-SNAPSHOT.jar
ENTRYPOINT ["java", "-classpath", "/dubbo-samples-kubernetes-apiserver-1.0-SNAPSHOT.jar", "org.apache.dubbo.samples.ProviderBootstrap"]
@@ -0,0 +1,162 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You 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">
<groupId>org.apache.dubbo</groupId>
<version>1.0-SNAPSHOT</version>

<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-samples-kubernetes-apiserver</artifactId>

<properties>
<source.level>1.8</source.level>
<target.level>1.8</target.level>
<dubbo.version>3.0.0-SNAPSHOT</dubbo.version>
<spring.version>4.3.16.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-kubernetes</artifactId>
<version>${dubbo.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>

<profiles>
<!-- For jdk 11 above JavaEE annotation -->
<profile>
<id>javax.annotation</id>
<activation>
<jdk>[1.11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${source.level}</source>
<target>${target.level}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.apache.dubbo.samples.ProviderBootstrap</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/dubbo/internal
</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<layout>default</layout>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
</repository>
</repositories>

</project>
@@ -0,0 +1,46 @@
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.samples;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.samples.action.GreetingServiceConsumer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

public class ConsumerBootstrap {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
GreetingServiceConsumer greetingServiceConsumer = context.getBean(GreetingServiceConsumer.class);
String hello = greetingServiceConsumer.doSayHello("Kubernetes Api Server");
System.out.println("result: " + hello);
}

@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"org.apache.dubbo.samples.action"})
static class ConsumerConfiguration {

}
}

0 comments on commit d0bd965

Please sign in to comment.