一,编辑wordpress的mysql服务
[root@\ k8s-m-01~]# vim wordpress.yaml
apiVersion: v1
kind: Namespace
metadata:
name: mysql
---
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: mysql
spec:
ports:
- name: http
port: 3306
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
[root@\ k8s-m-01~]# kubectl apply -f wordpress.yaml
二,准备配置文件
1,wordpress
[root@\ k8s-m-01~]# ll /wordpress/
drwxr-xr-x. 2 root root 78 Apr 1 17:57 nginx
drwxr-xr-x. 2 root root 42 Apr 1 18:00 php
drwxr-xr-x. 5 root root 4096 Apr 1 17:45 wordpress
[root@\ k8s-m-01~]# ll /wordpress/wordpress/
-rw-r--r--. 1 root root 405 Apr 1 17:39 index.php
-rw-r--r--. 1 root root 19915 Apr 1 17:39 license.txt
-rw-r--r--. 1 root root 7345 Apr 1 17:39 readme.html
-rw-r--r--. 1 root root 7165 Apr 1 17:39 wp-activate.php
drwxr-xr-x. 9 root root 4096 Apr 1 17:39 wp-admin
-rw-r--r--. 1 root root 351 Apr 1 17:39 wp-blog-header.php
-rw-r--r--. 1 root root 2328 Apr 1 17:39 wp-comments-post.php
-rw-r--r--. 1 root root 2908 Apr 1 17:45 wp-config.php
-rw-r--r--. 1 root root 2913 Apr 1 17:39 wp-config-sample.php
drwxr-xr-x. 4 root root 52 Apr 1 17:39 wp-content
-rw-r--r--. 1 root root 3939 Apr 1 17:39 wp-cron.php
drwxr-xr-x. 25 root root 8192 Apr 1 17:39 wp-includes
-rw-r--r--. 1 root root 2496 Apr 1 17:39 wp-links-opml.php
-rw-r--r--. 1 root root 3313 Apr 1 17:39 wp-load.php
-rw-r--r--. 1 root root 44993 Apr 1 17:39 wp-login.php
-rw-r--r--. 1 root root 8509 Apr 1 17:39 wp-mail.php
-rw-r--r--. 1 root root 21125 Apr 1 17:39 wp-settings.php
-rw-r--r--. 1 root root 31328 Apr 1 17:39 wp-signup.php
-rw-r--r--. 1 root root 4747 Apr 1 17:39 wp-trackback.php
-rw-r--r--. 1 root root 3236 Apr 1 17:39 xmlrpc.php
# 复制wordpress连接数据库的配置文件
[root@\ k8s-m-01/file/wordpress]# cp wp-config-sample.php wp-config.php
# 修改连接数据库的信息
[root@\ k8s-m-01/file/wordpress]# vim wp-config.php
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'root' );
/** MySQL database password */
define( 'DB_PASSWORD', '123456' );
/** MySQL hostname */
define( 'DB_HOST', 'mysql.mysql.svc.cluster.local' );
#压缩
[root@\ k8s-m-01/wordpress]# cd wordpress/
[root@\ k8s-m-01/wordpress/wordpress]# tar -zcf wordpress.tar.gz ./
[root@\ k8s-m-01/wordpress/wordpress]# ll
total 15576
-rw-r--r--. 1 root root 405 Apr 1 17:39 index.php
-rw-r--r--. 1 root root 19915 Apr 1 17:39 license.txt
-rw-r--r--. 1 root root 7345 Apr 1 17:39 readme.html
-rw-r--r--. 1 root root 15733555 Apr 1 18:50 wordpress.tar.gz
... ...
# 复制到nginx和php下,构建镜像需要
[root@\ k8s-m-01/wordpress/wordpress]# cp wordpress.tar.gz /wordpress/nginx/
[root@\ k8s-m-01/wordpress/wordpress]# cp wordpress.tar.gz /wordpress/php/
PS:
/** MySQL hostname */
define( 'DB_HOST', 'mysql.mysql.svc.cluster.local' );
由来
[root@\ k8s-m-01~]# kubectl run test -it --rm --image=busybox:1.28.3
If you don't see a command prompt, try pressing enter.
/ # nslookup mysql.mysql
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: mysql.mysql
Address 1: 10.96.236.95 mysql.mysql.svc.cluster.local
2,nginx
1) 准备配置文件
[root@\ k8s-m-01~]# mkdir -p /wordpress/{nginx,php}
[root@\ k8s-m-01/wordpress/nginx]# ll
total 16
-rw-r--r--. 1 root root 464 Apr 1 10:43 default.conf
-rw-r--r--. 1 root root 267 Apr 1 17:49 Dockfile
-rw-r--r--. 1 root root 641 Mar 24 15:18 nginx.conf
-rw-r--r--. 1 root root 398 Mar 24 15:12 nginx.repo
# 准备官方源
[root@\ k8s-m-01/wordpress/nginx]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 配置文件修改用户
[root@\ k8s-m-01/wordpress/nginx]# vim nginx.conf
user www;
# nginx
[root@\ k8s-m-01/wordpress/nginx]# vim default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
2) 构建镜像
[root@\ k8s-m-01/wordpress/nginx]# vim Dockerfile
FROM centos:7
ADD nginx.repo /etc/yum.repos.d/
RUN yum install -y nginx
ADD default.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/
WORKDIR /usr/share/nginx/
EXPOSE 80 443
RUN useradd www
ADD wordpress.tar.gz /usr/share/nginx/html/
CMD nginx -g "daemon off;"
[root@\ k8s-m-01/wordpress/nginx]# docker build -t urbanezxx/nginx:wordpress ./
... ...
Successfully built beb17c7a3e5d
Successfully tagged urbanezxx/nginx:wordpress
# 上传到仓库(以备使用)
[root@\ k8s-m-01/wordpress/nginx]# docker push urbanezxx/nginx:wordpress
# 验证文件是否存在
[root@\ k8s-m-01/wordpress/nginx]# docker run -it --rm urbanezxx/nginx:wordpress bash
[root@dc42b893a9d8 nginx]# ll html/
-rw-r--r--. 1 root root 494 Oct 29 15:25 50x.html
-rw-r--r--. 1 root root 612 Oct 29 15:25 index.html
-rw-r--r--. 1 root root 405 Apr 1 09:39 index.php
-rw-r--r--. 1 root root 19915 Apr 1 09:39 license.txt
-rw-r--r--. 1 root root 7345 Apr 1 09:39 readme.html
-rw-r--r--. 1 root root 7165 Apr 1 09:39 wp-activate.php
drwxr-xr-x. 9 root root 4096 Apr 1 09:39 wp-admin
-rw-r--r--. 1 root root 351 Apr 1 09:39 wp-blog-header.php
-rw-r--r--. 1 root root 2328 Apr 1 09:39 wp-comments-post.php
-rw-r--r--. 1 root root 2913 Apr 1 09:39 wp-config-sample.php
-rw-r--r--. 1 root root 2908 Apr 1 09:45 wp-config.php
drwxr-xr-x. 4 root root 52 Apr 1 09:39 wp-content
-rw-r--r--. 1 root root 3939 Apr 1 09:39 wp-cron.php
drwxr-xr-x. 25 root root 8192 Apr 1 09:39 wp-includes
-rw-r--r--. 1 root root 2496 Apr 1 09:39 wp-links-opml.php
-rw-r--r--. 1 root root 3313 Apr 1 09:39 wp-load.php
-rw-r--r--. 1 root root 44993 Apr 1 09:39 wp-login.php
-rw-r--r--. 1 root root 8509 Apr 1 09:39 wp-mail.php
-rw-r--r--. 1 root root 21125 Apr 1 09:39 wp-settings.php
-rw-r--r--. 1 root root 31328 Apr 1 09:39 wp-signup.php
-rw-r--r--. 1 root root 4747 Apr 1 09:39 wp-trackback.php
-rw-r--r--. 1 root root 3236 Apr 1 09:39 xmlrpc.php
3,php
1) 准备配置文件
[root@\ k8s-m-01/wordpress/php]# ll
-rw-r--r--. 1 root root 162 Apr 1 17:52 Dockfile
-rw-r--r--. 1 root root 23007 Mar 25 15:43 php-fpm.conf
# 修改用户
[root@\ k8s-m-01/wordpress/php]# vim php-fpm.conf
user = www
group = www
2) 准备镜像
# 构建镜像
[root@\ k8s-m-01/wordpress/php]# vim Dockerfile
FROM urbanezxx/php:php-fpm-mysql
RUN useradd www
ADD php-fpm.conf /usr/local/etc/
EXPOSE 9000
ADD wordpress.tar.gz /usr/share/nginx/html/
CMD ["php-fpm","-F"]
#先拉取基础镜像(已经编译好的php)
[root@\ k8s-m-01/wordpress/php]# docker pull urbanezxx/php:v1-fpm-mysql
[root@\ k8s-m-01/wordpress/php]# docker build -t urbanezxx/php:wordpress .
# 上传到仓库(以备使用)
[root@\ k8s-m-01/wordpress/nginx]# docker push urbanezxx/php:wordpress
三,完整版
[root@\ k8s-m-01~]# vim wordpress.yaml
apiVersion: v1
kind: Namespace
metadata:
name: mysql
---
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: mysql
spec:
ports:
- name: http
port: 3306
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
---
apiVersion: v1
kind: Namespace
metadata:
name: wordpress
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: wordpress
type: NodePort
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: wordpress
namespace: wordpress
spec:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: php
image: urbanezxx/php:wordpress
- name: nginx
image: urbanezxx/nginx:wordpress
四,执行及相关查看命令
[root@\ k8s-m-01~]# kubectl apply -f wordpress.yaml
# 查看服务状态
[root@\ k8s-m-01~]# kubectl get svc -n mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.96.236.95 <none> 3306/TCP 7h24m
[root@\ k8s-m-01~]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NodePort 10.109.144.61 <none> 80:32053/TCP 7h25m
# 查看容器状态,要加上-n mysql ,不然默认是default
[root@\ k8s-m-01~]# kubectl get pod -n wordpress
NAME READY STATUS RESTARTS AGE
wordpress-554bb57b5d-zkwms 2/2 Running 0 63s
[root@\ k8s-m-01~]# kubectl get pod -n mysql
NAME READY STATUS RESTARTS AGE
name-mysql-56f8cdb464-xps7f 1/1 Running 0 10m
# 查看资源详情
[root@\ k8s-m-01~]# kubectl describe pod -n wordpress
Name: wordpress-554bb57b5d-zkwms
Namespace: wordpress
Priority: 0
Node: k8s-m-01/192.168.15.31
Start Time: Thu, 01 Apr 2021 19:24:22 +0800
Labels: app=wordpress
pod-template-hash=554bb57b5d
Annotations: <none>
Status: Running
IP: 10.244.0.18
IPs:
IP: 10.244.0.18
Controlled By: ReplicaSet/wordpress-554bb57b5d
Containers:
php:
Container ID: docker://4686145dc734643ddea844ef086a5619148f6c22f8e316130592fdeb721b2849
Image: urbanezxx/php:wordpress
Image ID: docker-pullable://urbanezxx/php@sha256:c6edac270f94f8652a5a5cbbdbb590714faa5cce3bea22775b0f1ac5baba8269
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 01 Apr 2021 19:24:22 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-pjpsw (ro)
nginx:
Container ID: docker://2cb1254ac0061f1df4fbbc487638d1b99784e4a3fc6a1d76be572dbea3c4a16d
Image: urbanezxx/nginx:wordpress
Image ID: docker-pullable://urbanezxx/nginx@sha256:17260777f1724ffcfce94240f74feaecc215695a1925980ad911eec46b6aa0f7
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 01 Apr 2021 19:24:23 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-pjpsw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-pjpsw:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-pjpsw
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m27s default-scheduler Successfully assigned wordpress/wordpress-554bb57b5d-zkwms to k8s-m-01
Normal Pulled 2m27s kubelet Container image "urbanezxx/php:wordpress" already present on machine
Normal Created 2m27s kubelet Created container php
Normal Started 2m27s kubelet Started container php
Normal Pulled 2m27s kubelet Container image "urbanezxx/nginx:wordpress" already present on machine
Normal Created 2m27s kubelet Created container nginx
Normal Started 2m26s kubelet Started container nginx
五,创建数据库
#创建数据库wordpress
[root@\ k8s-m-01~]# kubectl get pod -n mysql
NAME READY STATUS RESTARTS AGE
name-mysql-56f8cdb464-xps7f 1/1 Running 0 14m
[root@\ k8s-m-01~]# kubectl exec -it -n mysql name-mysql-56f8cdb464-xps7f -- bash
root@name-mysql-56f8cdb464-xps7f:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
ingress增加域名访问
[root@\ k8s-m-01~]# vim wordpress.yaml
apiVersion: v1
kind: Namespace
metadata:
name: mysql
---
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: mysql
spec:
ports:
- name: http
port: 3306
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
---
apiVersion: v1
kind: Namespace
metadata:
name: wordpress
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: wordpress
clusterIP: None
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: wordpress
namespace: wordpress
spec:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: php
image: urbanezxx/php:wordpress
- name: nginx
image: urbanezxx/nginx:wordpress
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wordpress-name
namespace: wordpress
spec:
rules:
- host: www.wp.com
http:
paths:
- path: /
backend:
serviceName: wordpress
servicePort: 80
[root@\ k8s-m-01~]# kubectl apply -f wordpress.yaml
[root@\ k8s-m-01~]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NodePort 10.109.144.61 <none> 80:32053/TCP 29h
[root@\ k8s-m-01~]# kubectl get ingress -n wordpress
NAME CLASS HOSTS ADDRESS PORTS AGE
wordpresss <none> wp.nginx.com 192.168.15.33 80 38m
修改hosts,访问
https 访问
# 创建证书目录
[root@\ k8s-m-01~]# mkdir zs
[root@\ k8s-m-01~]# cd zs/
# 生成证书
[root@\ k8s-m-01~/zs]# openssl genrsa -out tls.key 2048
Generating RSA private key, 2048 bit long modulus
.........................+++
......................................................................+++
e is 65537 (0x10001)
[root@\ k8s-m-01~/zs]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=ShangHai/L=ShangHai/O=Ingress/CN=www.test.com
[root@\ k8s-m-01~/zs]# ll
-rw-r--r--. 1 root root 1289 Apr 2 17:48 tls.crt
-rw-r--r--. 1 root root 1675 Apr 2 17:48 tls.key
# 创建Ingress
[root@\ k8s-m-01~/zs]# kubectl -n wordpress create secret tls ingress-tls --cert=tls.crt --key=tls.key
secret/ingress-tls created
[root@\ k8s-m-01~]# vim wordpress.yaml
... ...
增加
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wordpress-name
namespace: wordpress
spec:
tls:
- secretName: ingress-tls
rules:
- host: www.wp.com
http:
paths:
- path: /
backend:
serviceName: wordpress
servicePort: 80
servicePort: 443
完整版配置清单
apiVersion: v1
kind: Namespace
metadata:
name: mysql
---
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: mysql
spec:
ports:
- name: http
port: 3306
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
---
apiVersion: v1
kind: Namespace
metadata:
name: wordpress
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
ports:
- name: http
port: 80
targetPort: 80
- name: https
port: 443
targetPort: 443
selector:
app: wordpress
clusterIP: None
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: wordpress
namespace: wordpress
spec:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: php
image: urbanezxx/php:wordpress
- name: nginx
image: urbanezxx/nginx:wordpress
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wordpress-name
namespace: wordpress
spec:
tls:
- secretName: ingress-tls
rules:
- host: www.wp.com
http:
paths:
- path: /
backend:
serviceName: wordpress
servicePort: 80
servicePort: 443
# 查看端口
[root@\ k8s-m-01~]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.102.254.20 <none> 80:30654/TCP,443:31066/TCP 144m
ingress-nginx-controller-admission ClusterIP 10.111.163.84 <none> 443/TCP 144m