我使用 io.fabric8.kubernetes-client,版本 3.1.8 来执行 kubernetes 资源的 RollingUpdate。部署很好。但是我遇到了 StatefulSet 的例外情况。但是,如果我对 StatefulSet 使用“kubectl apply -f ***.yaml”也没问题。
滚动更新部署的代码:
public void createOrReplaceResourceByYaml(String namespace, KubernetesResource resource) {
KubernetesClient client = k8sRestClient.newKubeClient();
Deployment deployment = (Deployment) resource;
logger.info(String.format("Create/Replace Deployment [%s] in namespace [%s].", ((Deployment) resource).getMetadata().getName(), namespace));
NonNamespaceOperation<Deployment, DeploymentList, DoneableDeployment, ScalableResource<Deployment, DoneableDeployment>> deployments = client.extensions().deployments().inNamespace(namespace);
Deployment result = deployments.createOrReplace(deployment);
logger.info(String.format("Created/Replaced Deployment [%s].", result.getMetadata().getName()));
}
滚动更新 StatefulSet 的代码
public void createOrReplaceResourceByYaml(String namespace, KubernetesResource resource) {
KubernetesClient client = k8sRestClient.newKubeClient();
StatefulSet statefulSet = (StatefulSet) resource;
logger.info(String.format("Create/Replace StatefulSet [%s] in namespace [%s].", statefulSet.getMetadata().getName(), namespace));
NonNamespaceOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> statefulSets = client.apps().statefulSets().inNamespace(namespace);
StatefulSet result = statefulSets.createOrReplace(statefulSet);
logger.info(String.format("Created/Replaced StatefulSet [%s].", result.getMetadata().getName()));
}
StatefulSet 的 RollingUpdate 时出现异常
执行失败:PUT 位于: https://kubernetes.default.svc/apis/apps/v1beta1/namespaces/itsma1/statefulsets/pro-rabbitmq 。消息:StatefulSet.apps“pro-rabbitmq”无效:规范:禁止:禁止更新“副本”、“模板”和“updateStrategy”以外的字段的状态集规范。接收状态:状态(apiVersion=v1, code=422, details=StatusDetails(causes=[StatusCause(field=spec, message=Forbidden: 禁止更新“replicas”、“template”和“updateStrategy”以外的字段的 statefulset spec。, reason=FieldValueForbidden, additionalProperties ={})], group=apps, kind=StatefulSet, name=pro-rabbitmq, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=StatefulSet.apps “pro-rabbitmq” 无效: spec: Forbidden: 禁止更新“replicas”、“template”和“updateStrategy”以外字段的 statefulset spec。, metadata=ListMeta(resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid , status=Failure, additionalProperties={}).
我很好奇为什么会发生错误以及如何解决它。
原文由 Cain 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以试试这个来更新 StatefulSet
client.apps().statefulSets().withName("repl1").rolling().withTimeout(5, TimeUnit.MINUTES).updateImage("");
如果你只想缩放,你可以试试这个
client.apps().statefulSets().withName("repl1").scale(5, true);