All Products
Search
Document Center

Java SDK examples

Last Updated: Aug 15, 2019

Java edition:

1. Install the Java SDK.

Java SDK address on Maven:https://search.maven.org/search?q=a:aliyun-java-sdk-eci

Add the following dependency to the project POM:

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-eci</artifactId>
  4. <version>1.0.5</version>
  5. </dependency>

2. Example

Public
  1. import com.alibaba.fastjson.JSON;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.aliyuncs.AcsRequest;
  4. import com.aliyuncs.AcsResponse;
  5. import com.aliyuncs.DefaultAcsClient;
  6. import com.aliyuncs.IAcsClient;
  7. import com.aliyuncs.RpcAcsRequest;
  8. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupFromTemplateRequest;
  9. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupFromTemplateResponse;
  10. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.*;
  11. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest;
  12. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.DnsConfig.*;
  13. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.Volume.*;
  14. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupRequest.Container.*;
  15. import com.aliyuncs.eci.model.v20180808.CreateContainerGroupResponse;
  16. import com.aliyuncs.eci.model.v20180808.DeleteContainerGroupRequest;
  17. import com.aliyuncs.eci.model.v20180808.DeleteContainerGroupResponse;
  18. import com.aliyuncs.eci.model.v20180808.DescribeContainerGroupPriceRequest;
  19. import com.aliyuncs.eci.model.v20180808.DescribeContainerGroupsRequest;
  20. import com.aliyuncs.eci.model.v20180808.DescribeContainerGroupsResponse;
  21. import com.aliyuncs.eci.model.v20180808.ExecContainerCommandRequest;
  22. import com.aliyuncs.eci.model.v20180808.UpdateContainerGroupRequest;
  23. import com.aliyuncs.eci.model.v20180808.UpdateContainerGroupResponse;
  24. import com.aliyuncs.exceptions.ClientException;
  25. import com.aliyuncs.http.FormatType;
  26. import com.aliyuncs.http.HttpResponse;
  27. import com.aliyuncs.profile.DefaultProfile;
  28. import com.aliyuncs.profile.IClientProfile;
  29. import com.aliyuncs.regions.ProductDomain;
  30. import java.util.Arrays;
  31. import java.util.Collections;
  32. public class EciTest20180808 {
  33. /**
  34. * Default request parameters
  35. *
  36. * @param request RpcAcsRequest
  37. */
  38. private static void fillParam(RpcAcsRequest request) {
  39. request.setAcceptFormat(FormatType.JSON);
  40. request.setVersion(EciTestConstant.VERSION);
  41. ProductDomain productDomain = new ProductDomain(EciTestConstant.PRODUCT, EciTestConstant.PRODUCT_DOMAIN);
  42. request.setProductDomain(productDomain);
  43. }
  44. /**
  45. * IAcsClient send the request
  46. * aliyun-sdk-core version must be 4.2.5.
  47. * @param request
  48. * @param <T>
  49. * @return AcsResponse returns the AcsResponse object corresponding to the request.
  50. */
  51. private static <T> T doAction(AcsRequest<T> request) {
  52. DefaultAcsClient client = null;
  53. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  54. EciTestConstant.AK_SECRET);
  55. try {
  56. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  57. EciTestConstant.PRODUCT_DOMAIN);
  58. } catch (ClientException e) {
  59. e.printStackTrace();
  60. }
  61. client = new DefaultAcsClient(iClientProfile);
  62. AcsResponse response = null;
  63. try {
  64. response = client.getAcsResponse(request);
  65. } catch (ClientException e) {
  66. e.printStackTrace();
  67. }
  68. return (T)response;
  69. }
  70. /**
  71. * IAcsClient send the request
  72. * @param request
  73. * @return returns in JSON format.
  74. */
  75. private static HttpResponse doAction2(RpcAcsRequest request) {
  76. IAcsClient iAcsClient = null;
  77. IClientProfile iClientProfile = DefaultProfile.getProfile(EciTestConstant.REGION_ID, EciTestConstant.AK_ID,
  78. EciTestConstant.AK_SECRET);
  79. try {
  80. DefaultProfile.addEndpoint("cn-hangzhou", EciTestConstant.REGION_ID, EciTestConstant.PRODUCT,
  81. EciTestConstant.PRODUCT_DOMAIN);
  82. } catch (ClientException e) {
  83. e.printStackTrace();
  84. }
  85. iAcsClient = new DefaultAcsClient(iClientProfile);
  86. HttpResponse response = null;
  87. try {
  88. response = iAcsClient.doAction(request);
  89. System.out.println("responseContent:" + new String(response.getHttpContent()));
  90. } catch (ClientException e) {
  91. e.printStackTrace();
  92. }
  93. return response;
  94. }
  95. }
CreateContainerGroup
  1. public static CreateContainerGroupResponse createContainerGroup() {
  2. CreateContainerGroupRequest createContainerGroupRequest = new CreateContainerGroupRequest();
  3. fillParam(createContainerGroupRequest);
  4. createContainerGroupRequest.setActionName("CreateContainerGroup");//Filled by default
  5. createContainerGroupRequest.setRegionId(EciTestConstant.REGION_ID);
  6. createContainerGroupRequest.setSecurityGroupId(EciTestConstant.SECURITY_GROUP_ID);
  7. createContainerGroupRequest.setVSwitchId(EciTestConstant.VSWITCH_ID);
  8. createContainerGroupRequest.setContainerGroupName("liu-test");
  9. //Tag
  10. Tag tag = new Tag();
  11. tag.setKey("java-sdk-eci");
  12. tag.setValue("v1");
  13. /**
  14. * volumes
  15. * volume1: ConfigFileVolume
  16. */
  17. Volume volume1 = new Volume();
  18. volume1.setName("default-volume1-config");
  19. volume1.setType("ConfigFileVolume");
  20. ConfigFileVolumeConfigFileToPath configFileToPath = new ConfigFileVolumeConfigFileToPath();
  21. configFileToPath.setPath("eci/container-config");
  22. configFileToPath.setContent("eci-config");
  23. volume1.setConfigFileVolumeConfigFileToPaths(Collections.singletonList(configFileToPath));
  24. /**
  25. * volumes
  26. * volume2: NFSVolume
  27. */
  28. Volume volume2 = new Volume();
  29. volume2.setType("NFSVolume");
  30. volume2.setName("default-volume2-nfs");
  31. volume2.setNFSVolumePath(EciTestConstant.NAS_PATH);
  32. volume2.setNFSVolumeServer(EciTestConstant.NAS_SERVER);
  33. volume2.setNFSVolumeReadOnly(false);
  34. /**
  35. * volumes
  36. * volume3: EmptyDirVolume
  37. */
  38. Volume volume3 = new Volume();
  39. volume3.setType("EmptyDirVolume");
  40. volume3.setName("default-volume3-empty");
  41. //securityContext
  42. SecurityContext securityContext = new SecurityContext();
  43. securityContext.setReadOnlyRootFilesystem(true);
  44. securityContext.setRunAsUser(1337L);
  45. Capability capability = new Capability();
  46. capability.setAdds(Arrays.asList("NET_ADMIN"));
  47. securityContext.setCapability(capability);
  48. /**
  49. * initContainer busyBox
  50. */
  51. Container initContainer = new Container();
  52. initContainer.setName("centos-liu-init");
  53. initContainer.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  54. initContainer.setMemory(2F);
  55. initContainer.setCpu(1.0F);
  56. initContainer.setImagePullPolicy("Always");
  57. //initContainer.setSecurityContext(securityContext);
  58. createContainerGroupRequest.setInitContainers(Arrays.asList(initContainer));
  59. /**
  60. * container1 share data with container2 through EmptyDirVolume
  61. */
  62. Container container = new Container();
  63. container.setName("nginx-liu");
  64. container.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/nginx:alpine");
  65. container.setMemory(2F);
  66. container.setCpu(1F);
  67. container.setImagePullPolicy("Always");
  68. VolumeMount volumeMount = new VolumeMount();
  69. volumeMount.setName("default-volume3-empty");
  70. volumeMount.setMountPath("/usr/share/nginx/html");
  71. volumeMount.setReadOnly(false);
  72. container.setVolumeMounts(Collections.singletonList(volumeMount));
  73. //probe
  74. ContainerProbe readinessProbe = new ContainerProbe();
  75. readinessProbe.setTcpSocketPort(80);
  76. readinessProbe.setInitialDelaySeconds(5);
  77. readinessProbe.setPeriodSeconds(3);
  78. readinessProbe.setFailureThreshold(3);
  79. readinessProbe.setSuccessThreshold(3);
  80. readinessProbe.setTimeoutSeconds(10);
  81. ContainerProbe livenessProbe = new ContainerProbe();
  82. livenessProbe.setTcpSocketPort(80);
  83. livenessProbe.setInitialDelaySeconds(5);
  84. livenessProbe.setPeriodSeconds(3);
  85. livenessProbe.setFailureThreshold(3);
  86. livenessProbe.setSuccessThreshold(1);
  87. livenessProbe.setTimeoutSeconds(10);
  88. container.setReadinessProbe(readinessProbe);
  89. container.setLivenessProbe(livenessProbe);
  90. //container.setSecurityContext(securityContext);
  91. /**
  92. * container 2 centOS mount both NFSVolume(NAS) and EmptyDirVolume
  93. */
  94. Container container2 = new Container();
  95. container2.setName("centos-liu");
  96. container2.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  97. container2.setMemory(2F);
  98. container2.setCpu(1.0F);
  99. container2.setImagePullPolicy("Always");
  100. VolumeMount volumeMount2 = new VolumeMount();
  101. volumeMount2.setName("default-volume2-nfs");
  102. volumeMount2.setMountPath("/pod-data");
  103. volumeMount2.setReadOnly(false);
  104. VolumeMount volumeMount3 = new VolumeMount();
  105. volumeMount3.setName("default-volume3-empty");
  106. volumeMount3.setMountPath("/usr/share");
  107. volumeMount3.setReadOnly(false);
  108. container2.setVolumeMounts(Arrays.asList(volumeMount2, volumeMount3));
  109. //copy the file by shell command in Container2
  110. container2.setCommands(Arrays.asList("/bin/sh"));
  111. container2.setArgs(Arrays.asList("-c", "while true; do cp /pod-data/index.html /usr/share/; sleep 100; done"));
  112. //dns config
  113. DnsConfig dnsConfig = new DnsConfig();
  114. dnsConfig.setNameServers(Arrays.asList("8.8.8.8"));
  115. dnsConfig.setSearches(Arrays.asList("ns.kubelete.local"));
  116. DnsConfigOption option = new DnsConfigOption();
  117. option.setName("name");
  118. option.setValue("value");
  119. dnsConfig.setOptions(Arrays.asList(option));
  120. HostAliase hostAliase = new HostAliase();
  121. hostAliase.setIp("196.128.0.10");
  122. hostAliase.setHostnames(Arrays.asList("test1.com", "test2.com"));
  123. createContainerGroupRequest.setDnsConfig(dnsConfig);
  124. createContainerGroupRequest.setHostAliases(Arrays.asList(hostAliase));
  125. createContainerGroupRequest.setTags(Collections.singletonList(tag));
  126. createContainerGroupRequest.setVolumes(Arrays.asList(volume1, volume2, volume3));
  127. createContainerGroupRequest.setContainers(Arrays.asList(container, container2));
  128. return doAction(createContainerGroupRequest);
  129. }
UpdateContainerGroup
  1. public static UpdateContainerGroupResponse updateContainerGroup(String containerGroupId) {
  2. UpdateContainerGroupRequest updateContainerGroupRequest = new UpdateContainerGroupRequest();
  3. fillParam(updateContainerGroupRequest);
  4. updateContainerGroupRequest.setActionName("UpdateContainerGroup");//Filled by default
  5. updateContainerGroupRequest.setRegionId(EciTestConstant.REGION_ID);
  6. updateContainerGroupRequest.setContainerGroupId(containerGroupId);
  7. //Tag
  8. Tag tag = new Tag();
  9. tag.setKey("java-sdk-eci");
  10. tag.setValue("v2");
  11. /**
  12. * volumes
  13. * volume1: ConfigFileVolume
  14. */
  15. Volume volume1 = new Volume();
  16. volume1.setName("default-volume1-config");
  17. volume1.setType("ConfigFileVolume");
  18. ConfigFileVolumeConfigFileToPath configFileToPath = new ConfigFileVolumeConfigFileToPath();
  19. configFileToPath.setPath("eci/container-config");
  20. configFileToPath.setContent("eci-config");
  21. volume1.setConfigFileVolumeConfigFileToPaths(Collections.singletonList(configFileToPath));
  22. /**
  23. * volumes
  24. * volume2: NFSVolume
  25. */
  26. Volume volume2 = new Volume();
  27. volume2.setType("NFSVolume");
  28. volume2.setName("default-volume2-nfs");
  29. volume2.setNFSVolumePath(EciTestConstant.NAS_PATH);
  30. volume2.setNFSVolumeServer(EciTestConstant.NAS_SERVER);
  31. volume2.setNFSVolumeReadOnly(false);
  32. /**
  33. * volumes
  34. * volume3: EmptyDirVolume
  35. */
  36. Volume volume3 = new Volume();
  37. volume3.setType("EmptyDirVolume");
  38. volume3.setName("default-volume3-empty");
  39. //securityContext
  40. SecurityContext securityContext = new SecurityContext();
  41. securityContext.setReadOnlyRootFilesystem(true);
  42. securityContext.setRunAsUser(1337L);
  43. Capability capability = new Capability();
  44. capability.setAdds(Arrays.asList("NET_ADMIN"));
  45. securityContext.setCapability(capability);
  46. /**
  47. * initContainer busyBox
  48. */
  49. Container initContainer = new Container();
  50. initContainer.setName("centos-liu-init");
  51. initContainer.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  52. initContainer.setMemory(2F);
  53. initContainer.setCpu(1.0F);
  54. initContainer.setImagePullPolicy("Always");
  55. //initContainer.setSecurityContext(securityContext);
  56. updateContainerGroupRequest.setInitContainers(Arrays.asList(initContainer));
  57. /**
  58. * container1 share data with container2 through EmptyDirVolume
  59. */
  60. Container container = new Container();
  61. container.setName("nginx-liu");
  62. container.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/nginx:alpine");
  63. container.setMemory(1F);
  64. container.setCpu(0.5F);
  65. container.setImagePullPolicy("Always");
  66. VolumeMount volumeMount = new VolumeMount();
  67. volumeMount.setName("default-volume3-empty");
  68. volumeMount.setMountPath("/usr/share/nginx/html");
  69. volumeMount.setReadOnly(false);
  70. container.setVolumeMounts(Collections.singletonList(volumeMount));
  71. //probe
  72. ContainerProbe readinessProbe = new ContainerProbe();
  73. readinessProbe.setTcpSocketPort(80);
  74. readinessProbe.setInitialDelaySeconds(5);
  75. readinessProbe.setPeriodSeconds(3);
  76. readinessProbe.setFailureThreshold(3);
  77. readinessProbe.setSuccessThreshold(3);
  78. readinessProbe.setTimeoutSeconds(10);
  79. ContainerProbe livenessProbe = new ContainerProbe();
  80. livenessProbe.setTcpSocketPort(80);
  81. livenessProbe.setInitialDelaySeconds(5);
  82. livenessProbe.setPeriodSeconds(3);
  83. livenessProbe.setFailureThreshold(3);
  84. livenessProbe.setSuccessThreshold(1);
  85. livenessProbe.setTimeoutSeconds(10);
  86. container.setReadinessProbe(readinessProbe);
  87. container.setLivenessProbe(livenessProbe);
  88. //container.setSecurityContext(securityContext);
  89. /**
  90. * container 2 centOS mount both NFSVolume(NAS) and EmptyDirVolume
  91. */
  92. Container container2 = new Container();
  93. container2.setName("centos-liu");
  94. container2.setImage("registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0");
  95. container2.setMemory(1F);
  96. container2.setCpu(0.5F);
  97. container2.setImagePullPolicy("Always");
  98. VolumeMount volumeMount2 = new VolumeMount();
  99. volumeMount2.setName("default-volume2-nfs");
  100. volumeMount2.setMountPath("/pod-data");
  101. volumeMount2.setReadOnly(false);
  102. VolumeMount volumeMount3 = new VolumeMount();
  103. volumeMount3.setName("default-volume3-empty");
  104. volumeMount3.setMountPath("/usr/share");
  105. volumeMount3.setReadOnly(false);
  106. container2.setVolumeMounts(Arrays.asList(volumeMount2, volumeMount3));
  107. //copy the file by shell command in Container2
  108. container2.setCommands(Arrays.asList("/bin/sh"));
  109. container2.setArgs(Arrays.asList("-c", "while true; do cp /pod-data/index.html /usr/share/; sleep 100; done"));
  110. //dns config
  111. DnsConfig dnsConfig = new DnsConfig();
  112. dnsConfig.setNameServers(Arrays.asList("8.8.8.8"));
  113. dnsConfig.setSearches(Arrays.asList("ns.kubelete.local"));
  114. DnsConfigOption option = new DnsConfigOption();
  115. option.setName("name");
  116. option.setValue("value");
  117. dnsConfig.setOptions(Arrays.asList(option));
  118. updateContainerGroupRequest.setDnsConfig(dnsConfig);
  119. updateContainerGroupRequest.setTags(Collections.singletonList(tag));
  120. updateContainerGroupRequest.setVolumes(Arrays.asList(volume1, volume2, volume3));
  121. updateContainerGroupRequest.setContainers(Arrays.asList(container, container2));
  122. return doAction(updateContainerGroupRequest);
  123. }
DescribeContainerGroups
  1. public static DescribeContainerGroupsResponse describeContainerGroups(String[] containerGroupIds) {
  2. DescribeContainerGroupsRequest describeContainerGroupsRequest = new DescribeContainerGroupsRequest();
  3. fillParam(describeContainerGroupsRequest);
  4. describeContainerGroupsRequest.setActionName("DescribeContainerGroups");//Filled by default
  5. describeContainerGroupsRequest.setRegionId(EciTestConstant.REGION_ID);
  6. describeContainerGroupsRequest.setStatus("Running");
  7. describeContainerGroupsRequest.setContainerGroupIds(JSONObject.toJSONString(containerGroupIds));
  8. return doAction(describeContainerGroupsRequest);
  9. }
ExecContainerCommand
  1. public static HttpResponse execContainerCommand(String regionId, String containerGroupId, String containerName, String coomand){
  2. ExecContainerCommandRequest execContainerCommandRequest = new ExecContainerCommandRequest();
  3. execContainerCommandRequest.setRegionId(regionId);
  4. execContainerCommandRequest.setContainerGroupId(containerGroupId);
  5. execContainerCommandRequest.setContainerName(containerName);
  6. execContainerCommandRequest.setCommand(coomand);
  7. return doAction2(execContainerCommandRequest);
  8. }
DescribeContainerGroupPrice
  1. public static HttpResponse describeContainerGroupPrice(String regionId, Float cpu, Float mem){
  2. DescribeContainerGroupPriceRequest describeContainerGroupPriceRequest = new DescribeContainerGroupPriceRequest();
  3. describeContainerGroupPriceRequest.setRegionId(regionId);
  4. describeContainerGroupPriceRequest.setCpu(cpu);
  5. describeContainerGroupPriceRequest.setMemory(mem);
  6. return doAction2(describeContainerGroupPriceRequest);
  7. }
DeleteContainerGroup
  1. public static DeleteContainerGroupResponse deleteContainerGroup(String containerGroupId) {
  2. DeleteContainerGroupRequest deleteContainerGroupRequest = new DeleteContainerGroupRequest();
  3. fillParam(deleteContainerGroupRequest);
  4. deleteContainerGroupRequest.setActionName("DeleteContainerGroup");//Filled by default
  5. deleteContainerGroupRequest.setContainerGroupId(containerGroupId);
  6. return doAction(deleteContainerGroupRequest);
  7. }
CreateContainerGroupFromTemplate
  1. public static CreateContainerGroupFromTemplateResponse createfromTemplate() {
  2. CreateContainerGroupFromTemplateRequest createContainerGroupFromTemplateRequest = new CreateContainerGroupFromTemplateRequest();
  3. createContainerGroupFromTemplateRequest.setTemplate("{\"ResourceGroupId\": \"rg-aekzh43vbot2abx\", \"RegionId\": \"cn-hangzhou\", \"ContainerGroupName\": \"template-test\", \"Tags\": [{\"key\": \"template\", \"value\": \"template\"}], \"EipInstanceId\": \"\", \"VSwitchId\": \"vsw-bp1jrgfqqy54kg5hcf8bc\", \"SecurityGroupId\": \"sg-bp118knl07ymorgzfhyw\", \"Spec\": {\"DnsPolicy\": \"ClusterFirst\", \"DnsConfig\": {\"Searches\": [\"c7a1790b3f206498382cbba682c257b0e.svc.cluster.local\", \"svc.cluster.local\", \"cluster.local\"]}, \"hostAliases\": [{\"ip\": \"127.0.0.1\", \"hostnames\": [\"www.test2.com\", \"www.test.com\"]}, {\"ip\": \"127.0.0.1\", \"hostnames\": [\"www.test3.com\"]}], \"RestartPolicy\": \"Always\", \"Volumes\": [{\"EmptyDir\": {\"SizeLimit\": \"1024Mi\"}, \"Name\": \"empty-volume\"}], \"InitContainers\": [{\"Image\": \"registry-vpc.cn-hangzhou.aliyuncs.com/liumi/busybox:1.0\", \"Name\": \"busybox\", \"Resources\": {\"Cpu\": 1.35, \"Memory\": 3.67}, \"ImagePullPolicy\": \"Always\"}], \"Containers\": [{\"restartCount\": 5, \"Name\": \"nginx\", \"Env\": [{\"Name\": \"KUBERNETES_SERVICE_HOST\", \"Value\": \"47.xx.32.xx\"}, {\"Name\": \"KUBERNETES_SERVICE_PORT\", \"Value\": \"6443\"}], \"ImagePullPolicy\": \"Always\", \"Image\": \"registry-vpc.cn-hangzhou.aliyuncs.com/liumi/nginx:alpine\", \"VolumeMounts\": [{\"ReadOnly\": false, \"MountPath\": \"/pod/data\", \"SubPath\": \"\", \"Name\": \"empty-volume\"}], \"Ports\": [{\"Protocol\": \"TCP\", \"ContainerPort\": 8080}, {\"Protocol\": \"TCP\", \"ContainerPort\": 80}], \"Resources\": {\"Cpu\": 1.35, \"Memory\": 3.67}, \"securityContextPrivileged\": true}]}, \"Resources\": {\"Cpu\": 2, \"Memory\": 4}, \"ZoneId\": \"cn-hangzhou-h\"}");
  4. return doAction(createContainerGroupFromTemplateRequest);
  5. }
ExportContainerGroupTemplate
  1. private static ExportContainerGroupTemplateResponse exportContainerGroupTemplate(String regionId, String containerGroupId) {
  2. ExportContainerGroupTemplateRequest exportContainerGroupTemplateRequest = new ExportContainerGroupTemplateRequest();
  3. exportContainerGroupTemplateRequest.setRegionId(regionId);
  4. exportContainerGroupTemplateRequest.setContainerGroupId(containerGroupId);
  5. return doAction(exportContainerGroupTemplateRequest);
  6. }