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

标签 c# asp.net .net kubernetes .net-core

我收到 Microsoft.Rest.HttpOperationException:“此行上的操作返回了无效的状态代码“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/

相关文章:

c# - 使用 innerhtml 填充 div 标签有什么缺点

html - 为什么 IE 对待我的 png 图像的方式与其他浏览器不同?

.net - 在 NHibernate 中映射枚举集合

c# - 引用外部 dll 时由于某些元组错误而无法编译 c# 项目

c# - asp.net ffmpeg 视频编码挂起

c# - 将部分构造函数参数传递给 caSTLe windsor 容器

c# - 列表在调试时不填充,直到打开

c# - 围绕另一个任意点旋转任意点的问题

javascript - 删除和退格键在 radeditor 中不起作用

c# - 简单 WebClient 仅按名称不工作 : Customized cultures cannot be passed by LCID,