跨可用区的集群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标签 哪个标签不懂的话就改成哪个标签,会有英文解释

相关推荐
颜颜yan_42 分钟前
在openEuler上搞个云原生AI模型商店:像点外卖一样部署模型
人工智能·云原生
阿里云云原生2 小时前
线下活动速递丨AI 原生应用开源开发者沙龙·杭州站
云原生
阿里云云原生3 小时前
MCP 网关实战:基于 Higress + Nacos 的零代码工具扩展方案
云原生·mcp
阿里云云原生3 小时前
UModel 查询:驯服“可观测性混乱”,阿里云的图模型建模利器!
云原生
水滴与鱼3 小时前
DOCKER制作ROS运行的镜像文件
运维·docker·容器
洒家肉山大魔王5 小时前
Kubernetes中Pod 处于 CrashLoopBackOff 状态(生产环境)
linux·容器·kubernetes·pod·pod循环重启
weixin_307779137 小时前
采用Amazon SES解决电商邮件延迟:以最小化运维实现最大效率的方案选择
运维·云原生·架构·云计算·aws
Lynnxiaowen7 小时前
今天我们学习kubernetes内容Ingress资源对象
学习·容器·kubernetes
聊天QQ:4877392787 小时前
探索锂电池主动均衡仿真:从开关电容到多种电路的奇妙之旅
云原生
明月惊雀7 小时前
微服务搭建踩坑
微服务·云原生·架构