基于k8s的Flink的application部署模式

具体代码如下:

import java.util.Collections;
import java.util.concurrent.Executors;

import org.apache.flink.client.deployment.ClusterDeploymentException;
import org.apache.flink.client.deployment.ClusterSpecification;
import org.apache.flink.client.deployment.application.ApplicationConfiguration;
import org.apache.flink.client.program.ClusterClient;
import org.apache.flink.client.program.ClusterClientProvider;
import org.apache.flink.configuration.CheckpointingOptions;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.configuration.JobManagerOptions;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.configuration.TaskManagerOptions;
import org.apache.flink.kubernetes.KubernetesClusterDescriptor;
import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
import org.apache.flink.kubernetes.configuration.KubernetesDeploymentTarget;
import org.apache.flink.kubernetes.kubeclient.Fabric8FlinkKubeClient;
import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;

public class KubernetesApplicationDeployment {
	public static void main(String[] args) {
		String flinkDistJar = "hdfs://data/flink/libs/flink-kubernetes_2.12-1.11.0.jar";
		Configuration flinkConfiguration = GlobalConfiguration.loadConfiguration();
		flinkConfiguration.set(DeploymentOptions.TARGET, KubernetesDeploymentTarget.APPLICATION.getName());
		flinkConfiguration.set(CheckpointingOptions.INCREMENTAL_CHECKPOINTS, true);
		flinkConfiguration.set(PipelineOptions.JARS, Collections.singletonList(flinkDistJar));
		flinkConfiguration.set(KubernetesConfigOptions.CLUSTER_ID, "k8s-app1");
		flinkConfiguration.set(KubernetesConfigOptions.CONTAINER_IMAGE, "img_url");
		flinkConfiguration.set(KubernetesConfigOptions.CONTAINER_IMAGE_PULL_POLICY, 
				KubernetesConfigOptions.ImagePullPolicy.Always);
		flinkConfiguration.set(KubernetesConfigOptions.JOB_MANAGER_CPU, 2.5);
		flinkConfiguration.set(KubernetesConfigOptions.TASK_MANAGER_CPU, 1.5);
		flinkConfiguration.set(JobManagerOptions.TOTAL_FLINK_MEMORY, MemorySize.parse("4096M"));
		flinkConfiguration.set(JobManagerOptions.TOTAL_PROCESS_MEMORY, MemorySize.parse("4096M"));
		flinkConfiguration.set(TaskManagerOptions.NUM_TASK_SLOTS, 1);
		
		KubernetesClient kubernetesClient = new DefaultKubernetesClient();
		FlinkKubeClient flinkKubeClient = new Fabric8FlinkKubeClient(flinkConfiguration, 
				kubernetesClient, () -> Executors.newFixedThreadPool(2));
		KubernetesClusterDescriptor kubernetesClusterDescriptor = new 
				KubernetesClusterDescriptor(flinkConfiguration, flinkKubeClient);
		
		ClusterSpecification clusterSpecification = new ClusterSpecification
				.ClusterSpecificationBuilder().createClusterSpecification();
		ApplicationConfiguration appConfig = new ApplicationConfiguration(args, null);
		
		ClusterClientProvider<String> clusterClientProvider = null;
		try {
			clusterClientProvider = kubernetesClusterDescriptor.deployApplicationCluster(
					clusterSpecification, appConfig);
		} catch (ClusterDeploymentException e) {
			e.printStackTrace();
		}
		kubernetesClusterDescriptor.close();
		ClusterClient<String> clusterClient = clusterClientProvider.getClusterClient();
		String clusterId = clusterClient.getClusterId();
		System.out.println(clusterId);
	}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

画蛇添足

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值