/** * Scale up/down the number of pod in Deployment * * @param deploymentName * @param numberOfReplicas * @throws ApiException */ public static void scaleDeployment(String deploymentName, int numberOfReplicas) throws ApiException { AppsV1Api appsV1Api = new AppsV1Api(); appsV1Api.setApiClient(COREV1_API.getApiClient()); // 获取V1DeploymentList V1DeploymentList listNamespacedDeployment = appsV1Api.listNamespacedDeployment( DEFAULT_NAME_SPACE, null, null, null, null, null, null, null, null, Boolean.FALSE); List<V1Deployment> appsV1DeploymentItems = listNamespacedDeployment.getItems(); Optional<V1Deployment> findedDeployment = appsV1DeploymentItems.stream() .filter( (V1Deployment deployment) -> deployment.getMetadata().getName().equals(deploymentName)) .findFirst(); findedDeployment.ifPresent( (V1Deployment deploy) -> { try { V1DeploymentSpec newSpec = deploy.getSpec().replicas(numberOfReplicas); V1Deployment newDeploy = deploy.spec(newSpec); appsV1Api.replaceNamespacedDeployment( deploymentName, DEFAULT_NAME_SPACE, newDeploy, null, null, null); } catch (ApiException ex) { LOGGER.warn("Scale the pod failed for Deployment:" + deploymentName, ex); } }); }