1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| import ( appsv1 "k8s.io/api/apps/v1" apiv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "context" )
func createDeployment(clientset *kubernetes.Clientset) error { labels := map[string]string{ "app.kubernetes.io/env": "dev", "app.kubernetes.io/name": "nginx", "app.kubernetes.io/version": "base", }, replicas := 1 var revisionhistorylimit int32 = 1
imagepullsecret := []apiv1.LocalObjectReference{ apiv1.LocalObjectReference{Name: "acr-registry-token"}, }
deployment = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "test-nginx", Namespace: "dev", Labels: }, Spec: appsv1.DeploymentSpec{ Replicas: &replicas, Selector: &metav1.LabelSelector{ MatchLabels: labels, }, RevisionHistoryLimit: &revisionhistorylimit, Template: apiv1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, }, Spec: apiv1.PodSpec{ Volumes: []apiv1.Volume{ apiv1.Volume{ Name: "timezone", VolumeSource: apiv1.VolumeSource{ ConfigMap: &apiv1.ConfigMapVolumeSource{ LocalObjectReference: apiv1.LocalObjectReference{ Name: "timezone", }, }, }, }, }, Containers: []apiv1.Container{ apiv1.Container{ Name: "web-service", Image: fmt.Sprintf("%s:%s", image, tag), Ports: []apiv1.ContainerPort{ apiv1.ContainerPort{ Name: "web", ContainerPort: 80, Protocol: "TCP", }, }, Resources: apiv1.ResourceRequirements{ Requests: apiv1.ResourceList{ apiv1.ResourceCPU: resource.MustParse("100m"), apiv1.ResourceMemory: resource.MustParse("256Mi"), }, Limits: apiv1.ResourceList{ apiv1.ResourceMemory: resource.MustParse("1024Mi"), }, }, VolumeMounts: []apiv1.VolumeMount{ apiv1.VolumeMount{ Name: "timezone", MountPath: "/etc/localtime", SubPath: "Shanghai", ReadOnly: true, }, }, LivenessProbe: &apiv1.Probe{ ProbeHandler: apiv1.ProbeHandler{ TCPSocket: &apiv1.TCPSocketAction{ Port: intstr.FromInt(80), }, }, InitialDelaySeconds: 60, PeriodSeconds: 15, }, ImagePullPolicy: "Always", }, }, ImagePullSecrets: imagepullsecret, }, }, }, }
client := clientset.AppsV1().Deployments("dev")
result, err := clientset.AppsV1().Deployments(env).Create(context.TODO(), newappdeploy, metav1.CreateOptions{}) if err != nil { return err } fmt.Printf("Created deployment %q. \n", result.GetObjectMeta().GetName()) }
|