WordPress 是一种使用 PHP 语言开发的博客平台,用户可以在支持 PHP 和 MySQL 数据库的服务器上架设自己的网志。也可以把 WordPress 当作一个内容管理系统(CMS)来使用。WordPress 是一个免费的开源项目,在 GNU 通用公共许可证下授权发布。
kubernetes,简称K8s,是用8代替名字中间的8个字符“ubernete”而成的缩写。是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。
kubernetes集群架构示意图
1.本次实践环境为个人测试学习环境,生产环境请谨慎使用;
2.本次实践是在k8s环境中,部署wordpress服务;
3.可以成功在在局域网内访问wordpres的web页面。
本次实践规划环境介绍
hostname | IP地址 | 操作系统版本 | k8s版本 | 角色 |
---|---|---|---|---|
k8s-master | 192.168.3.201 | centos7.6 | v1.23.1 | master节点 |
k8s-node01 | 192.168.3.202 | centos7.6 | v1.23.1 | worker节点 |
k8s-node02 | 192.168.3.203 | centos7.6 | v1.23.1 | worker节点 |
检查k8s的各工作节点状态
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready control-plane,master 289d v1.23.1
k8s-node01 Ready <none> 289d v1.23.1
k8s-node02 Ready <none> 289d v1.23.1
检查k8s集群中各系统组件的pod状态,确保都处于正常状态。
[root@k8s-master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-7bc6547ffb-2nf66 1/1 Running 18 (13m ago) 289d
calico-node-8c4pn 1/1 Running 17 (2d8h ago) 289d
calico-node-f28qq 1/1 Running 18 (12m ago) 289d
calico-node-wmc2j 1/1 Running 18 (13m ago) 289d
coredns-6d8c4cb4d-6gm4x 1/1 Running 18 (13m ago) 289d
coredns-6d8c4cb4d-7vxlz 1/1 Running 18 (13m ago) 289d
etcd-k8s-master 1/1 Running 18 (13m ago) 289d
kube-apiserver-k8s-master 1/1 Running 18 (13m ago) 289d
kube-controller-manager-k8s-master 1/1 Running 18 (13m ago) 289d
kube-proxy-8dfw8 1/1 Running 18 (13m ago) 289d
kube-proxy-ghzrv 1/1 Running 18 (12m ago) 289d
kube-proxy-j867z 1/1 Running 17 (2d8h ago) 289d
kube-scheduler-k8s-master 1/1 Running 18 (13m ago) 289d
创建wordpress部署目录
[root@k8s-master ~]# mkdir -p /data/wordpress
[root@k8s-master ~]# cd /data/wordpress/
[root@k8s-master wordpress]#
创建mysql的相关密码文件
cat << EOF > mysql_pw.txt
username=wordpress
password=wordoress
>EOF
创建一个wordpress的命名空间
kubectl create namespace wordpress
创建secret资源
[root@k8s-master wordpress]# kubectl --namespace=wordpress create secret generic mysecret --from-env-file=./mysql_pw.txt
secret/mysecret created
[root@k8s-master wordpress]# kubectl get secrets -n wordpress
NAME TYPE DATA AGE
default-token-clz9g kubernetes.io/service-account-token 3 29m
mysecret Opaque 2 19s
该yaml文件包括wordpress服务和数据库服务两部分,存储采用的本地存储hostpath方式。
[root@k8s-master wordpress]# kubectl apply -f wordpress.yaml
service/wordpress unchanged
deployment.apps/wordpress configured
service/wordpress-mysql unchanged
statefulset.apps/wordpress-mysql unchanged
[root@k8s-master wordpress]# cat wordpress.yaml
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
labels:
app: wordpress
spec:
ports:
- protocol: TCP
targetPort: 80
port: 80
nodePort: 30020
selector:
app: wordpress
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: wordpress
labels:
app: wordpress
spec:
replicas: 2
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql.wordpress.svc.cluster.local
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
volumeMounts:
- mountPath: "/var/www/html"
name: wordpress-data
volumes:
- name: wordpress-data
hostPath:
path: /data/wordpress/data
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
namespace: wordpress
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: wordpress-mysql
namespace: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
tier: mysql
serviceName: "wordpress-mysql"
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-storage
volumes:
- name: mysql-storage
hostPath:
path: /var/lib/mysql
应用wordpress.yaml文件
[root@k8s-master wordpress]# kubectl apply -f wordpress.yaml
service/wordpress unchanged
deployment.apps/wordpress unchanged
service/wordpress-mysql unchanged
statefulset.apps/wordpress-mysql unchanged
检查wordpress的相关pod状态
[root@k8s-master wordpress]# kubectl get pods -n wordpress
NAME READY STATUS RESTARTS AGE
wordpress-758b776dd-5fkg5 1/1 Running 0 5m9s
wordpress-758b776dd-j8s66 1/1 Running 0 4m53s
wordpress-mysql-0 1/1 Running 0 25m
查看wordpress对外访问端口为30020
[root@k8s-master wordpress]# kubectl get service -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NodePort 10.98.58.28 <none> 80:30020/TCP 35m
wordpress-mysql ClusterIP 10.106.221.76 <none> 3306/TCP 14m
访问地址:http://192.168.3.202:30020/wp-admin/install.php
可以选择系统中文语言或者英语,这里是选择英语。
自定义网站信息,设置网站名及网站管理员密码等信息。
访问地址:http://192.168.3.202:30020/
后台管理地址:http://192.168.3.202:30020/wp-admin/
输入之前设置的管理员和密码,进入ordpress后台管理。