Kubernetes(K8s)部署WordPress博客
如果您还没有部署K8s集群环境,请查看第一期教程Kubernetes部署
一、创建命名空间
kubectl create ns wordpress
1.查看命名空间
kubectl get ns
二、配置数据库
[root@k8s-master wordpressTest]# vim wordpress-db.yaml
请复制以下内容
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-db
namespace: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress-db
template:
metadata:
labels:
app: wordpress-db
spec:
containers:
- name: wordpress-db
image: mysql:5.7
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: redhat
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-db
namespace: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress-db
数据库密码为redhat
1.创建数据库容器
[root@k8s-master wordpressTest]# kubectl create -f wordpress-db.yaml
deployment.apps/wordpress-db created
service/wordpress-db created
2.查看pod
[root@k8s-master wordpressTest]# kubectl get pods -n wordpress
NAME READY STATUS RESTARTS AGE
wordpress-db-875865d79-vlcnf 1/1 Running 0 13s #记住这里的NAME
3.连接数据库,创建用户
[root@k8s-master wordpressTest]# kubectl exec -ti -n wordpress wordpress-db-875865d79-vlcnf -- bash #中间的wordpress-db-875865d79-vlcnf为上面的NAME
使用root用户登录,密码redhat
bash-4.2# mysql -uroot -predhat
创建名为WordPress的库,密码是redhat,给wpuser给予权限
mysql> create database wordpress charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> create user 'wpuser'@"%" identified by 'redhat';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on wordpress.* to 'wpuser'@"%";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>exit;
bash-4.2# exit
三、 部署Wordpress博客
[root@k8s-master wordpressTest]# vim wordpress-deploy.yaml
把下面内容复制进去
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
imagePullPolicy: IfNotPresent
env:
- name: WORDPRESS_DB_HOST
value: wordpress-db
- name: WORDPRESS_DB_USER
value: wpuser #刚才创建的用户
- name: WORDPRESS_DB_PASSWORD
value: redhat #数据库密码
- name: WORDPRESS_DB_NAME
value: wordpress #库名
- name: WORDPRESS_TABLE_PREFIX
value: wp_
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
type: NodePort
ports:
- port: 80
nodePort: 30009 #外部端口
selector:
app: wordpress
1.创建WordPress容器
[root@k8s-master wordpressTest]# kubectl create -f wordpress-deploy.yaml
deployment.apps/wordpress created
service/wordpress created
2.查看WordPress在哪个节点上运行
[root@k8s-master wordpressTest]# kubectl get pods -n wordpress -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress-59569c76f6-5h8xq 1/1 Running 0 26s 192.168.242.164 k8s-node02.linux.com <none> <none>
wordpress-db-875865d79-vlcnf 1/1 Running 0 12m 192.168.201.234 k8s-node01.linux.com <none> <none>
WordPress运行在node2节点,数据库运行在node1节点
3.查看端口
[root@k8s-master wordpressTest]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NodePort 172.16.216.117 <none> 80:30009/TCP 37s
wordpress-db ClusterIP 172.16.45.191 <none> 3306/TCP 13m
80端口被映射到了30009端口
打开浏览器,输入node2节点的IP+30009端口即可