一、統(tǒng)一配置中心
使用掛在配置文件方式,支持熱更新纳胧,配置文件內(nèi)容可命令生成,后復(fù)制到configmap的yml文件中帘撰。這里不做介紹了
- 1跑慕、編輯ConfigMap配置文件 configmap-nginx.yml 直接上內(nèi)容:
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-configmap
data:
nginx_conf: |-
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#測(cè)試個(gè)性cm
location /test/ {
proxy_pass http://www.baidu.com/;
proxy_redirect default;
proxy_set_header Host www.baidu.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
add_header Set-Cookie "flag=ssz-test";
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- 2、執(zhí)行發(fā)布命令(支持熱更新)
kubectl apply -f configmap-nginx.yml
二摧找、發(fā)布Pod
- 1 編寫(xiě)配置文件Deployment配置文件: deployment-nginx.yml
內(nèi)容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
app: my-nginx
replicas: 2
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: nginx
subPath: nginx.conf
volumes:
- name: nginx
configMap:
name: nginx-configmap
items:
- key: nginx_conf
path: nginx.conf
#resources:
# requests:
# cpu: 1
# memory: 500Mi
# limits:
# cpu: 2
# memory: 1024Mi
- 2核行、執(zhí)行發(fā)布命令
kubectl apply -f deployment-nginx.yml
三、發(fā)布Service (NodePort方式)
- 1蹬耘、編寫(xiě)Service配置 service-nginx.yml
內(nèi)容下:
apiVersion: v1
kind: Service
metadata:
name: nginx-svc #定義service名稱為nginx-service
labels:
app: nginx-svc #為service打上app標(biāo)簽
spec:
type: NodePort #使用NodePort方式開(kāi)通芝雪,在每個(gè)Node上分配一個(gè)端口作為外部訪問(wèn)入口
selector:
app: my-nginx
ports:
- port: 8000 #port是k8s集群內(nèi)部訪問(wèn)service的端口,即通過(guò)clusterIP: port可以訪問(wèn)到某個(gè)service
targetPort: 80 #targetPort是pod的端口综苔,從port和nodePort來(lái)的流量經(jīng)過(guò)kube-proxy流入到后端pod的targetPort上惩系,最后進(jìn)入容器
nodePort: 30080 #nodePort是外部訪問(wèn)k8s集群中service的端口,通過(guò)nodeIP: nodePort可以從外部訪問(wèn)到某個(gè)service
- 2如筛、執(zhí)行發(fā)布命令
kubectl apply -f service-nginx.yml
四堡牡、總結(jié)
- 1、使用NodePort方式發(fā)布的Service端口范圍只能30000~32767杨刨,可以在任一節(jié)點(diǎn)訪問(wèn)晤柄。如果想使用80端口,只能前置 nginx 反向代理到80 端口妖胀。
- 2可免、關(guān)于修改configmap內(nèi)容后的更新。
直接個(gè)性configmap-nginx.yml內(nèi)容 做粤,比如個(gè)性其中的baidu為163 ,
kubectl apply -f configmap-nginx.yml
配置即可生效捉撮。但是pod中的nginx是無(wú)法加載的怕品,可以升級(jí)nginx鏡像版本后滾動(dòng)更新,如:
kubectl apply -f deployment-nginx-v2.yml
也可以直接銷毀當(dāng)前容器新建:
kubectl scale deployment my-nginx --replicas= 0
kubectl scale deployment my-nginx --replicas= 2