1. 网站架构
本次部署形式完全舍弃 Docker,将所有应用都置于Kubernetes,采用 Deployment 而非单 Pod 部署,稳定性得到升级。
2. 部署 MariaDB
[root@k8s-master ~]# mkdir tdr
[root@k8s-master ~]# cd tdr/
(1)定义 ConfigMap
[root@k8s-master tdr]# vim mariadb-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mariadb-configmap
data:
USER: "wp"
PASSWORD: "123"
ROOT_PASSWORD: "123"
DATABASE: "db"
(2)定义 Deployment
[root@k8s-master tdr]# vim mariadb-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb-deployment
labels:
app: mariadb-deployment
spec:
replicas:
selector:
matchLabels:
app: mariadb-deployment
template:
metadata:
labels:
app: mariadb-deployment
spec:
containers:
- name: mariadb
image: docker.io/library/mariadb:latest
imagePullPolicy: Never
ports:
- name: mariadbport
containerPort: 3306
envFrom:
- prefix: "MARIADB_"
configMapRef:
name: mariadb-configmap
(3)定义 Service
[root@k8s-master tdr]# vim mariadb-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mariadb-service
spec:
selector:
app: mariadb-deployment
ports:
- port: 3306
targetPort: 3306
protocol: TCP
type: NodePort
(4)启动 MariaDB
[root@k8s-master tdr]# kubectl create -f mariadb-configmap.yaml
configmap/mariadb-configmap created
[root@k8s-master tdr]# kubectl create -f mariadb-deployment.yaml
deployment.apps/mariadb-deployment created
[root@k8s-master tdr]# kubectl create -f mariadb-service.yaml
service/mariadb-service created
[root@k8s-master tdr]# kubectl get all
NAME READY STATUS RESTARTS AGE
#pod信息:
pod/cluster-test-66bb44bd88-nk46t 1/1 Running 38 (14m ago) 10d
pod/mariadb-deployment-5bf6d9f98c-wt7cc 1/1 Running 0 25m
#service信息:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10d
service/mariadb-service NodePort 10.96.249.62 <none> 3306:30150/TCP 14m
#deployment信息:
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/cluster-test 1/1 1 1 10d
deployment.apps/mariadb-deployment 1/1 1 1 25m
#replicaset信息:
NAME DESIRED CURRENT READY AGE
replicaset.apps/cluster-test-66bb44bd88 1 1 1 10d
replicaset.apps/mariadb-deployment-5bf6d9f98c 1 1 1 25m
[root@k8s-master tdr]# kubectl exec -it pods/cluster-test-66bb44bd88-nk46t -- bash
(08:47 cluster-test-66bb44bd88-nk46t:/) nslookup mariadb-service
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: mariadb-service.default.svc.cluster.local
Address: 10.96.249.62
3. 部署 Wrodpress
(1)定义 ConfigMap
[root@k8s-master tdr]# vim wordpress-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: wordpress-config
data:
NAME: "db"
USER: "wp"
PASSWORD: "123"
HOST: "mariadb-service"
(2)定义 Deployment
[root@k8s-master tdr]# vim wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-deployment
labels:
app: wordpress-deployment
spec:
replicas: 2
selector:
matchLabels:
app: wordpress-deployment
template:
metadata:
labels:
app: wordpress-deployment
spec:
containers:
- name: wp
image: docker.io/library/wordpress:latest
imagePullPolicy: Never
ports:
- name: wordpressport
containerPort: 80
envFrom:
- prefix: "WORDPRESS_DB_"
configMapRef:
name: wordpress-config
(3)定义 Service
[root@k8s-master tdr]# vim wordpress-service.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-service
spec:
selector:
app: wordpress-deployment
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
nodePort: 32000
type: NodePort
(4)启动 WordPress
[root@k8s-master tdr]# kubectl create -f wordpress-configmap.yaml
configmap/workpress-config created
[root@k8s-master tdr]# kubectl create -f wordpress-deployment.yaml
deployment.apps/wordpress-deployment created
[root@k8s-master tdr]# kubectl create -f wordpress-service.yaml
service/wordpress-service created
[root@k8s-master tdr]# kubectl get po
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-nk46t 1/1 Running 38 (50m ago) 10d
mariadb-deployment-5bf6d9f98c-wt7cc 1/1 Running 0 60m
wordpress-deployment-555685954b-77wb8 1/1 Running 0 107s
wordpress-deployment-555685954b-9g8rf 1/1 Running 0 107s
(5)访问测试
访问192.168.1.110:32000