安装Nginx-Web服务
准备
k8s集群
需要的k8s对象
-
Namespace
用来隔离对象资源, name=app-namespace-nginx
-
Service
Service API 是 Kubernetes 的组成部分,它是一种抽象,帮助你将 Pod 集合在网络上公开出去。 每个 Service 对象定义端点的一个逻辑集合(通常这些端点就是 Pod)以及如何访问到这些 Pod 的策略.常用的公开到集群外方式有NodePort,Ingress,些示例中使用NodePort
name=app-service-nginx
-
Deployment
用于管理运行一个应用负载的一组 Pod,通常适用于不保持状态的负载 管理运行后端服务Pod
name=app-deployment-nginx
-
Pod
实际运行业务服务的一组容器集合,是可以在 Kubernetes 中创建和管理的、最小的可部署的计算单元.
此案例中使用的服务是nginx的Web服务
name=(由调度器自动生成)
-
-
ConfigMap
保存配置文件的对象
name=app-config-map-nginx
整体描述
使用Deployment 对象管理一组Pod,Pod的实例个数使用 spec:replicas: 2 定义为2个,
Pod中运行的是一个Nginx服务,它的配置文件是从ConfigMap对象中来,使用方式是通过卷(volume)的形式挂载到Pod内的容器的,
然后使用Service来关联到Deployment管理的Pod,通过NodePort的方法显露到集群外部让client访问
yaml文件
# yaml 文件名称: app-nginx.yaml
apiVersion: v1
kind: Namespace
metadata:
name: app-namespace-nginx
---
apiVersion: v1
kind: Service
metadata:
namespace: app-namespace-nginx
name: app-service-nginx
spec:
selector:
app: app-nginx ###关联容器标签
ports:
- port: 80 ###容器端口
targetPort: 80
nodePort: 30000 ###nodeport映射为30002端口,便于外部主机访问
type: NodePort ###svc类型为nodeport
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: app-namespace-nginx
name: app-deployment-nginx
# 这里定义的是deployment的labels
labels:
app: app-deployment-nginx
spec:
replicas: 2
selector:
matchLabels:
# 这个labels 定义所创建的 ReplicaSet 如何查找要管理的 Pod,如果跟下面Pod的labels无法匹配,则会报错 spec.template.metadata.labels: Invalid value: map[string]string{"app":"nginx"}: `selector` does not match template `labels`
app: app-nginx
template:
metadata:
# 这里是Pod将要被打上的labels, 要跟上面 deployment 定义的 ReplicaSet labels 要对应
labels:
app: app-nginx
spec:
containers:
- name: nginx
image: nginx:stable
imagePullPolicy: IfNotPresent ###镜像拉取策略
ports:
- containerPort: 80
volumeMounts:
- name: app-volume-nginx-config
mountPath: /etc/nginx
volumes:
- name: app-volume-nginx-config
configMap:
# configMap的名称
name: app-config-map-nginx
optional: false # 将环境变量标记为可选
items:
# configMap的内的 key
- key: nginx-nginx-conf
path: nginx.conf # 容器卷内的文件路径
- key: nginx-mime-types
path: mime.types
---
apiVersion: v1
# https://kubernetes.io/zh-cn/docs/concepts/configuration/configmap/
kind: ConfigMap
# data 或 binaryData 字段下面的每个键的名称都必须由字母数字字符或者 -、_ 或 . 组成。
# 在 data 下保存的键名不可以与在 binaryData 下出现的键名有重叠。
# 从 v1.19 开始,你可以添加一个 immutable 字段到 ConfigMap 定义中, 创建不可变更的 ConfigMap。
metadata:
namespace: app-namespace-nginx
name: app-config-map-nginx
# 如果 immutable 为 true ,则些配置是不可变的
immutable: false
data:
nginx-nginx-conf: |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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;
server {
listen 80;
server_name localhost;
proxy_set_header host $host; # 转发请求时将请求的域名一起转发
location / {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
include /etc/nginx/conf.d/*.conf;
}
nginx-mime-types: |
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/avif avif;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;
application/vnd.wap.wmlc wmlc;
application/wasm wasm;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
---
启动和访问
# 启动
kubectl app -f app-nginx.yaml
# 查看 Service,Pod 等资源对象是否正常
# 注意所有的pod要是Running状态 ,service的port正常显露出去
kubectl -n app-namespace-nginx get all
NAME READY STATUS RESTARTS AGE
pod/app-deployment-nginx-687fbb7db-9dfbd 1/1 Running 0 113m
pod/app-deployment-nginx-687fbb7db-rjpgw 1/1 Running 0 113m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/app-service-nginx NodePort 10.233.11.138 <none> 80:30000/TCP 32h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/app-deployment-nginx 2/2 2 2 32h
NAME DESIRED CURRENT READY AGE
replicaset.apps/app-deployment-nginx-687fbb7db 2 2 2 113m
replicaset.apps/app-deployment-nginx-8cc7b775f 0 0 0 32h
# 访问 任意节点的宿主机IP:30000就可以访问
curl http://宿主机IP:30000/
标签:Web,vnd,name,application,app,Nginx,nginx,Pod,安装
From: https://www.cnblogs.com/xysgo/p/18533647