k8s部署nginx的三種方式小結(jié)
使用kubernetes來部署nginx服務(wù),nginx一般是作為服務(wù)的入口,其在kubernetes的部署方式也大致相似,我將其分為三種----直接部署、使用數(shù)據(jù)卷部署、使用ConfigMap部署。個人建議使用ConfigMap部署。
直接部署
這種方式就是直接將nginx復(fù)制到容器內(nèi)部,將其制作為鏡像,之后進行部署,優(yōu)點嗎?不知道。缺點在每次更新配置文件時,需要重新制作經(jīng)鏡像
部署步驟
前提:需要有自己的nginx配置文件
- 拉去nginx官方鏡像,建議選擇穩(wěn)定版(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/
- 構(gòu)建自己的nginx鏡像
# 在Dockerfile所在目錄執(zhí)行,v1.0.0是新構(gòu)建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: {}
- 執(zhí)行yaml文件
$ kubectl apply -f nginx-service-deployment.yaml
- 通過nodeIp+nodePort進行訪問
使用數(shù)據(jù)卷部署
這種方式是通過將nginx的配置文件以數(shù)據(jù)卷的形式掛載出來,修改nginx配置文件,只需要修改掛載出來的文件,同時刪除pod即可。缺點是需要數(shù)據(jù)卷做支撐,如nfs等,如果使用pv、pvc,還需要配置pv、pvc文件,在集群模式下不要使用host進行掛載,測試時可以使用;優(yōu)點是部署好后改動小。
部署步驟
前提:需要有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
- 執(zhí)行yaml文件
$ kubectl apply -f nginx-service-deployment.yaml
- 通過nodeIp+nodePort進行訪問
使用ConfigMap進行部署
這種方式是通過ConfigMap的方式將nginx的配置文件掛載出來,修改nginx的配置文件時,只需要修改ConfigMap,同時刪除就的pod即可。缺點是配置文件時只讀文件,如果對文件有特殊要求的不行;優(yōu)點是改動小,操作簡單。
部署步驟
- 編寫nginx-configmap.yaml
這里寫了一個簡單的例子,訪問/hello-world/進行跳轉(zhuǎn)
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
- 執(zhí)行yaml文件
$ kubectl apply -f nginx-configmap.yaml $ kubectl apply -f nginx-service-deployment.yaml
- 通過nodeIp+nodePort進行訪問
到此這篇關(guān)于k8s部署nginx的三種方式小結(jié)的文章就介紹到這了,更多相關(guān)k8s部署nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx+SSL+Node.js運行環(huán)境配置教程
這篇文章主要介紹了Nginx+SSL+Node.js運行環(huán)境配置教程,本文用反向代理的方式代理基于Node.js的Web應(yīng)用,需要的朋友可以參考下2014-09-09nginx反向代理用做內(nèi)網(wǎng)域名轉(zhuǎn)發(fā)
這篇文章主要為大家詳細介紹了nginx反向代理用做內(nèi)網(wǎng)域名轉(zhuǎn)發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10使用nginx同域名下部署多個vue項目并使用反向代理的方法
這篇文章主要介紹了使用nginx同域名下部署多個vue項目并使用反向代理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02Nginx下讓W(xué)ordPress支持固定鏈接的偽靜態(tài)規(guī)則
Nginx下讓W(xué)ordPress支持固定鏈接的偽靜態(tài)規(guī)則,要讓nginx支持wordpress固定鏈接非常簡單,需要自己進行添加點配置代碼2013-02-02