Created
January 3, 2018 14:16
-
-
Save gokart23/f3ed3667ea9321876d4178240db38503 to your computer and use it in GitHub Desktop.
Remove nodes from a Kubernetes deployment programatically, in order to potentially use the deployment as a pool of ready nodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.security.Key; | |
import java.security.KeyStore; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.UnrecoverableKeyException; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import org.apache.commons.codec.binary.Base64; | |
import io.fabric8.kubernetes.api.model.Pod; | |
import io.fabric8.kubernetes.api.model.PodList; | |
import io.fabric8.kubernetes.client.ConfigBuilder; | |
import io.fabric8.kubernetes.client.DefaultKubernetesClient; | |
import io.fabric8.kubernetes.client.KubernetesClient; | |
public class KubeConnect { | |
public static final String K8S_MASTER = "https://kubernetes.master.url/"; | |
public static final String K8S_NAMESPACE = "some-namespace"; | |
public static final String K8S_DEPLOYMENT_SELECTOR_LABEL = "app"; | |
public static final String K8S_DEPLOYMENT_SELECTOR_VALUE = "nginx"; | |
public static final String K8S_PKCS12_CERTIFICATE_LOCATION = "/location/of/certificate.pfx"; | |
private static String pemEncodeKey(Key key) { | |
return Base64.encodeBase64String(new StringBuilder() // | |
.append("-----BEGIN PRIVATE KEY-----\n") // | |
.append(Base64.encodeBase64String(key.getEncoded())) // | |
.append("\n-----END PRIVATE KEY-----\n") // | |
.toString().getBytes(StandardCharsets.UTF_8)); | |
} | |
public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException { | |
char[] password = "".toCharArray(); | |
java.io.FileInputStream fis = null; | |
KeyStore ks = KeyStore.getInstance("PKCS12"); | |
try { | |
fis = new java.io.FileInputStream(KubeConnect.K8S_PKCS12_CERTIFICATE_LOCATION); | |
ks.load(fis, password); | |
} finally { | |
if (fis != null) { | |
fis.close(); | |
} | |
} | |
String alias = ks.aliases().nextElement(); | |
X509Certificate certificate = (X509Certificate) ks.getCertificate(alias); | |
Key key = ks.getKey(alias, password); | |
ConfigBuilder builder = new ConfigBuilder(); | |
builder = builder.withMasterUrl(KubeConnect.K8S_MASTER); | |
builder = builder.withNamespace(KubeConnect.K8S_NAMESPACE); | |
builder = builder.withClientCertData(Base64.encodeBase64String(certificate.getEncoded())) | |
.withClientKeyData(pemEncodeKey(key)) | |
.withClientKeyPassphrase(password.toString()); | |
KubernetesClient kube = new DefaultKubernetesClient(builder.build()); | |
System.out.println("Currently have these pods in the deployment\n"); | |
PodList pods = kube.pods().withLabel(KubeConnect.K8S_DEPLOYMENT_SELECTOR_LABEL, KubeConnect.K8S_DEPLOYMENT_SELECTOR_VALUE).list(); | |
for (Pod pod : pods.getItems()) { | |
System.out.println( pod.getMetadata().getName() + ": " + pod.getStatus().getPodIP() ); | |
} | |
// Select a pod to remove from deployment | |
Pod selected_pod = pods.getItems().get(0); | |
System.out.println("Removing this pod from deployment control: " + selected_pod.getMetadata().getName() ); | |
kube.pods().withName(selected_pod.getMetadata().getName()).edit().editMetadata().removeFromLabels("app").endMetadata().done(); | |
System.out.println("Removed"); | |
kube.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
<groupId>kubeconnect</groupId> | |
<artifactId>kubeconnect</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<build> | |
<sourceDirectory>src</sourceDirectory> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.7.0</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>io.fabric8</groupId> | |
<artifactId>kubernetes-api</artifactId> | |
<version>2.2.101</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-codec</groupId> | |
<artifactId>commons-codec</artifactId> | |
<version>1.4</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment