跨可用区的集群k8s的基本操作和配置理解

service

启动:kubectl -f api_service.yaml -n <namespace>

查询:kubectl get svc -n <namespace> ;

修改:kubectl apply -f api_service.yaml -n <namespace>

停止service: kubectl delete svc [service名字] 或者 kubectl delete -f api_service.yaml (删除文件中定义的所有service)

pod

启动:kubectl -f api.yaml -n <namespace>

查询:kubectl get pods -n <namespace> ;kubectl describe pod [podName] -n <namespace>

修改:kubectl apply -f api.yaml -n <namespace>

删除pod重启: kubectl delete pod [pod_name]

停止pod:kubectl delete -f api.yaml

注:pod就是在deployment下面定义的,因此停止了pod,deployment也会停止,而删除pod,由于deployment还在,所以会自动重启

pod和service一般可以写到一个文件中,一次性全部启动,一次性全部删除

apisixroute

启动:kubectl -f apisixroute.yaml -n <namespace>

查询:kubectl get apisixroute -n <namespace> ;kubectl describe apisixroute -n <namespace>

修改:kubectl apply -f apisixroute.yaml -n <namespace>

停止: kubectl delete apisixroute <apisixrouter的名字> 或者 kubectl delete -f apisixroute.yaml (删除文件中定义的所有apisixroute)

configmap

==共享k8s更换configmap的操作 ==

  1. kubectl get configmaps -n <namespace>
  2. kubectl delete configmap configmap-config2 -n
  3. 删除application 4.传入新的application 5.kubectl create configmap configmap-config2 --from-file=application.yml -n <namespace> 6. 重启服务

:configmap在pod的yml文件中配置,包括configmap的名称,就是springboot项目中用到的application.yml文件转换的

shell 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: <deployment名称>
  namespace: <namespace名称>
spec:
  replicas: 3  <启动的容器的数量,保证高可用>
  selector:
    matchLabels:
      component: test #<是一个列表,用于匹配pod标签,表示deployment应用到那些pod上>
  template:
    metadata:
      labels:
        component: test  #<和上面的matchLabels.component的值需要保持一致,否则报错>
    spec:
      affinity:
        nodeAffinity: #节点亲和
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions: #选择调度到哪些 AZ
                - key: topology.kubernetes.io/zone
                  operator: In
                  values:
                    - suzhou
                    - wuxi
                    #- fenhu
        podAntiAffinity: #pod 反亲和,控制 pod 起在不同主机
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
                - key: component #根据服务标签自行修改好了
                  operator: In
                  values:
                    - test-api #根据服务标签自行修改
            topologyKey: kubernetes.io/hostname
      topologySpreadConstraints: #均匀分布,控制 pod 起在不同 AZ
      - labelSelector:
          matchLabels:
            component: test-api #根据服务标签自行修改maxSkew:1
        maxSkew: 1  
        topologyKey: topology.kubernetes.io/zone
        whenUnsatisfiable: DoNotSchedule
      tolerations: #容忍,允许 pod 调度到 Euler 节点
        - effect: NoSchedule
          key: noderole
          operator: Equal
          value: Euler
      nodeSelector: #选择调度到 Euler
        noderole: Euler                      
      containers:   #一个pod可以定义多个docker容器   使用kubectl describe <pod名称> 可以查看到docker容器的细节信息
      - name: <容器名称>
        image: 镜像地址:镜像号  
        imagePullPolicy: IfNotPresent  #镜像pull策略,Always(总是),Never(总不),IfNotPresent(如果没有就pull) IfNotPresent 本地的k8s没有镜像才去远程pull
        lifecycle: {}
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 500m
            memory: 1Gi
        ports:
        - containerPort: 9900
        volumeMounts:
          - name: config-volume
            mountPath: /apps/svr//resource
      volumes:
        - name: config-volume
          configMap:
            name: test-config2   #用于配置本容器应用的configmap
---
apiVersion: v1
kind: Service
metadata:
  name: <service名称>
spec:
  selector:
    component: test #<一个标签,用于匹配deployment标签,表示service应用到那个service中>
  ports:
    - protocol: TCP
      port: 9900 
      targetPort: 9900        #将容器的9900端口映射到pod的9900端口,可以通过pod的地址+端口访问容器服务
  #      nodePort: 32698
  type: ClusterIP     #除了ClusterIP,还有NodePort,LoadBalancer选项      ClusterIP表示只能在k8s集群内部访问,NodePort表示可以再k8s集群以外的地址访问,需要配置额外的参数nodePort,如上注释掉的部分,表示暴露在外的端口,LoadBalancer是负载均衡的方式(我没用过)

apisixroute.yaml文件

shell 复制代码
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
  name: <apisix的名称>
  namespace: <命名空间>
spec:
  http:
    - name: <spec的名称>
      match:
        hosts:
          - console.exam.test.internal
        paths:
          - /exam/console/service/*
      backend:
        serviceName: <访问的service的名称>
        servicePort: 9900
      plugins:
        - name: proxy-rewrite
          enable: true
          config:
            regex_uri: [ "^/exam/console/service/(.*)", "/exam/$1" ]  #在转发请求之前,Apache APISIX 会使用 proxy-rewrite 插件对请求的 URI 进行重写,将 /exam/console/service/(.*) 重写为 /exam/$1,其中 $1 是原始路径中 /exam/console/service/ 之后的部分。

==一个有意思的命令 ==

kubectl explain Deployment.spec.selector 解释Deployment里的spec.selector标签 哪个标签不懂的话就改成哪个标签,会有英文解释

相关推荐
Ice星空3 小时前
Docker 镜像创建和管理以及 buildx 交叉编译
运维·docker·容器
Cyber4K3 小时前
【Kubernetes专项】Docker 容器部署及基本用法
运维·docker·云原生·容器
暴躁的鱼3 小时前
docker运行可登录的gerrit容器
运维·docker·容器
Joren的学习记录3 小时前
【Linux运维大神系列】Kubernetes详解2(kubeadm部署k8s1.27单节点集群)
linux·运维·kubernetes
赵文宇(温玉)4 小时前
Docker与VM的差异与最佳场景
docker·容器·eureka
lbb 小魔仙4 小时前
【Linux】K8s 集群搭建避坑指南:基于 Linux 内核参数调优的生产级部署方案
linux·运维·kubernetes
techzhi4 小时前
docker compose和docker-compose的区别
运维·docker·容器
jasnet_u4 小时前
SpringBoot3.x+SpringCloudAlibaba2023+JDK17微服务基础框架搭建
微服务·云原生·架构
木二_4 小时前
附058.Kubernetes Gitea部署
ci/cd·kubernetes·gitea
没有bug.的程序员4 小时前
Kubernetes 与微服务的融合架构:调度、弹性、健康检查深度协同
jvm·微服务·云原生·架构·kubernetes·健康检查·弹性伸缩