前提摘要
-
一台主机:master
-
一台从机:node
-
主机安装: prometheus 监控插件
-
案例说明:
- 主机内网:172.21.0.6
- 从机内网:172.21.0.13
- 项目模板:Yii2.0 advanced 高级模板
- 项目路径:172.21.0.6:/usr/share/nginx/html/
- 项目镜像:980315926pxm/lnmp:7.3(nginx镜像容器使用apt安装的php7.3-fpm)
目录
-
1. 安装 NFS 网络插件 (主机 + 从机)
-
2. 主机 master 开放网络共享项目目录
-
3. 编写 frontend.conf、backend.conf (nginx 配置文件)
-
4. 编写 frontend-rc.yaml、backend-rc.yaml (ReplicationController)
-
5. 编写 advanced-svc.yaml (Service)
-
6. 创建 ConfigMap 、ReplicationController、Service
-
7. ReplicationController 添加高可用自动缩容功能
-
8. kubectl 命令详解
1. 安装 NFS 网络插件 (主机 + 从机)
ubuntu:
# apt-get 方式安装
apt-get install nfs-kernel-server
# 启动并设置开机自动启动
systemctl enable rpcbind
systemctl enable nfs-kernel-server
systemctl start rpcbind
systemctl start nfs-kernel-server
centos:
# yum 方式安装
yum -y install nfs-utils rpcbind
# 启动并设置开机自动启动
systemctl enable rpcbind
systemctl enable nfs
systemctl start rpcbind
systemctl start nfs
2. 主机 master 开放网络共享项目目录
cat <<EOF > /etc/exports
/usr/share/nginx/html/
EOF
systemctl restart nfs-kernel-server # ubuntu重启方式
systemctl restart nfs # centos重启方式
切换登录其他 node 从机查看
showmount -e 172.21.0.6
image.png
3. 编写 frontend.conf、backend.conf (nginx 配置文件)
frontend.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/frontend/web;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html/frontend/web;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
backend.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/backend/web;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php$is_args$args;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html/backend/web;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
4. 编写 frontend-rc.yaml、backend-rc.yaml (ReplicationController)
frontend-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
spec:
replicas: 1
selector:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: 980315926pxm/lnmp:7.3
imagePullPolicy: IfNotPresent
resources:
requests:
memory: 64Mi
cpu: 100m
limits:
memory: 128Mi
cpu: 200m
livenessProbe:
exec:
command:
- "/etc/init.d/php7.3-fpm"
- "start"
ports:
- containerPort: 80
volumeMounts:
- name: code
mountPath: /usr/share/nginx/html/
- name: config
mountPath: /etc/nginx/conf.d/
volumes:
- name: code
nfs:
server: 172.21.0.6
path: "/usr/share/nginx/html/advanced/"
- name: config
configMap:
name: frontend
items:
- key: "frontend.conf"
path: "frontend.conf"
backend-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: backend
spec:
replicas: 1
selector:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: 980315926pxm/lnmp:7.3
imagePullPolicy: IfNotPresent
resources:
requests:
memory: 64Mi
cpu: 100m
limits:
memory: 128Mi
cpu: 200m
livenessProbe:
exec:
command:
- "/etc/init.d/php7.3-fpm"
- "start"
ports:
- containerPort: 80
volumeMounts:
- name: code
mountPath: /usr/share/nginx/html/
- name: config
mountPath: /etc/nginx/conf.d/
volumes:
- name: code
nfs:
server: 172.21.0.6
path: "/usr/share/nginx/html/advanced/"
- name: config
configMap:
name: backend
items:
- key: "backend.conf"
path: "backend.conf"
注:spec.containers.volumes.nfs.server 参数填写项目主机内网地址(不要抄我的 172.21.0.6)
5. 编写 advanced-svc.yaml (Service 暴露端口:30080、30081)
apiVersion: v1
kind: Service
metadata:
name: frontend-service
labels:
app: frontend
spec:
type: NodePort
ports:
- name: frontend
port: 80
nodePort: 30080
selector:
app: frontend
---
apiVersion: v1
kind: Service
metadata:
name: backend-service
labels:
app: backend
spec:
type: NodePort
ports:
- name: backend
port: 80
nodePort: 30081
selector:
app: backend
注:type = NodePort 暴露 port 限制范围(30000~32727)
6. 创建 ConfigMap 、ReplicationController、Service
# ConfigMap
kubectl create configmap frontend --from-file=frontend.conf
kubectl create configmap backend --from-file=backend.conf
# ReplicationController
kubectl create -f frontend-rc.yaml
kubectl create -f backend-rc.yaml
# Service
kubectl create -f advanced-svc.yaml
-
frontend :http://xx.xx.xx.xx:30080
-
backend :http://xx.xx.xx.xx:30081
7. ReplicationController 添加高可用自动缩容功能
kubectl autoscale rc frontend-rc --min=1 --max=10 --cpu-percent=50
kubectl autoscale rc backend-rc --min=1 --max=10 --cpu-percent=50
默认监测时间:30s
master 检测到项目容器 cpu 使用率上升至 50% 以上时,自动扩充容器数量
master 检测到项目容器 cpu 使用率下降至 50% 以下时,自动缩减容器数量
8. kubectl 命令详解
-
kubectl get po
-
kubectl get rc
-
kubectl get svc
-
kubectl get cm
-
kubectl get hpa
-
kubectl get node
-
kubectl describe po pod.name
-
kubectl describe rc rc.name
-
kubectl describe svc svc.name
-
kubectl describe node node.name
-
kubectl top po
-
kubectl top node