k8s部署nginx的三种方式小结

 更新时间:2023年11月15日 09:14:22   作者:转战容器化  
本文主要介绍了k8s部署nginx的三种方式小结,主要包括直接部署、使用数据卷部署、使用ConfigMap部署,具有一定的参考价值,感兴趣的可以了解一下

使用kubernetes来部署nginx服务,nginx一般是作为服务的入口,其在kubernetes的部署方式也大致相似,我将其分为三种----直接部署、使用数据卷部署、使用ConfigMap部署。个人建议使用ConfigMap部署。

直接部署

这种方式就是直接将nginx复制到容器内部,将其制作为镜像,之后进行部署,优点吗?不知道。缺点在每次更新配置文件时,需要重新制作经镜像

部署步骤

前提:需要有自己的nginx配置文件

  • 拉去nginx官方镜像,建议选择稳定版(stable)
$ docker pull nginx:1.22.0
  • 编写Dockerfile
FROM nginx:1.22.0

# 删除官方nginx镜像默认的配置
RUN rm -rf /etc/nginx/conf.d/default.conf

# 将nginx.conf(自己的nginx配置)添加到默认配置目录下
# 注意:nginx.conf需要与Dockerfile在同一目录
ADD ./nginx.conf /etc/nginx/conf.d/
  • 构建自己的nginx镜像
# 在Dockerfile所在目录执行,v1.0.0是新构建nginx镜像的tag
$ docker build -t nginx:v1.0.0 .
  • 编写nginx-service-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-servie
  name: nginx-service
	# 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
	# 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
  # NodePort类型可以对外暴露端口
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
		# 镜像名称
      - image: nginx:v1.0.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
  • 执行yaml文件
$ kubectl apply -f nginx-service-deployment.yaml
  • 通过nodeIp+nodePort进行访问

使用数据卷部署

这种方式是通过将nginx的配置文件以数据卷的形式挂载出来,修改nginx配置文件,只需要修改挂载出来的文件,同时删除pod即可。缺点是需要数据卷做支撑,如nfs等,如果使用pv、pvc,还需要配置pv、pvc文件,在集群模式下不要使用host进行挂载,测试时可以使用;优点是部署好后改动小。

部署步骤

前提:需要有nginx的配置文件,并且配置好nfs共享

  • 配置nfs共享,将nginx配置文件共享出来,略
    注意:挂载的方式只支持文件夹挂载不支持文件挂载,不仅是在nfs配置中,容器的配置中也是一样的
  • 编写nginx-service-deployment.yaml,示例不适用pv、pvc
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
  # 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
	# 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
  # NodePort类型可以对外暴露端口
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
      - image: nginx:1.22.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
        volumeMounts:
        - name: nginx-config
          mountPath: "/etc/nginx/conf.d/"
      volumes:
      - name: nginx-config
        nfs:
		  # 共享的目录
          path: "/opt/nginx/"
	      server: xxx.xxx.xxx.xxx
  • 执行yaml文件
$ kubectl apply -f nginx-service-deployment.yaml
  • 通过nodeIp+nodePort进行访问

使用ConfigMap进行部署

这种方式是通过ConfigMap的方式将nginx的配置文件挂载出来,修改nginx的配置文件时,只需要修改ConfigMap,同时删除就的pod即可。缺点是配置文件时只读文件,如果对文件有特殊要求的不行;优点是改动小,操作简单。

部署步骤

  • 编写nginx-configmap.yaml
    这里写了一个简单的例子,访问/hello-world/进行跳转
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
  namespace: hello-world
data:
  default.conf: |
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

  • 编写nginx-service-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-service
  name: nginx-service
	# 命名空间,没有可以删除,默认是default
  namespace: hello-world
spec:
  ports:
	# 对外暴露的端口
  - nodePort: 30013
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-pod
	# NodePort类型可以对外暴露端口
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-pod
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-pod
      namespace: hello-world
    spec:
      containers:
      - image: nginx:1.22.0
        name: nginx
        ports:
        - containerPort: 80
        resources: {}
        volumeMounts:
        - name: nginx-config
          mountPath: "/etc/nginx/conf.d/"
          readOnly: true
      volumes:
      - name: nginx-config
        configMap:
          name: nginx-configmap
  • 执行yaml文件
$ kubectl apply -f nginx-configmap.yaml
$ kubectl apply -f nginx-service-deployment.yaml
  • 通过nodeIp+nodePort进行访问

到此这篇关于k8s部署nginx的三种方式小结的文章就介绍到这了,更多相关k8s部署nginx内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Nginx的安装和多域名配置的实现方法

    Nginx的安装和多域名配置的实现方法

    这篇文章主要介绍了Nginx的安装和多域名配置的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • Nginx日志按日期切割详解(按天切割)

    Nginx日志按日期切割详解(按天切割)

    由于nginx的日志本身只是支持按照server_name或者大小进行划分,对于习惯了apache的按照一个网站每天一个日志的我来说是不可以接受的,所以就实现了按天切割的功能,这篇文章主要介绍了关于Nginx日志按日期切割的相关资料,需要的朋友可以参考下。
    2017-03-03
  • 详解nginx服务器http重定向到https的正确写法

    详解nginx服务器http重定向到https的正确写法

    本篇文章主要介绍了nginx服务器http重定向到https的正确写法 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Nginx的缓存配置与其他配置方法

    Nginx的缓存配置与其他配置方法

    Nginx的压缩功能配置是通过gzip压缩技术,可以使原来的网页的内容大小压缩成原来的30%,这样,用户在访问网页的时候,由于传输的内容比原内容小很多,故而速度会快很多,本文给大家介绍Nginx的缓存配置与其他配置方法,感兴趣的朋友一起看看吧
    2023-12-12
  • 神器!最佳 Nginx 日志分析工具 GoAccess

    神器!最佳 Nginx 日志分析工具 GoAccess

    非常小又精悍的 Nginx 日志分析工具 GoAccess,今天在 CentOS VPS 上安装测试了一番,就2个字,神器!
    2014-02-02
  • 通过浏览器查看nginx服务器状态配置方法

    通过浏览器查看nginx服务器状态配置方法

    这篇文章主要介绍了通过浏览器查看nginx服务器状态配置方法,本文讲解开启nginx-status的配置方法,并对服务器的参数做了详细讲解,需要的朋友可以参考下
    2015-04-04
  • Nginx服务器下配置使用索引目录的教程

    Nginx服务器下配置使用索引目录的教程

    这篇文章主要介绍了Nginx服务器下配置使用索引目录的教程,包括自带的auto_index和使用fancy插件美化的用法,需要的朋友可以参考下
    2016-01-01
  • Nginx方向代理wss或ws的实现示例

    Nginx方向代理wss或ws的实现示例

    本文主要介绍了Nginx方向代理wss或ws的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-10-10
  • Nginx访问日志access_log配置及信息详解(推荐)

    Nginx访问日志access_log配置及信息详解(推荐)

    当你设置日志级别成debug,如果你在调试一个在线的高流量网站的话,你的错误日志可能会记录每个请求的很多消息,这样会变得毫无意义,下面小编给大家介绍Nginx访问日志access_log配置及信息详解,感兴趣的朋友跟随小编一起看看吧
    2024-04-04
  • nginx处理http请求实现过程解析

    nginx处理http请求实现过程解析

    这篇文章主要介绍了nginx处理http请求实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11

最新评论