首页 > 其他分享 >K8s笔记-使用 Service 把前端连接到后端

K8s笔记-使用 Service 把前端连接到后端

时间:2024-01-25 14:25:26浏览次数:21  
标签:index k8s name Service 笔记 nginx html conf K8s

1 配置configMap

1.1 配置cm

[root@k8s-master ~]# kubectl exec -it nginx-deploy-78d8bf4fd7-2xtd2 -n test -- sh -c "cat /etc/nginx/nginx.conf"
[root@k8s-master ~]# kubectl exec -it nginx-deploy-78d8bf4fd7-2xtd2 -n test -- sh -c "cat /usr/share/nginx/html/index.html"
# 在本地创建一个nginx.conf的配置文件
vi /k8s-test/subpath/nginx.conf
vi /k8s-test/subpath/index.html
vi /k8s-test/subpath/nginx-front.html
# 使用nginx.conf创建一个configmap
[root@k8s-master subpath]# kubectl create configmap nginx-conf-cm --from-file=/k8s-test/subpath/nginx.conf
[root@k8s-master subpath]# kubectl create configmap nginx-front-cm --from-file=/k8s-test/subpath/nginx-front.conf
[root@k8s-master subpath]# kubectl create configmap nginx-index-cm --from-file=/k8s-test/subpath/index.html

1.2 文件内容

[root@k8s-master subpath]# cat index.html 
<h1>Welcome to nginx from backend!</h1>
###################################################################################################
[root@k8s-master subpath]# cat nginx.conf 
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  10249;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

########################################################################
[root@k8s-master subpath]# cat nginx-front.conf 
# Backend 是 nginx 的内部标识符,用于命名以下特定的 upstream
upstream Backend {
    # hello 是 Kubernetes 中的后端服务所使用的内部 DNS 名称
    server nginx-backend;
}

server {
    listen 80;

    location / {
        # 以下语句将流量通过代理方式转发到名为 Backend 的上游
        proxy_pass http://Backend;
    }
}

2 创建后端

2.1 配置文件

[root@k8s-master nginx]# cat nginx-depoly-backend.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1    
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-index
          mountPath: 'usr/share/nginx/html/index.html'
          subPath: usr/share/nginx/html/index.html
      volumes:
      - name: nginx-index
        configMap:
          name: nginx-index-cm
          items:
          - key: index.html
            path: usr/share/nginx/html/index.html
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-backend
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80

3 创建前端

3.1 配置文件

[root@k8s-master nginx]# cat nginx-depoly-frontend.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-frontend
  labels:
    app: nginx-frontend
spec:
  selector:
    matchLabels:
      app: nginx-frontend
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-frontend
    spec:
      containers:
      - name: nginx-frontend
        image: nginx:1.16.1    
        ports:
        - containerPort: 80
        volumeMounts: # 挂载数据卷
        - name: nginx-conf # 要挂载数据卷的名称
          mountPath: '/etc/nginx/nginx.conf' # 数据卷的挂载路径
          subPath: etc/ngxin/nginx.conf
        - name: nginx-front # 要挂载数据卷的名称
          mountPath: '/etc/nginx/conf.d/default.conf' # 数据卷的挂载路径
          subPath: etc/nginx/conf.d/default.conf
      volumes: # 数据卷的定义
      - name: nginx-conf # 数据卷的名称
        configMap: # 数据卷类型为 configmap 
          name: nginx-conf-cm # 使用的configmap的名字
          items: # 对configMap中的key进行映射,如果不指定,默认会将configmap中所有key全部转换为一个同名的文件
          - key: nginx.conf # 指定挂载那个key
            path: etc/ngxin/nginx.conf # 挂载后该key重命名为什么名字
      - name: nginx-front # 数据卷的名称
        configMap: # 数据卷类型为 configmap 
          name: nginx-front-cm # 使用的configmap的名字
          items: # 对configMap中的key进行映射,如果不指定,默认会将configmap中所有key全部转换为一个同名的文件
          - key: nginx-front.conf # 指定挂载那个key
            path: etc/nginx/conf.d/default.conf # 挂载后该key重命名为什么名字
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-frontend
spec:
  selector:
    app: nginx-frontend
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  type: NodePort

4 测试

[root@k8s-node1 ~]# kubectl run -it test --image=busybox:1.28 --rm --restart=Never -- /bin/sh
# wget http://nginx-frontend
Connecting to nginx-frontend (10.105.228.85:80)
index.html           100% |****************************************************************************************************************************|    40   0:00:00 ETA
/ # cat index.html
<h1>Welcome to nginx from backend!</h1>

标签:index,k8s,name,Service,笔记,nginx,html,conf,K8s
From: https://www.cnblogs.com/goldtree358/p/17987031

相关文章

  • Pure Admin 学习笔记(一)
    介绍vue-pure-admin(opensnewwindow)是一款开源免费且开箱即用的中后台管理系统模版。使用纯ESM、Vue3、Element-Plus、Vite、TypeScript、Pinia、TailwindCSS等主流技术栈开发在线预览PureAdmin完整版PureAdmin精简版文档地址PureAdmin保姆级文档快速上手开发环境......
  • 20240125打卡——《构建之法》读书笔记第1~4章
    第一章概论在这一章中,作者为我们介绍了一些关于软件工程的基本知识。①软件=程序+软件工程:正是因为对软件开发活动(构建管理、源代码管理、软件设计、软件测试、项目管理)相关的内容的完成,才能完成把整个程序转化成为一个可用的软件的过程。扩展的推论:软件企业=软件+商业模式......
  • 计网笔记:python实现简单的UDP/TCP代码
    初学计网,同时也是第一次写blog,若有不妥之处请多多包涵......
  • 《Hive编程指南》读书笔记
    前言:最近刚接触写HiveSQL,却发现许多查询的执行速度远不如预期。为了提升查询效率,我去阅读了《Hive编程指南》,希望通过理解其底层机制来找到优化的方式,并为未来能编写出高效的SQL奠定基础。谨以此文做个记录。一、Hive因何而生先有Hadoop再有HiveHadoop实现了一个计算模型——......
  • 《SAIS Supervising and Augmenting Intermediate Steps for Document-Level Relation
    代码 原文地址 预备知识:1.什么是标记索引(tokenindices)?标记索引是一种用于表示文本中的单词或符号的数字编码。它们可以帮助计算机理解和处理自然语言。例如,假如有一个字典{"我":1,"是":2,"Bing":3,".":4},那么文本"我是Bing."的标记索引就是[1,2,3,4]。不同的模......
  • Docker启动Nacos报错:Nacos Server did not start because dumpservice bean construct
    一、表象重启服务器之后Docker运行Nacos容器,启动成功,但是外网无法访问。查看了一下Nacos启动日志(dockerlogsnacos容器名)二、分析很明显是数据库配``置问题。。如果是数据库配置的问题,可以着重检查以下信息尤其是MySQL内网Host,查询方式见Docker安装Nacos三、解决我已......
  • [Vue]Vue3学习笔记1
    官网文档: https://cn.vuejs.org/guide/essentials/template-syntax.html 每个组件都是由createApp创建import{createApp}from'vue'constapp=createApp({/*根组件选项*/}) Vue3.4+支持v-bind同名简写如果属性名称与要绑定的JavaScript值相同,语法可以......
  • 学习Java8中StreamAPI的笔记
    本次笔记记录一下我自己学习Stream流的一个情况。第一种:使用Stream流来代替增强for循环进行赋值:这是使用增强for循环的写法:publicstaticvoidmain(String[]args){ArrayList<String>strings=newArrayList<>();strings.add("张三");strings.add("李四");strings.add......
  • 学习笔记-24.1.24
    switch(v.getId()){caseR.id.eye1:if(hide){hide=false;eye1.setImageResource(R.drawable.baseline_remove_red_eye_24);//可见样貌......
  • 安卓家庭记账本开发笔记1
    项目目的:开发一个简单的记账本app开发进度:在idea程序中创建一个空项目,绘制家庭记账本的主界面。创建空项目的步骤之后会整理单独发一篇博客。下面是绘制主界面的代码:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.co......