0
# Deployment Operations
1
2
This document covers OpenShift DeploymentConfig operations, including scaling, rollouts, pod management, and deployment strategies.
3
4
## Core Imports
5
6
```java { .api }
7
import io.fabric8.openshift.client.OpenShiftClient;
8
import io.fabric8.openshift.api.model.DeploymentConfig;
9
import io.fabric8.openshift.api.model.DeploymentConfigList;
10
import io.fabric8.openshift.client.dsl.DeployableScalableResource;
11
import io.fabric8.kubernetes.api.model.Pod;
12
import io.fabric8.kubernetes.api.model.PodList;
13
import io.fabric8.kubernetes.client.dsl.MixedOperation;
14
import io.fabric8.kubernetes.client.dsl.RollableScalableResource;
15
```
16
17
## Basic DeploymentConfig Operations
18
19
### Listing and Getting DeploymentConfigs
20
21
```java { .api }
22
// List all deployment configurations in a namespace
23
DeploymentConfigList deploymentConfigs = client.deploymentConfigs()
24
.inNamespace("my-project")
25
.list();
26
27
// Get a specific deployment configuration
28
DeploymentConfig dc = client.deploymentConfigs()
29
.inNamespace("my-project")
30
.withName("my-app")
31
.get();
32
33
// List with label selector
34
DeploymentConfigList appDCs = client.deploymentConfigs()
35
.inNamespace("my-project")
36
.withLabel("app", "my-application")
37
.list();
38
```
39
40
### Creating DeploymentConfigs
41
42
```java { .api }
43
// Create a new deployment configuration
44
DeploymentConfig newDC = new DeploymentConfigBuilder()
45
.withNewMetadata()
46
.withName("my-new-app")
47
.withNamespace("my-project")
48
.addToLabels("app", "my-new-app")
49
.endMetadata()
50
.withNewSpec()
51
.withReplicas(3)
52
.addToSelector("app", "my-new-app")
53
.withNewTemplate()
54
.withNewMetadata()
55
.addToLabels("app", "my-new-app")
56
.endMetadata()
57
.withNewSpec()
58
.addNewContainer()
59
.withName("my-container")
60
.withImage("my-app:latest")
61
.addNewPort()
62
.withContainerPort(8080)
63
.endPort()
64
.endContainer()
65
.endSpec()
66
.endTemplate()
67
.endSpec()
68
.build();
69
70
DeploymentConfig created = client.deploymentConfigs()
71
.inNamespace("my-project")
72
.create(newDC);
73
```
74
75
## Scaling Operations
76
77
### Manual Scaling
78
79
```java { .api }
80
// Scale deployment configuration to specified replicas
81
DeploymentConfig scaled = client.deploymentConfigs()
82
.inNamespace("my-project")
83
.withName("my-app")
84
.scale(5);
85
86
// Scale with edit operation
87
DeploymentConfig scaledEdit = client.deploymentConfigs()
88
.inNamespace("my-project")
89
.withName("my-app")
90
.edit(dc -> new DeploymentConfigBuilder(dc)
91
.editSpec()
92
.withReplicas(3)
93
.endSpec()
94
.build());
95
96
// Get current scale
97
Scale currentScale = client.deploymentConfigs()
98
.inNamespace("my-project")
99
.withName("my-app")
100
.getScale();
101
102
int currentReplicas = currentScale.getSpec().getReplicas();
103
```
104
105
### Autoscaling Integration
106
107
```java { .api }
108
// Create horizontal pod autoscaler for deployment config
109
HorizontalPodAutoscaler hpa = new HorizontalPodAutoscalerBuilder()
110
.withNewMetadata()
111
.withName("my-app-hpa")
112
.withNamespace("my-project")
113
.endMetadata()
114
.withNewSpec()
115
.withNewScaleTargetRef()
116
.withApiVersion("apps.openshift.io/v1")
117
.withKind("DeploymentConfig")
118
.withName("my-app")
119
.endScaleTargetRef()
120
.withMinReplicas(2)
121
.withMaxReplicas(10)
122
.withTargetCPUUtilizationPercentage(70)
123
.endSpec()
124
.build();
125
126
client.autoscaling().v1().horizontalPodAutoscalers()
127
.inNamespace("my-project")
128
.create(hpa);
129
```
130
131
## Rollout Operations
132
133
### Managing Rollouts
134
135
```java { .api }
136
// Trigger a new deployment (rollout)
137
DeploymentConfig rolledOut = client.deploymentConfigs()
138
.inNamespace("my-project")
139
.withName("my-app")
140
.deployLatest();
141
142
// Get rollout status
143
DeploymentConfig dc = client.deploymentConfigs()
144
.inNamespace("my-project")
145
.withName("my-app")
146
.get();
147
148
Integer latestVersion = dc.getStatus().getLatestVersion();
149
List<DeploymentCondition> conditions = dc.getStatus().getConditions();
150
151
// Check if rollout is complete
152
boolean isProgressing = conditions.stream()
153
.anyMatch(condition ->
154
"Progressing".equals(condition.getType()) &&
155
"True".equals(condition.getStatus()));
156
```
157
158
### Rollback Operations
159
160
```java { .api }
161
// Rollback to previous version
162
DeploymentConfig rolledBack = client.deploymentConfigs()
163
.inNamespace("my-project")
164
.withName("my-app")
165
.rollback();
166
167
// Rollback to specific version
168
DeploymentConfig specificRollback = client.deploymentConfigs()
169
.inNamespace("my-project")
170
.withName("my-app")
171
.rollback(3); // rollback to version 3
172
```
173
174
### Pausing and Resuming Rollouts
175
176
```java { .api }
177
// Pause rollouts
178
DeploymentConfig paused = client.deploymentConfigs()
179
.inNamespace("my-project")
180
.withName("my-app")
181
.pause();
182
183
// Resume rollouts
184
DeploymentConfig resumed = client.deploymentConfigs()
185
.inNamespace("my-project")
186
.withName("my-app")
187
.resume();
188
189
// Check if paused
190
boolean isPaused = client.deploymentConfigs()
191
.inNamespace("my-project")
192
.withName("my-app")
193
.get()
194
.getSpec()
195
.getPaused();
196
```
197
198
## Pod Management
199
200
### Getting Pods for DeploymentConfig
201
202
```java { .api }
203
// Get all pods managed by deployment configuration
204
PodList pods = client.deploymentConfigs()
205
.inNamespace("my-project")
206
.withName("my-app")
207
.getPods();
208
209
// Get pods for specific deployment version
210
PodList versionPods = client.pods()
211
.inNamespace("my-project")
212
.withLabel("deployment", "my-app-3") // version 3
213
.list();
214
```
215
216
### Pod Operations
217
218
```java { .api }
219
// Get logs from all pods
220
List<Pod> appPods = client.deploymentConfigs()
221
.inNamespace("my-project")
222
.withName("my-app")
223
.getPods();
224
225
for (Pod pod : appPods) {
226
String podLogs = client.pods()
227
.inNamespace("my-project")
228
.withName(pod.getMetadata().getName())
229
.getLog();
230
System.out.println("Pod " + pod.getMetadata().getName() + " logs:\n" + podLogs);
231
}
232
233
// Execute command in deployment config pods
234
String output = client.pods()
235
.inNamespace("my-project")
236
.withName(appPods.get(0).getMetadata().getName())
237
.exec("ps", "aux");
238
```
239
240
## Monitoring and Watching
241
242
### Watching DeploymentConfig Changes
243
244
```java { .api }
245
// Watch deployment configuration changes
246
client.deploymentConfigs()
247
.inNamespace("my-project")
248
.withName("my-app")
249
.watch(new Watcher<DeploymentConfig>() {
250
@Override
251
public void eventReceived(Action action, DeploymentConfig dc) {
252
System.out.println("DeploymentConfig " + action + ": " +
253
dc.getMetadata().getName() +
254
" (version: " + dc.getStatus().getLatestVersion() + ")");
255
}
256
257
@Override
258
public void onClose(WatcherException cause) {
259
System.out.println("Watch closed: " + cause.getMessage());
260
}
261
});
262
```
263
264
### Waiting for Rollout Completion
265
266
```java { .api }
267
// Wait for rollout to complete
268
client.deploymentConfigs()
269
.inNamespace("my-project")
270
.withName("my-app")
271
.waitUntilCondition(dc -> {
272
List<DeploymentCondition> conditions = dc.getStatus().getConditions();
273
return conditions.stream().anyMatch(condition ->
274
"Progressing".equals(condition.getType()) &&
275
"False".equals(condition.getStatus()) &&
276
"NewReplicationControllerAvailable".equals(condition.getReason()));
277
}, 5, TimeUnit.MINUTES);
278
```
279
280
## Advanced Operations
281
282
### Updating DeploymentConfig Images
283
284
```java { .api }
285
// Update container image
286
DeploymentConfig updated = client.deploymentConfigs()
287
.inNamespace("my-project")
288
.withName("my-app")
289
.rolling()
290
.withTimeout(10, TimeUnit.MINUTES)
291
.updateImage("my-container", "my-app:v2.0");
292
293
// Update multiple images
294
DeploymentConfig multiUpdated = client.deploymentConfigs()
295
.inNamespace("my-project")
296
.withName("my-app")
297
.rolling()
298
.updateImage(Map.of(
299
"app-container", "my-app:v2.0",
300
"sidecar-container", "sidecar:v1.1"
301
));
302
```
303
304
### Environment Variable Management
305
306
```java { .api }
307
// Update environment variables
308
DeploymentConfig envUpdated = client.deploymentConfigs()
309
.inNamespace("my-project")
310
.withName("my-app")
311
.edit(dc -> new DeploymentConfigBuilder(dc)
312
.editSpec()
313
.editTemplate()
314
.editSpec()
315
.editFirstContainer()
316
.addNewEnv()
317
.withName("NEW_ENV_VAR")
318
.withValue("new-value")
319
.endEnv()
320
.endContainer()
321
.endSpec()
322
.endTemplate()
323
.endSpec()
324
.build());
325
```
326
327
### Resource Limits and Requests
328
329
```java { .api }
330
// Update resource requirements
331
DeploymentConfig resourceUpdated = client.deploymentConfigs()
332
.inNamespace("my-project")
333
.withName("my-app")
334
.edit(dc -> new DeploymentConfigBuilder(dc)
335
.editSpec()
336
.editTemplate()
337
.editSpec()
338
.editFirstContainer()
339
.withNewResources()
340
.addToRequests("cpu", new Quantity("100m"))
341
.addToRequests("memory", new Quantity("128Mi"))
342
.addToLimits("cpu", new Quantity("500m"))
343
.addToLimits("memory", new Quantity("512Mi"))
344
.endResources()
345
.endContainer()
346
.endSpec()
347
.endTemplate()
348
.endSpec()
349
.build());
350
```
351
352
## Usage Examples
353
354
### Complete Deployment Lifecycle Example
355
356
```java
357
import io.fabric8.openshift.client.OpenShiftClient;
358
import io.fabric8.openshift.api.model.*;
359
360
public class DeploymentLifecycle {
361
private final OpenShiftClient client;
362
363
public DeploymentLifecycle(OpenShiftClient client) {
364
this.client = client;
365
}
366
367
public void deployApplication(String namespace, String appName, String image) {
368
// 1. Create or update deployment configuration
369
DeploymentConfig dc = createOrUpdateDeploymentConfig(namespace, appName, image);
370
371
// 2. Wait for deployment to complete
372
waitForDeploymentComplete(namespace, appName);
373
374
// 3. Verify deployment health
375
verifyDeploymentHealth(namespace, appName);
376
377
System.out.println("Application " + appName + " deployed successfully");
378
}
379
380
private DeploymentConfig createOrUpdateDeploymentConfig(String namespace,
381
String appName, String image) {
382
DeploymentConfig existing = client.deploymentConfigs()
383
.inNamespace(namespace)
384
.withName(appName)
385
.get();
386
387
if (existing != null) {
388
// Update existing deployment
389
return client.deploymentConfigs()
390
.inNamespace(namespace)
391
.withName(appName)
392
.rolling()
393
.withTimeout(10, TimeUnit.MINUTES)
394
.updateImage(appName, image);
395
} else {
396
// Create new deployment
397
return client.deploymentConfigs()
398
.inNamespace(namespace)
399
.create(new DeploymentConfigBuilder()
400
.withNewMetadata()
401
.withName(appName)
402
.withNamespace(namespace)
403
.addToLabels("app", appName)
404
.endMetadata()
405
.withNewSpec()
406
.withReplicas(3)
407
.addToSelector("app", appName)
408
.withNewStrategy()
409
.withType("Rolling")
410
.withNewRollingParams()
411
.withMaxUnavailable(new IntOrString("25%"))
412
.withMaxSurge(new IntOrString("25%"))
413
.endRollingParams()
414
.endStrategy()
415
.addNewTrigger()
416
.withType("ConfigChange")
417
.endTrigger()
418
.addNewTrigger()
419
.withType("ImageChange")
420
.withNewImageChangeParams()
421
.withAutomatic(true)
422
.addToContainerNames(appName)
423
.withNewFrom()
424
.withKind("ImageStreamTag")
425
.withName(appName + ":latest")
426
.endFrom()
427
.endImageChangeParams()
428
.endTrigger()
429
.withNewTemplate()
430
.withNewMetadata()
431
.addToLabels("app", appName)
432
.endMetadata()
433
.withNewSpec()
434
.addNewContainer()
435
.withName(appName)
436
.withImage(image)
437
.addNewPort()
438
.withContainerPort(8080)
439
.withProtocol("TCP")
440
.endPort()
441
.withNewResources()
442
.addToRequests("cpu", new Quantity("100m"))
443
.addToRequests("memory", new Quantity("128Mi"))
444
.addToLimits("cpu", new Quantity("500m"))
445
.addToLimits("memory", new Quantity("512Mi"))
446
.endResources()
447
.withNewLivenessProbe()
448
.withNewHttpGet()
449
.withPath("/health")
450
.withNewPort(8080)
451
.endHttpGet()
452
.withInitialDelaySeconds(30)
453
.withPeriodSeconds(10)
454
.endLivenessProbe()
455
.withNewReadinessProbe()
456
.withNewHttpGet()
457
.withPath("/ready")
458
.withNewPort(8080)
459
.endHttpGet()
460
.withInitialDelaySeconds(5)
461
.withPeriodSeconds(5)
462
.endReadinessProbe()
463
.endContainer()
464
.endSpec()
465
.endTemplate()
466
.endSpec()
467
.build());
468
}
469
}
470
471
private void waitForDeploymentComplete(String namespace, String appName) {
472
client.deploymentConfigs()
473
.inNamespace(namespace)
474
.withName(appName)
475
.waitUntilCondition(dc -> {
476
Integer latestVersion = dc.getStatus().getLatestVersion();
477
Integer replicas = dc.getSpec().getReplicas();
478
Integer readyReplicas = dc.getStatus().getReadyReplicas();
479
480
return latestVersion != null &&
481
replicas != null &&
482
readyReplicas != null &&
483
replicas.equals(readyReplicas);
484
}, 10, TimeUnit.MINUTES);
485
}
486
487
private void verifyDeploymentHealth(String namespace, String appName) {
488
PodList pods = client.deploymentConfigs()
489
.inNamespace(namespace)
490
.withName(appName)
491
.getPods();
492
493
long readyPods = pods.getItems().stream()
494
.filter(pod -> pod.getStatus().getConditions().stream()
495
.anyMatch(condition ->
496
"Ready".equals(condition.getType()) &&
497
"True".equals(condition.getStatus())))
498
.count();
499
500
DeploymentConfig dc = client.deploymentConfigs()
501
.inNamespace(namespace)
502
.withName(appName)
503
.get();
504
505
int expectedReplicas = dc.getSpec().getReplicas();
506
507
if (readyPods != expectedReplicas) {
508
throw new RuntimeException(
509
"Deployment unhealthy: " + readyPods + "/" + expectedReplicas + " pods ready");
510
}
511
}
512
}
513
```
514
515
## Types
516
517
### DeploymentConfig
518
519
```java { .api }
520
public class DeploymentConfig implements HasMetadata {
521
public ObjectMeta getMetadata();
522
public DeploymentConfigSpec getSpec();
523
public DeploymentConfigStatus getStatus();
524
}
525
526
public class DeploymentConfigSpec {
527
public Integer getReplicas();
528
public Map<String, String> getSelector();
529
public PodTemplateSpec getTemplate();
530
public DeploymentStrategy getStrategy();
531
public List<DeploymentTriggerPolicy> getTriggers();
532
public Integer getRevisionHistoryLimit();
533
public Boolean getPaused();
534
}
535
536
public class DeploymentConfigStatus {
537
public Integer getLatestVersion();
538
public Integer getObservedGeneration();
539
public Integer getReplicas();
540
public Integer getUpdatedReplicas();
541
public Integer getAvailableReplicas();
542
public Integer getUnavailableReplicas();
543
public Integer getReadyReplicas();
544
public List<DeploymentCondition> getConditions();
545
public List<ReplicationControllerCondition> getDetails();
546
}
547
```
548
549
### DeploymentStrategy
550
551
```java { .api }
552
public class DeploymentStrategy {
553
public String getType(); // Rolling, Recreate, Custom
554
public RollingDeploymentStrategyParams getRollingParams();
555
public RecreateDeploymentStrategyParams getRecreateParams();
556
public CustomDeploymentStrategyParams getCustomParams();
557
public ResourceRequirements getResources();
558
public Map<String, String> getLabels();
559
public Map<String, String> getAnnotations();
560
}
561
562
public class RollingDeploymentStrategyParams {
563
public IntOrString getMaxUnavailable();
564
public IntOrString getMaxSurge();
565
public Long getTimeoutSeconds();
566
public Long getIntervalSeconds();
567
public Long getUpdatePeriodSeconds();
568
public LifecycleHook getPre();
569
public LifecycleHook getPost();
570
}
571
```
572
573
### DeployableScalableResource DSL
574
575
```java { .api }
576
public interface DeployableScalableResource<T> extends RollableScalableResource<T> {
577
T deployLatest();
578
T rollback();
579
T rollback(int toRevision);
580
T pause();
581
T resume();
582
PodList getPods();
583
584
// Scaling operations
585
T scale(int count);
586
Scale getScale();
587
588
// Rolling update operations
589
RollingUpdater<T> rolling();
590
}
591
```
592
593
### Rolling Update Operations
594
595
```java { .api }
596
public interface RollingUpdater<T> {
597
RollingUpdater<T> withTimeout(long timeout, TimeUnit timeUnit);
598
T updateImage(String containerName, String image);
599
T updateImage(Map<String, String> containerToImageMap);
600
}
601
```