gpt4 book ai didi

c# - 如何在 C# 中使用 kubernetes-client 创建 k8s 部署?

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:37 25 4
gpt4 key购买 nike

我在这一行收到 Microsoft.Rest.HttpOperationException: 'Operation returned an invalid status code 'BadRequest''。

var result = client.CreateNamespacedDeployment(部署, 命名空间);

Kubernetes-client有少量好的资源,大部分都是用java、python等其他语言写的。所以我指的是这些文档。

这是我到目前为止的实现。

        V1Deployment deployment = new V1Deployment()
{
ApiVersion = "extensions/v1beta1",
Kind = "Deployment",
Metadata = new V1ObjectMeta()
{
Name = "...",
NamespaceProperty = env,
Labels = new Dictionary<string, string>()
{
{ "app", "..." }
}
},
Spec = new V1DeploymentSpec
{
Replicas = 1,
Selector = new V1LabelSelector()
{
MatchLabels = new Dictionary<string, string>
{
{ "app", "..." }
}
},
Template = new V1PodTemplateSpec()
{
Metadata = new V1ObjectMeta()
{
CreationTimestamp = null,
Labels = new Dictionary<string, string>
{
{ "app", "..." }
}
},
Spec = new V1PodSpec
{
Containers = new List<V1Container>()
{
new V1Container()
{
Name = "...",
Image = "...",
ImagePullPolicy = "Always",
Ports = new List<V1ContainerPort> { new V1ContainerPort(80) }
}
}
}
}
},
Status = new V1DeploymentStatus()
{
Replicas = 1
}
};

var result = client.CreateNamespacedDeployment(deployment, namespace);

我想知道如何使用 kubernetes-client 创建 kubernetes 部署的正确方法,我也想知道这个问题的原因。

最佳答案

为了完全清楚和 future 的访问者,值得一提的是,在使用您的代码示例时,从 API 服务器返回的错误请求错误(代码:400)背后究竟是什么:

"the API version in the data (extensions/v1beta1) does not match the expected API version (apps/v1)"

解决方案:

 ApiVersion = "extensions/v1beta1" -> ApiVersion = "apps/v1"

完整代码示例:

 private static void Main(string[] args)
{
var k8SClientConfig = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8080" };
IKubernetes client = new Kubernetes(k8SClientConfig);

ListDeployments(client);

V1Deployment deployment = new V1Deployment()
{
ApiVersion = "apps/v1",
Kind = "Deployment",
Metadata = new V1ObjectMeta()
{
Name = "nepomucen",
NamespaceProperty = null,
Labels = new Dictionary<string, string>()
{
{ "app", "nepomucen" }
}
},
Spec = new V1DeploymentSpec
{
Replicas = 1,
Selector = new V1LabelSelector()
{
MatchLabels = new Dictionary<string, string>
{
{ "app", "nepomucen" }
}
},
Template = new V1PodTemplateSpec()
{
Metadata = new V1ObjectMeta()
{
CreationTimestamp = null,
Labels = new Dictionary<string, string>
{
{ "app", "nepomucen" }
}
},
Spec = new V1PodSpec
{
Containers = new List<V1Container>()
{
new V1Container()
{
Name = "nginx",
Image = "nginx:1.7.9",
ImagePullPolicy = "Always",
Ports = new List<V1ContainerPort> { new V1ContainerPort(80) }
}
}
}
}
},
Status = new V1DeploymentStatus()
{
Replicas = 1
}
};

关于c# - 如何在 C# 中使用 kubernetes-client 创建 k8s 部署?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56054344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com