Dockerfile-service
cat > Dockerfile-service << EOF FROM centos:centos7.9.2009 MAINTAINER qyl WORKDIR /opt COPY wordpress-6.2-zh_CN.zip ./ COPY yum.sh /opt/ RUN sh /opt/yum.sh RUN yum clean all &&\ yum install -y nginx unzip &&\ unzip ./wordpress-6.2-zh_CN.zip # 安装php7.4环境 RUN yum install -y php74-php-devel php74-php php74-php-cli php74-php-common php74-php-gd php74-php-ldap php74-php-mbstring php74-php-mcrypt php74-php-pdo php74-php-mysqlnd php74-php-fpm php74-php-opcache php74-php-pecl-redis php74-php-pecl-mongodb php74-php-fpm #修改 php-fpm 的用户和组为nginx RUN sed -i 's/^user\ =\ apache/user\ =\ nginx/g' /etc/opt/remi/php74/php-fpm.d/www.conf && \ sed -i 's/^group\ =\ apache/group\ =\ nginx/g' /etc/opt/remi/php74/php-fpm.d/www.conf &&\ php74 -v RUN rm -rf /usr/share/nginx/html/* &&\ cp -rf ./wordpress/* /usr/share/nginx/html/ COPY default.conf /etc/nginx/conf.d/default.conf COPY wp-config.php /usr/share/nginx/html/ EXPOSE 80 # 为了进入特权模式,所要运行的环境 CMD /usr/sbin/init EOF
构建镜像
docker build -t wp-service:v1 -f Dockerfile-service .
docker compose 编排
使用上述构建的镜像编排容器
cat > docker-compose.yaml << EOF version: '3' services: mariadb: image: wp-mariadb:v1 container_name: db ports: - 3306:3306 wordpress: image: wp-service:v1 container_name: wordpress privileged: true # 开启容器特权模式 ports: - 80:80 links: - mariadb # 服务名 depends_on: - mariadb EOF
compose 启动容器
[root@master01 docker]# docker compose up -d [+] Running 3/3 ✔ Network docker_default Created 0.1s ✔ Container db Started 0.6s ✔ Container wordpress Started 1.3s [root@master01 docker]# docker compose ps NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS db wp-mariadb:v1 "mysqld_safe --user=…" mariadb About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp wordpress wp:v1 "/bin/sh -c /usr/sbi…" wordpress About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp
进入 wordpress 容器启动 php-fpm、nginx
因为这里使用的 yum 源安装的(其他方式也可以),没有php-fpm二进制命令启动 php-fpm 服务,而使用 systemctl 启动服务需要权限,也就是 compose 里面的
privileged: true
字段和 Dockerfile-service 里面的CMD /usr/sbin/init
,这样就能保证在容器使用systemctl启动服务了
docker exec -it wordpress bash systemctl restart php74-php-fpm systemctl restart nginx
浏览器访问 80 端口,安装 wordpress
K8s部署
svc
使用 svc 暴露服务
[root@master01 wordpress]# cat service.yaml apiVersion: v1 kind: Service metadata: labels: app: mariadb name: mariadb spec: clusterIP: None selector: app: mariadb type: ClusterIP ports: - port: 3306 --- apiVersion: v1 kind: Service metadata: name: wordpress labels: app: wordpress spec: ports: - port: 80 selector: app: wordpress type: NodePort
deploy
包含 mariadb 和 wordpress 服务
[root@master01 wordpress]# cat deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app: mariadb name: mariadb spec: replicas: 1 selector: matchLabels: app: mariadb template: metadata: labels: app: mariadb spec: containers: - image: wp-mariadb:v1 name: mariadb --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: wordpress name: wordpress spec: replicas: 1 selector: matchLabels: app: wordpress template: metadata: labels: app: wordpress spec: containers: - image: wp-service:v1 name: wordpress securityContext: privileged: true
进入WordPress pod 启动 php-fpm、nginx 服务
[root@master01 wordpress]# kubectl get pod NAME READY STATUS RESTARTS AGE mariadb-5cd9b8655d-cq8nd 1/1 Running 0 4s wordpress-744964c4cb-sk47g 1/1 Running 0 4s [root@master01 wordpress]# kubectl exec -it pod/wordpress-744964c4cb-sk47g -- bash [root@wordpress-744964c4cb-sk47g opt]# systemctl restart php74-php-fpm [root@wordpress-744964c4cb-sk47g opt]# systemctl restart nginx
浏览器访问
[root@master01 wordpress]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21d mariadb ClusterIP None <none> 3306/TCP 10h wordpress NodePort 10.97.150.148 <none> 80:32214/TCP 10h
✊ 最后
👏 我是秋意临,欢迎大家一键三连、加入云社区
👋 我们下期再见(⊙o⊙)!!!