在kubernetes StatefulSet中顺序创建Pod

时间:2019-01-09 11:39:29

标签: deployment kubernetes sequence statefulset kubernetes-statefulset

我在部署应用程序时遇到了困难,该应用程序需要了解有关特定状态集的集群内部状态并相应地执行操作。 因此,在我的deployment.yml文件中,添加了

spec:
 serviceName: "my-service"
  replicas: 3

哪个kubernetes会立即创建3个Pod(它不会等到容器内的应用程序启动),这在我的情况下是不需要的。 因此,我在应用程序中有一个逻辑,例如,如果它是第一个副本,则执行逻辑1,如果它的第二个副本(或除第一个副本之外的其他任何东西)在有状态集中拾取生成的pod的最后一个ip,然后连接到它。

下面是我的代码

    String containerRingLastNodeIP = null;
    ApiClient client = null;
    Configuration.setDefaultApiClient( client );

    V1PodList list = null;
    try
    {
        client = Config.defaultClient();
    }
    catch ( IOException e )
    {
        SystemLogger.logSystemErrorMessege( e );
    }
    CoreV1Api api = new CoreV1Api( client );
    try
    {
        list = api
                .listNamespacedPod( "mynamespace", null, null, null, null, null, null, null,
                        null, null );
    }
    catch ( ApiException e )
    {
        SystemLogger.logSystemErrorMessege( e );
    }

    if ( list == null )
    {
        SystemLogger.logSystemInfoMessege( "PODS list is empty" );
    }

    List<V1Pod> containersList = list.getItems().stream()
            .filter( x -> x.getMetadata().getName().contains( CONTAINER_NAME_TAG ) ).sorted( ( o1, o2 ) -> {
                int pod1Id = Integer.parseInt( o1.getMetadata().getName().split( "-" )[1] );
                int pod2Id = Integer.parseInt( o2.getMetadata().getName().split( "-" )[1] );
                return pod1Id > pod2Id ? 1 : ( pod1Id < pod2Id ? -1 : 0 );
            } ).collect(
                    Collectors.toList() );

    String[] containerArguments;
    if ( containersList.size() == 1 )
    {
        //do logic 1
    }
    else
    {
        V1Pod recentlyInitializedContainer = containersList
                .get( containersList.size() - 2 );//remove the existing pod from the list
        containerRingLastNodeIP = recentlyInitializedContainer.getStatus().getPodIP();
        //do logic 2
    }

因此,由于kubernetes不要等到我的代码在pod中运行并且它仅启动pod时,我的逻辑才会失败,并且无法按预期工作。

我目前所看到的唯一方法是将副本= 1并手动放大,这我认为不好。 扩展部署时间并不会带来相同的问题。有什么想法可以通过kubernetes完成吗?

0 个答案:

没有答案