How to update/replace deployment of Kubernetes in JAVA

7/19/2018

I use ConfigMap as Volume in the deployment. I'm trying to update a deployment with different Volume and VolumeMount.(see example below)

I tried all the following ways:

(I use io.fabric8.kubernetes.api to do the operations on kubernetes.)

1)

client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).replace(deployment);

2)

client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).createOrReplace(deployment);

3)

client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).cascading(false).replace(deployment);

4)

client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).edit().editSpec().editTemplate().editSped().removeAllFromVolumes(volumes).endSpec().endTemplate().endTemplate().endSpec().done();

5)

client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).edit().editSpec().editTemplate().editSped().removeFromVolumes(volume).endSpec().endTemplate().endTemplate().endSpec().done();

All the above commands don't work!...

I also tried :

client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).edit().editSpec().editTemplate().editSped().AddToVolumes(volume).endSpec().endTemplate().endTemplate().endSpec().done();

and it works.

i.e. only ADD works, but REMOVE doesn't work....

Is it a BUG or not ? Did I use it in the right way? I just want to update the deployment with different Volume (ConfigMap type) and VolumeMount, how should I do? Thank you!

Example: The old Deployment is like this: (I only show the main part)

kind: Deployment
metadata:
  name: d2
  namespace: n1
  spec:
    replicas: 1
    template:
      metadata:
        creationTimestamp: null
      spec:
        containers:
        - image: x.x.x.x
          imagePullPolicy: IfNotPresent
          name: c2
          volumeMounts:
          - mountPath: /var
            name: configmapvolume1
          - mountPath: /var/a
            name: configmapvolume2
        volumes:
        - configMap:
            defaultMode: 420
            items:
            - key: a.yml
              path: a.yml
            name: a
          name: configmapvolume1
        - configMap:
            defaultMode: 420
            items:
            - key: c.yml
              path: c.yml
            name: a
          name: configmapvolume2

The new Deployment is like this:(I only show the main part)

kind: Deployment
metadata:
  name: d2
  namespace: n1
  spec:
    replicas: 1
    template:
      metadata:
        creationTimestamp: null
      spec:
        containers:
        - image: x.x.x.x
          imagePullPolicy: IfNotPresent
          name: c2
          volumeMounts:
          - mountPath: /var/b
            name: configmapvolume1
        volumes:
        - configMap:
            defaultMode: 420
            items:
            - key: b.yml
              path: b.yml
            name: b
          name: configmapvolume1
-- Sha Huang
deployment
java
kubernetes
updates

2 Answers

7/27/2018

I’ve found the reason. There was a parameter named "rollbackRevision" and it was set with value 0 in the deployment (the input of function replace). Because of this parameter, the replace operation always rollback to the previous version. After deleting this parameter (set it to null), the following function works well:
client().extensions().deployments().inNamespace(namespaceName).withName(deploymentName).replace(deployment);

-- Sha Huang
Source: StackOverflow

7/20/2018

If you add --save-config when you create your deployment for the first time, you’ll be able to adjust its specification later:

kubectl create -f deploymentv1.yaml --save-config

To update it with adjusted yaml file run the following command:

kubectl apply -f deploymentv2.yaml

This way new pod will be created from the adjusted specification. The old pod will be terminated then.

I’ll update my answer when I figure out how to apply --save-config option using java code.

-- VAS
Source: StackOverflow