k8s-WordPress 网站搭建
使用k8s安装wordpress
WordPress 网站搭建
编排 MariaDB
-
使用ConfigMap定义MariaDB所需的环境变量,yaml文件命名:maria-cm.yml
apiVersion: v1 kind: ConfigMap metadata: name: maria-cm data: DATABASE: 'db' USER: 'wp' PASSWORD: '123456' ROOT_PASSWORD: '123456'
-
定义MariaDB的Pod,yaml文件命名:maria-pod.yml
apiVersion: v1 kind: Pod metadata: name: maria-pod labels: app: wordpress role: database spec: containers: - image: mariadb:10 name: maria imagePullPolicy: IfNotPresent ports: - containerPort: 3306 envFrom: - prefix: 'MARIADB_' configMapRef: name: maria-cm
-
创建上述两个对象
# 创建ConfigMap对象 kubectl apply -f maria-cm.yml #创建MariaDB的Pod对象 kubectl apply -f maria-pod.yml
启动MariaDB时,可能会出现ImagePullBackOff的问题,使用kubectl logs maria-pod查看日志,一般是因为镜像下载失败。可以选择等待镜像下载,或使用docker pull mariadb:10
-
查看maria-db的ip地址,这个地址要用在后面的WordPress的配置中
[chris@iZbp15bwxq9nnodnc5a40nZ ~]$ kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES maria-pod 1/1 Running 0 18m 172.17.0.3 minikube <none> <none>
编排 WordPress
-
先用 ConfigMap 定义它的环境变量,文件名:wp-cm.yml
apiVersion: v1 kind: ConfigMap metadata: name: wp-cm data: HOST: '172.17.0.3' USER: 'wp' PASSWORD: '123456' NAME: 'db'
ConfigMap 里的“HOST”字段,必须是maria-pod 的 IP 地址,否则无法正常连接数据库。
-
编写 WordPress 的 YAML 文件,文件名:wp-pod.yml
apiVersion: v1 kind: Pod metadata: name: wp-pod labels: app: wordpress role: website spec: containers: - image: wordpress:5 name: wp-pod imagePullPolicy: IfNotPresent ports: - containerPort: 80 envFrom: - prefix: 'WORDPRESS_DB_' configMapRef: name: wp-cm
启动wp-pod时,建议先手动拉取wordpress镜像,若无法正常启动,则删除后,重启。删除命令:kubectl delete -f wp-pod.yml
-
查看wp-pod状态
[chris@iZbp15bwxq9nnodnc5a40nZ ~]$ kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES maria-pod 1/1 Running 0 60m 172.17.0.3 minikube <none> <none> wp-pod 1/1 Running 0 4m32s 172.17.0.6 minikube <none> <none> [chris@iZbp15bwxq9nnodnc5a40nZ ~]$
-
WordPress Pod 映射端口,使之对外可见:
[chris@iZbp15bwxq9nnodnc5a40nZ ~]$ kubectl port-forward wp-pod 8080:80 & [1] 1388029 [chris@iZbp15bwxq9nnodnc5a40nZ ~]$ Forwarding from 127.0.0.1:8080 -> 80
& 符号,让端口转发工作在后台进行
创建反向代理 Nginx
-
因为WordPress 使用 URL 重定向,直接访问“8080”会导致跳转故障,为让网站正常工作,应该在 Kubernetes 之外启动 Nginx 反向代理,保证外界看到的仍然是“80”端口号。
-
拉取nginx镜像
[chris@iZbp15bwxq9nnodnc5a40nZ ~]$ docker pull nginx Using default tag: latest latest: Pulling from library/nginx a2abf6c4d29d: Pull complete a9edb18cadd1: Pull complete 589b7251471a: Pull complete 186b1aaa4aa6: Pull complete b4df32aa5a72: Pull complete a0bcbecc962e: Pull complete Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
-
nginx配置文件
server { listen 80; default_type text/html; location / { proxy_http_version 1.1; proxy_set_header Host $host; proxy_pass http://127.0.0.1:8080; } }
-
用 docker run -v 命令加载这个配置文件,以容器的方式启动这个 Nginx 代理:
docker run -d --rm --net=host -v /home/chris/nginx/proxy.conf:/etc/nginx/conf.d/default.conf nginx:alpine
-
nginx启动后,访问:http://47.99.64.4/
上面的ip是我的临时服务器的ip,要换成你自己的ip地址。
大功造成
更多推荐
所有评论(0)