예제 #1
0
        public static Appsv1beta1Deployment CreateDeployment(string serverName, int replicasCount)
        {
            var image        = "openhack/minecraft-server:2.0-alpine";
            var instanceName = "minecraft-server-" + serverName;
            var storageName  = serverName + "-storage";
            var deployment   = new Appsv1beta1Deployment
            {
                ApiVersion = "apps/v1beta1",
                Kind       = "Deployment",
                Metadata   = new V1ObjectMeta
                {
                    Name = instanceName
                },
                Spec = new Appsv1beta1DeploymentSpec
                {
                    Replicas = replicasCount,
                    Template = new V1PodTemplateSpec
                    {
                        Metadata = new V1ObjectMeta
                        {
                            Labels = new Dictionary <string, string> {
                                { "app", instanceName }
                            }
                        },
                        Spec = new V1PodSpec
                        {
                            Containers = new List <V1Container>
                            {
                                new V1Container
                                {
                                    Name  = instanceName,
                                    Image = image,
                                    Env   = new List <V1EnvVar>
                                    {
                                        new V1EnvVar
                                        {
                                            Name  = "EULA",
                                            Value = "true"
                                        }
                                    },
                                    Ports = new List <V1ContainerPort>
                                    {
                                        new V1ContainerPort
                                        {
                                            ContainerPort = 25565,
                                            Name          = "first-port"
                                        },
                                        new V1ContainerPort
                                        {
                                            ContainerPort = 25575,
                                            Name          = "second-port"
                                        }
                                    },
                                    VolumeMounts = new List <V1VolumeMount>
                                    {
                                        new V1VolumeMount
                                        {
                                            Name      = "azure",
                                            MountPath = "/data"
                                        }
                                    }
                                }
                            },
                            Volumes = new List <V1Volume>
                            {
                                new V1Volume("azure",
                                             persistentVolumeClaim: new V1PersistentVolumeClaimVolumeSource
                                {
                                    ClaimName = storageName
                                })
                            }
                        }
                    }
                }
            };

            return(deployment);
        }
예제 #2
0
        internal void AddServicePod(string podName)
        {
            podName = podName.ToLower();
            var appLabel = new Dictionary <string, string> {
                { "app", podName }
            };

            using (Stream stream = KubeConfigStream)
            {
                var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(stream);

                using (IKubernetes client = new Kubernetes(config))
                {
                    var deployment = new Appsv1beta1Deployment("apps/v1beta1", "Deployment")
                    {
                        Metadata = new V1ObjectMeta()
                        {
                            Name = podName
                        },
                        Spec = new Appsv1beta1DeploymentSpec()
                        {
                            Template = new V1PodTemplateSpec()
                            {
                                Metadata = new V1ObjectMeta()
                                {
                                    Labels = appLabel
                                },
                                Spec = new V1PodSpec()
                                {
                                    Containers = new k8s.Models.V1Container[] {
                                        new V1Container
                                        {
                                            Name         = podName,
                                            Image        = "openhackteam5.azurecr.io/minecraft-server:2.0",
                                            VolumeMounts = new V1VolumeMount[]
                                            {
                                                new k8s.Models.V1VolumeMount
                                                {
                                                    Name      = "volume",
                                                    MountPath = "/data"
                                                }
                                            },
                                            Ports = new V1ContainerPort[]
                                            {
                                                new V1ContainerPort(25565, name: "port25565"),
                                                new V1ContainerPort(25575, name: "port25575")
                                            },
                                            Env = new V1EnvVar[] { new V1EnvVar("EULA", true.ToString()) }
                                        }
                                    },
                                    Volumes = new V1Volume[]
                                    {
                                        new V1Volume("volume", persistentVolumeClaim: new V1PersistentVolumeClaimVolumeSource(podName + SuffixPVC))
                                    },
                                    ImagePullSecrets = new V1LocalObjectReference[] { new V1LocalObjectReference("acr-auth") }
                                }
                            }
                        }
                    };
                    var loadBalancer = new V1Service("v1", "Service")
                    {
                        Metadata = new V1ObjectMeta {
                            Name = podName + SuffixLoadBalancer
                        },
                        Spec = new V1ServiceSpec
                        {
                            Type  = "LoadBalancer",
                            Ports = new V1ServicePort[] {
                                new   V1ServicePort(25565, "port25565", targetPort: 25565),
                                new   V1ServicePort(25575, "port25575", targetPort: 25575)
                            },
                            Selector = appLabel
                        }
                    };


                    var persistentVolumeClaim = new V1PersistentVolumeClaim()
                    {
                        Metadata = new V1ObjectMeta()
                        {
                            Name = podName + SuffixPVC, NamespaceProperty = DefaultNamespace
                        },
                        Spec = new V1PersistentVolumeClaimSpec
                        {
                            AccessModes      = new string[] { "ReadWriteMany" },
                            StorageClassName = "azurefile",
                            Resources        = new V1ResourceRequirements(requests: new Dictionary <string, ResourceQuantity> {
                                { "storage", new ResourceQuantity("5Gi") }
                            })
                        },
                        Status = new V1PersistentVolumeClaimStatus()
                    };
                    var pvcs = client.ListDeploymentForAllNamespaces1();
                    client.CreateNamespacedPersistentVolumeClaim(persistentVolumeClaim, DefaultNamespace);
                    client.CreateNamespacedDeployment1(deployment, DefaultNamespace);
                    client.CreateNamespacedService(loadBalancer, DefaultNamespace);
                }
            }
        }