K8S Fabric8 API +一次性部署,吊舱和服务?

时间:2019-11-16 00:57:50

标签: kubernetes fabric8

在K8S Fabric8 API中,是否有示例或示例代码可一次性创建部署,pod和服务?

请帮助!

1 个答案:

答案 0 :(得分:0)

您可以创建import numpy as np def iterative_sampler(sample_space, p=None): """ Samples elements from a sample space (a list) with a given probability distribution p (numPy array) without replacement. If called until StopIteration is raised, effectively produces a permutation of the sample space. """ if p is None: p = np.array([1/len(sample_space) for _ in sample_space]) try: assert isinstance(sample_space, list) assert isinstance(p, np.ndarray) except AssertionError: raise TypeError("Required types: \nsample_space: list \np type: np.ndarray") # Main loop n = len(sample_space) idxs_left = list(range(n)) for i in range(n): idx = np.random.choice( range(n-i), p= p[idxs_left] / p[idxs_left].sum() ) yield sample_space[idxs_left[idx]] del idxs_left[idx] 并使用Deployment公开它,如下所示:

Service

创建后,您可以检查创建的资源:

try (KubernetesClient client = new DefaultKubernetesClient()) {
    // Create a simple Nginx Deployment
    Deployment deployment = new DeploymentBuilder()
            .withNewMetadata().withName("nginx-deployment").addToLabels("app", "nginx").endMetadata()
            .withNewSpec()
            .withReplicas(3)
            .withNewSelector()
            .withMatchLabels(Collections.singletonMap("app", "nginx"))
            .endSelector()
            .withNewTemplate()
            .withNewMetadata().addToLabels("app", "nginx").endMetadata()
            .withNewSpec()
            .addNewContainer()
            .withName("nginx")
            .withImage("nginx:1.7.9")
            .addNewPort().withContainerPort(80).endPort()
            .endContainer()
            .endSpec()
            .endTemplate()
            .endSpec()
            .build();
    client.apps().deployments().inNamespace("default").create(deployment);

    // Expose the above created Deployment using a NodePort service
    Service service = new ServiceBuilder()
            .withNewMetadata().withName("nginx-svc").addToLabels("app", "nginx").endMetadata()
            .withNewSpec()
            .addNewPort()
            .withName("http")
            .withPort(80)
            .withTargetPort(new IntOrString(80))
            .endPort()
            .withSelector(Collections.singletonMap("app", "nginx"))
            .withType("NodePort")
            .endSpec()
            .build();
    client.services().inNamespace("default").create(service);
}

所有Pod就绪后,您可以通过~ : $ kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 3m33s ~ : $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4m31s nginx-svc NodePort 10.96.166.83 <none> 80:32174/TCP 3m35s ~ : $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-54f57cf6bf-dkh67 1/1 Running 0 4m48s nginx-deployment-54f57cf6bf-dx2ql 1/1 Running 0 4m48s nginx-deployment-54f57cf6bf-mwgkk 1/1 Running 0 4m48s ~ : $ minikube ip 192.168.39.39 (在我们的情况下为$MINIKUBE_IP:$NODEPORTenter image description here

访问您的应用程序