You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by gy...@apache.org on 2022/09/21 07:53:18 UTC
[flink-kubernetes-operator] branch main updated: [FLINK-29165] addded kubernetes client code examples
This is an automated email from the ASF dual-hosted git repository.
gyfora pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-kubernetes-operator.git
The following commit(s) were added to refs/heads/main by this push:
new f5130eb7 [FLINK-29165] addded kubernetes client code examples
f5130eb7 is described below
commit f5130eb7439dc487cb0224e1dc21872056485bbd
Author: Gaurav Miglani <ga...@D11-Gaurav.Miglani-TEMP>
AuthorDate: Tue Sep 13 14:08:13 2022 +0530
[FLINK-29165] addded kubernetes client code examples
---
examples/kubernetes-client-examples/README.MD | 45 ++++++++++++
examples/kubernetes-client-examples/pom.xml | 43 ++++++++++++
.../main/java/org/apache/flink/examples/Basic.java | 79 ++++++++++++++++++++++
pom.xml | 1 +
4 files changed, 168 insertions(+)
diff --git a/examples/kubernetes-client-examples/README.MD b/examples/kubernetes-client-examples/README.MD
new file mode 100644
index 00000000..e07ff4d1
--- /dev/null
+++ b/examples/kubernetes-client-examples/README.MD
@@ -0,0 +1,45 @@
+<!--
+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.
+-->
+
+# Flink Kubernetes Client Code Example
+
+## Overview
+
+This is an end-to-end example of running Flink Job via code using the Flink Kubernetes Operator.
+
+*What's in this example?*
+
+1. Sample code for submitting an application similar to examples/basic.yaml programatically.
+
+## How does it work?
+
+Just like when we apply k8 yaml file to k8 cluster, internally operator does create all deployment objects and submit.
+
+
+## Usage
+
+The following steps assume that you have the Flink Kubernetes Operator installed and running in your environment.
+
+**Step 1**: Build K8 client maven project
+```bash
+cd examples/kubernetes-client-examples
+mvn clean package
+```
+
+**Step 2**: Run the main method in `Basic.java`
\ No newline at end of file
diff --git a/examples/kubernetes-client-examples/pom.xml b/examples/kubernetes-client-examples/pom.xml
new file mode 100644
index 00000000..2325565f
--- /dev/null
+++ b/examples/kubernetes-client-examples/pom.xml
@@ -0,0 +1,43 @@
+<!--
+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">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.flink</groupId>
+ <artifactId>flink-kubernetes-operator-parent</artifactId>
+ <version>1.2-SNAPSHOT</version>
+ <relativePath>../..</relativePath>
+ </parent>
+
+ <artifactId>kubernetes-client-examples</artifactId>
+ <name>Flink Kubernetes client code Example</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.flink</groupId>
+ <artifactId>flink-kubernetes-operator</artifactId>
+ <version>1.2-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/examples/kubernetes-client-examples/src/main/java/org/apache/flink/examples/Basic.java b/examples/kubernetes-client-examples/src/main/java/org/apache/flink/examples/Basic.java
new file mode 100644
index 00000000..007a4f9c
--- /dev/null
+++ b/examples/kubernetes-client-examples/src/main/java/org/apache/flink/examples/Basic.java
@@ -0,0 +1,79 @@
+/*
+ * 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.flink.examples;
+
+import org.apache.flink.kubernetes.operator.crd.FlinkDeployment;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkDeploymentSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.FlinkVersion;
+import org.apache.flink.kubernetes.operator.crd.spec.JobManagerSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.JobSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.Resource;
+import org.apache.flink.kubernetes.operator.crd.spec.TaskManagerSpec;
+import org.apache.flink.kubernetes.operator.crd.spec.UpgradeMode;
+
+import io.fabric8.kubernetes.api.model.ObjectMeta;
+import io.fabric8.kubernetes.client.DefaultKubernetesClient;
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.KubernetesClientException;
+
+import java.util.Map;
+
+import static java.util.Map.entry;
+
+/** client code for ../basic.yaml. */
+public class Basic {
+ public static void main(String[] args) {
+ FlinkDeployment flinkDeployment = new FlinkDeployment();
+ flinkDeployment.setApiVersion("flink.apache.org/v1beta1");
+ flinkDeployment.setKind("FlinkDeployment");
+ ObjectMeta objectMeta = new ObjectMeta();
+ objectMeta.setName("basic");
+ flinkDeployment.setMetadata(objectMeta);
+ FlinkDeploymentSpec flinkDeploymentSpec = new FlinkDeploymentSpec();
+ flinkDeploymentSpec.setFlinkVersion(FlinkVersion.v1_15);
+ flinkDeploymentSpec.setImage("flink:1.15");
+ Map<String, String> flinkConfiguration =
+ Map.ofEntries(entry("taskmanager.numberOfTaskSlots", "2"));
+ flinkDeploymentSpec.setFlinkConfiguration(flinkConfiguration);
+ flinkDeployment.setSpec(flinkDeploymentSpec);
+ flinkDeploymentSpec.setServiceAccount("flink");
+ JobManagerSpec jobManagerSpec = new JobManagerSpec();
+ jobManagerSpec.setResource(new Resource(1.0, "2048m"));
+ flinkDeploymentSpec.setJobManager(jobManagerSpec);
+ TaskManagerSpec taskManagerSpec = new TaskManagerSpec();
+ taskManagerSpec.setResource(new Resource(1.0, "2048m"));
+ flinkDeploymentSpec.setTaskManager(taskManagerSpec);
+ flinkDeployment
+ .getSpec()
+ .setJob(
+ JobSpec.builder()
+ .jarURI(
+ "local:///opt/flink/examples/streaming/StateMachineExample.jar")
+ .parallelism(2)
+ .upgradeMode(UpgradeMode.STATELESS)
+ .build());
+
+ try (KubernetesClient kubernetesClient = new DefaultKubernetesClient()) {
+ FlinkDeployment orReplace =
+ kubernetesClient.resource(flinkDeployment).createOrReplace();
+ } catch (KubernetesClientException e) {
+ // some error while connecting to kube cluster
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/pom.xml b/pom.xml
index eda2768d..114b46cb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -57,6 +57,7 @@ under the License.
<module>flink-kubernetes-webhook</module>
<module>flink-kubernetes-docs</module>
<module>examples/flink-sql-runner-example</module>
+ <module>examples/kubernetes-client-examples</module>
</modules>
<properties>