20220812实习进度汇报

实习进度汇报

这几天在做什么:

  • 掌握 statefulset、deployment 用法,实现2副本高可用并将两个pod调度至不同node(反亲和)
  • client-go 实现demo(创建deployment编排之前的docker镜像并运行);

这些事情的进度:

1.使用hugo更新实习日记至github主页

github主页 (linjianshu.github.io)

https://github.com/linjianshu/e-book-Gin

https://github.com/linjianshu/client-go

2.学习statefulset和deployment 反亲和操作
  • 反亲和

先给不同的工作节点打上了label

kubectl label nodes minikube-m02 node=worker01

kubectl label nodes minikube-m03 node=worker02

kubectl label nodes minikube node=master

apiVersion: apps/v1
kind: Deployment
metadata:
  name: e-book-gin
  labels:
    app: e-book-gin  #deployment的标签
    version: v1.0.0
spec:
  replicas: 6  #副本数量
  selector:  #pod标签选择器,匹配pod标签,默认使用pods的标签 定义标签选择器,部署需要管理的pod(带有该标签的的会被管理)需在pod 模板中定义
    matchLabels:
      app: webapp
      version: v1.0.0
  strategy:
    type: RollingUpdate   #将现有pod替换为新pod的部署策略 滚动更新
  template: #pod的定义
    metadata:
      labels:
        app: webapp  #与12行对应
        version: v1.0.0
    spec:
      affinity:
        #节点的亲和性  好像是没有节点反亲和的
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: node   #要求pod不允许运行在标签node=master的节点上
                    operator: NotIn
                    values:
                      - master
        #pod的反亲和性
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                - key: "app"  #要求pod不允许运行在已有pod标签为webapp的节点上
                  operator: In
                  values:
                  - "webapp"
              topologyKey: "kubernetes.io/hostname"
      containers:
        - image: ccr.ccs.tencentyun.com/hfut-ie/e-book-gin:v3.0 #容器的镜像地址
          name: e-book-gin  #容器的名字
          ports:
            - containerPort: 7777 #对service暴露端口
              name: http
3.client-go 实现demo

1.尝试create deployment web应用

package main

import (
   "context"
   "flag"
   "fmt"
   v1 "k8s.io/api/apps/v1"
   v12 "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/client-go/util/homedir"
   "path"
)

func main() {
   var kubeconfig *string
   if home := homedir.HomeDir(); home != "" {
      kubeconfig = flag.String("kubeconfig", path.Join(home, ".kube", "config"), "")
   } else {
      kubeconfig = flag.String("kubeconfig", "", "")
   }

   config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
   if err != nil {
      panic(err.Error())
   }

   clientset, err := kubernetes.NewForConfig(config)
   if err != nil {
      panic(err.Error())
   }

   nodeAffinity := &v12.NodeAffinity{RequiredDuringSchedulingIgnoredDuringExecution: &v12.NodeSelector{NodeSelectorTerms: []v12.NodeSelectorTerm{{
      MatchExpressions: []v12.NodeSelectorRequirement{{
         Key:      "node",
         Operator: "NotIn",
         Values:   []string{"master"},
      }},
      MatchFields: []v12.NodeSelectorRequirement{},
   }}}}

   podAntiAffinity := &v12.PodAntiAffinity{RequiredDuringSchedulingIgnoredDuringExecution: []v12.PodAffinityTerm{{
      LabelSelector:     &metav1.LabelSelector{MatchLabels: map[string]string{}, MatchExpressions: []metav1.LabelSelectorRequirement{{Key: "app", Operator: metav1.LabelSelectorOpIn, Values: []string{"webapp"}}}},
      Namespaces:        nil,
      TopologyKey:       "kubernetes.io/hostname",
      NamespaceSelector: nil,
   }}}

   deployment := &v1.Deployment{
      TypeMeta:   metav1.TypeMeta{Kind: "Deployment", APIVersion: "apps/v1"},
      ObjectMeta: metav1.ObjectMeta{Name: "e-book-gin", Labels: map[string]string{"app": "e-book-gin", "version": "v1.0.0"}},
      Spec: v1.DeploymentSpec{
         Replicas: int32Ptr(6),
         Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "webapp", "version": "v1.0.0"}},
         Strategy: v1.DeploymentStrategy{Type: v1.RollingUpdateDeploymentStrategyType},
         Template: v12.PodTemplateSpec{metav1.ObjectMeta{Labels: map[string]string{"app": "webapp", "version": "v1.0.0"}}, v12.PodSpec{Affinity: &v12.Affinity{NodeAffinity: nodeAffinity, PodAntiAffinity: podAntiAffinity}, Containers: []v12.Container{{Image: "ccr.ccs.tencentyun.com/hfut-ie/e-book-gin:v3.0", Name: "e-book-gin", Ports: []v12.ContainerPort{{ContainerPort: 7777, Name: "http"}}}}}},
      },
      Status: v1.DeploymentStatus{},
   }
   dep, err := clientset.AppsV1().Deployments(metav1.NamespaceDefault).Create(context.TODO(), deployment, metav1.CreateOptions{})
   if err != nil {
      panic(err.Error())
   }

   fmt.Println(dep.Status)
}

func int32Ptr(i int32) *int32 { return &i }

存在哪些问题:

  • core dns问题 nslookup找不到headless service的ip

打算接下来做什么:

  • 持久化 pvc pv sc
  • secret + configmap
kubectl常用指令

minikube添加工作节点

minikube node add

打标签

kubectl label nodes minikube-m02 node=worker01

kubectl label nodes minikube-m03 node=worker02

kubectl label nodes minikube node=master

查看statefulset

kubectl get sts

查看标签

kubectl get nodes –show-labels

查看hostname

for i in 0 1 2; do kubectl exec redis-statefulset-$i – sh -c ‘hostname’; done

nslookup redis-statefulset-0

检测dns是否正常

查看kube-system命名空间的pod状态

kubectl get pods -n kube-system -o wide

查看dnscore有没有问题

kubectl logs -f coredns-65c54cc984-9wxzc -n kube-system

k8s一些常用的小细节_永远在路上啊的博客-CSDN博客

loop (coredns.io)

[CoreDNS Error:FATAL] plugin/loop: Loop_你好阳光的技术博客_51CTO博客

[插件 循环] 不适用于系统解析的运行 ·第 2087 期 ·科伦斯/科伦斯 ·GitHub

修改配置
kubectl -n kube-system edit configmap coredns

删除loop

相关资料

https://www.it610.com/article/1304836244370395136.htm

https://zhuanlan.zhihu.com/p/405150555 亲和与反亲和

https://blog.csdn.net/qq_39578545/article/details/125011953 yaml文件释义

https://blog.csdn.net/yujia_666/article/details/116532953 topologyKeyhttps://blog.csdn.net/a772304419/article/details/110943111 亲和与反亲和

https://blog.csdn.net/zhuchance/article/details/117995309 client-go创建deployment

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy