client-go连接kubernetes集群-delete相关操作
背景
紧跟client-go连接kubernetes集群-connect and list,client-go连接kubernetes集群-create相关操作与client-go连接kubernetes集群-update相关操作。现在操作一下删除deployment 与namespace。当然了也想看一下操作集群crud的操作都有哪些动作!
client-go连接kubernetes集群-delete相关操作
删除deployment
main.go
package mainimport (
"context"
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
)func main() {
var kubeconfig *string
if home := homedir.HomeDir();
home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
DeploymentName := "nginx"
if err = clientset.AppsV1().Deployments("zhangpeng").Delete(context.TODO(), DeploymentName, metav1.DeleteOptions{});
err != nil {
fmt.Println(err)
return
}
}
文章图片
kubectl get deploymnt -n zhangpeng
文章图片
删除namespace
main.go
package mainimport (
"context"
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
)func main() {
var kubeconfig *string
if home := homedir.HomeDir();
home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
NamespaceName := "zhangpeng"
if err = clientset.CoreV1().Namespaces().Delete(context.TODO(), NamespaceName, metav1.DeleteOptions{});
err != nil {
fmt.Println(err.Error())
return
} else {
fmt.Printf("Deleted Namespaces %s\n", NamespaceName)
}
}
文章图片
kubectl get ns
文章图片
zhangpeng namespace删除成功!
其他的:
- 当然了除了常用的List delete update and create还有很多操作方法,比如namespace 与 deployment的一些其他操作:
文章图片
文章图片
- 接下来要做的:把客户端单独提取出来,跟gin整合做一些简单的crud操作......
推荐阅读
- 环境配置|ssh本机连接服务器失败
- client-go连接kubernetes集群-update相关操作
- 网络ip地址错误连接不上网
- Appium中报错,“URLError(10061-由于目标计算机积极拒绝,无法连接”解决方案)
- 电脑网络重置后如何连接网络
- 路由器怎样连接2台电脑?路由器连接2台电脑上网办法
- 打印机共享无法连接处理办法总结
- 佳能6d连接iPhone手机拍照图文详细教程
- 电脑无法连接网络怎么办
- 在mac上搭建完成 开发环境之后 跑android 项目 模拟器连接不上的问题 模拟器是genymotion