package main
import (
"context"
"fmt"
appsV1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
//"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func main() {
configPath := "C:\Users\admin\Desktop\api-six\k8s-config-admin"
config, _ := clientcmd.BuildConfigFromFlags("", configPath)
clientset, _ := kubernetes.NewForConfig(config)
namespace := "devops"
var replicas int32 = 2
deployment := &appsV1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx",
Labels: map[string]string{
"app": "nginx",
},
},
Spec: appsV1.DeploymentSpec{
Replicas: &replicas,
//Replicas: int32Ptr(3),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "nginx",
},
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "nginx",
},
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "nginx",
Image: "nginx:latest",
Ports: []apiv1.ContainerPort{
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
ContainerPort: 80,
},
},
},
},
},
},
},
}
deployment, err := clientset.AppsV1().Deployments(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
panic(err)
}
fmt.Println(err, deployment)
}
[root@node66 ~]# kubectl get pods -n devops | grep nginx
nginx-86b8cc866b-dkxkr 1/1 Running 0 90s
nginx-86b8cc866b-tp74k 1/1 Running 0 90s
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)