首页 > 其他分享 >k8s构建LNMP

k8s构建LNMP

时间:2022-08-19 21:58:12浏览次数:59  
标签:php local LNMP nginx wordpress mysql 构建 k8s root

构建nginx资源

       创建nginx配置文件

root@deploy:/dockerfile/project/nginx# cat nginx.conf 
user  nginx;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#daemon off;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    client_max_body_size 10M;
    client_body_buffer_size 16k;
    gzip  on;


    server {
        listen       80;
        server_name  blogs.magedu.net;

        location / {
            root    html/wordpress;
            index   index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           html/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
        }

    }

}

 

       创建nginx运行脚本

root@deploy:/dockerfile/project/nginx# cat run_nginx.sh 
#!/bin/bash
/usr/local/nginx/sbin/nginx
tail -f /usr/local/nginx/logs/access.log /usr/local/nginx/logs/error.log

 

       创建nginx业务镜像Dockerfile

root@deploy:/dockerfile/project/nginx# cat Dockerfile 
FROM harbor.cncf.net/web/nginx:1.20.2 

ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD run_nginx.sh /usr/local/nginx/sbin/run_nginx.sh
RUN mkdir /usr/local/nginx/html/wordpress
RUN chown nginx.nginx /usr/local/nginx/html/wordpress/ -R

EXPOSE 80 443

CMD ["/usr/local/nginx/sbin/run_nginx.sh"]

 

       创建构建脚本

root@deploy:/dockerfile/project/nginx# cat build-command.sh 
#!/bin/bash
TAG=$1
docker build -t  harbor.cncf.net/project/wordpress-nginx:${TAG} .

docker push  harbor.cncf.net/project/wordpress-nginx:${TAG}

 

构建php镜像

       创建Dockerfile

root@deploy:/dockerfile/project/php# cat Dockerfile 
FROM harbor.cncf.net/os/centos:7.9

RUN useradd nginx

RUN yum install -y  https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm && yum install  php56-php-fpm php56-php-mysql -y 
ADD www.conf /opt/remi/php56/root/etc/php-fpm.d/www.conf
ADD run_php.sh /usr/local/bin/run_php.sh
EXPOSE 9000

CMD ["/usr/local/bin/run_php.sh"]

 

       创建php-fpm容器运行脚本

root@deploy:/dockerfile/project/php# cat run_php.sh 
#!/bin/bash
#echo "nameserver 10.20.254.254" > /etc/resolv.conf

/opt/remi/php56/root/usr/sbin/php-fpm
#/opt/remi/php56/root/usr/sbin/php-fpm --nodaemonize
tail -f /etc/hosts

 

       创建php-fpm运行配置文件

root@deploy:/dockerfile/project/php# grep -Ev "^;|^$" www.conf 
[www]
user = nginx
group = nginx
listen = 0.0.0.0:9000
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /opt/remi/php56/root/var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /opt/remi/php56/root/var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /opt/remi/php56/root/var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /opt/remi/php56/root/var/lib/php/wsdlcache

 

       创建镜像构建脚本

root@deploy:/dockerfile/project/php# cat build-command.sh
#!/bin/bash
TAG=$1
docker build -t harbor.cncf.net/project/wordpress-php-5.6:${TAG} .
docker push harbor.cncf.net/project/wordpress-php-5.6:${TAG}

 

创建wordpress资源

       准备nfs共享目录

root@harbor:/data/k8sdata# mkdir /data/k8sdata/wordpress
root@harbor:/data/k8sdata/wordpress# cat /etc/exports
/data/volumes2 *(rw,sync,no_root_squash)
root@harbor:/data/k8sdata/wordpress# exportfs -r

 

       准备wordpress站点文件

       下载wordpress

root@harbor:/data/k8sdata/wordpress# wget https://cn.wordpress.org/wordpress-5.0.16-zh_CN.tar.gz
root@harbor:/data/k8sdata/wordpress# tar xf wordpress-5.0.16-zh_CN.tar.gz 
root@harbor:/data/k8sdata/wordpress# mv wordpress/* .
root@harbor:/data/k8sdata/wordpress# rm -fr wordpress
root@harbor:/data/k8sdata/wordpress# rm -f wordpress-5.0.16-zh_CN.tar.gz

 

       获取nginx用户uid和gid并给与nfs站点目录相应权限

       k8s节点查看用户属主属组对应uid和gid号

root@deploy:/dockerfile/project/php# kubectl exec -it wordpress-app-deployment-78cfdc88f7-psvc4 -c wordpress-app-php -- id nginx
uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)
root@deploy:/dockerfile/project/php# kubectl exec -it wordpress-app-deployment-78cfdc88f7-psvc4 -c wordpress-app-nginx -- id nginx
uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)

 

       nfs-server设置查看NFS共享目录赋予wordpress容器用户和组权限

root@harbor:/data/k8sdata# chown 1000.1000 -R wordpress

 

       创建namespace命名空间

root@deploy:/dockerfile/project/wordpress# kubectl create ns wordpress
namespace/wordpress created

 

       准备wordpress yaml资源文件

root@deploy:/dockerfile/project# cat wordpress/wordpress.yaml 
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-deployment
  namespace: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress-app
  template:
    metadata:
      labels:
        app: wordpress-app
    spec:
      containers:
      - name: wordpress-app-nginx
        image: harbor.cncf.net/project/wordpress-nginx:1.0 
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        - containerPort: 443
          protocol: TCP
          name: https
        volumeMounts:
        - name: wordpress
          mountPath: /usr/local/nginx/html/wordpress
          readOnly: false

      - name: wordpress-app-php
        image: harbor.cncf.net/project/wordpress-php-5.6:1.0
        imagePullPolicy: Always
        ports:
        - containerPort: 9000
          protocol: TCP
          name: http
        volumeMounts:
        - name: wordpress
          mountPath: /usr/local/nginx/html/wordpress
          readOnly: false

      volumes:
      - name: wordpress
        nfs:
          server: 192.168.100.15
          path: /data/k8sdata/wordpress 

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-spec
  namespace: wordpress
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30031
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
    nodePort: 30033
  selector:
    app: wordpress-app

 

       查看资源创建

root@deploy:/dockerfile/project# kubectl get pods
root@deploy:/dockerfile/project# kubectl get svc

 

  配置数据库验证数据库地址

       数据库mysql主库全名称DNS地址:mysql-0.mysql.mysql-test.svc.cluster.local,pod名称.service名称.名称空间.svc.集群名称

root@deploy:/dockerfile/project/wordpress# kubectl exec -it wordpress-app-deployment-78cfdc88f7-psvc4 -c wordpress-app-nginx -- bash -c "ping mysql-0.mysql.mysql-test.svc.cluster.local"
PING mysql-0.mysql.mysql-test.svc.cluster.local (10.200.104.8) 56(84) bytes of data.
64 bytes from mysql-0.mysql.mysql-test.svc.cluster.local (10.200.104.8): icmp_seq=1 ttl=62 time=0.236 ms
64 bytes from mysql-0.mysql.mysql-test.svc.cluster.local (10.200.104.8): icmp_seq=2 ttl=62 time=0.527 ms
64 bytes from mysql-0.mysql.mysql-test.svc.cluster.local (10.200.104.8): icmp_seq=3 ttl=62 time=0.705 ms
^C
--- mysql-0.mysql.mysql-test.svc.cluster.local ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2010ms
rtt min/avg/max/mdev = 0.236/0.489/0.705/0.193 ms

 

数据库添加wordpress账户权限

root@deploy:~# kubectl exec -it -n mysql-test mysql-0 -c mysql -- bash
root@mysql-0:/# mysql -u root -p123456 -P3360
mysql> grant all on wordpress.* to "wordpress"@"%" identified by 'wordpress123456';
mysql> create database wordpress;

      

  客户端访问wordpress

    

     由于mysql端口是非默认3306,所以需要修改wordpress数据库连接php代码文件

       输入数据库名称 wordpress

       输入用户名 wordpress

       输入密码 wordpress123456

       输入数据库主机为k8s 创建的mysql主库全名称:端口号mysql-0.mysql.mysql-test.svc.cluster.local:3360

      

  返回浏览器刷新页面开始配置页面信息

 

      

  登录账户

      

  创建完成

 

标签:php,local,LNMP,nginx,wordpress,mysql,构建,k8s,root
From: https://www.cnblogs.com/punchlinux/p/16603346.html

相关文章

  • docker构建pytorch
    配置拉取镜像(devel版本有nvcc和cudnn)sudodockerpullpytorch/pytorch:1.6.0-cuda10.1-cudnn7-devel构建容器阿立dockerrun-it--gpusall--namelipytorch1.6-......
  • 无网络使用本地maven仓库配置,构建上传sonarqube进行代码检查
    #前提需要本地仓库下存在项目的所有依赖包1.mavensettings.xml文件配置。如果idea和本地mvn环境使用的settings.xml路径不一致。则需要同步修改。<!--#前提需要本......
  • 基于Jenkins+k8s+Git等技术构建DeOps平台
    一、DeOps简介1.什么是DeOps?   1.1敏捷开发提高开发效率,及时跟进用户需求,缩短开发周期。敏捷开发包括编写代码和构建代码两个阶段,可以使用git或者svn......
  • k8s弹性扩缩容
    1、查询deploy副本数,ready数表示副本数kubectlgetdeploy  2、通过命令直接扩容或者缩容,--replicas=1表示把my-dep缩容到副本数1,--replicas=4表示把my-dep扩容到......
  • 如何实现跨数百个K8s集群的管理
    随着云原生进程的加快,传统大型业务应用系统也走上了微服务化之路。服务功能分解是应用微服务化的巨大挑战,对于大型应用系统来说更是如此。不仅如此,虽然K8s已经实现了很多......
  • K8S服务滚动升级
    对于Kubernetes集群来说,一个service可能有多个pod,滚动升级(Rollingupdate)就是指每次更新部分Pod,而不是在同一时刻将该Service下面的所有Podshutdown,然后去更新(例如rep......
  • k8s上使用statefulset搭建es集群
    环境要求:k8s多节点集群,最好是kubespherek8s集群,k8s需要使用StorageClass实现:vim es.yamlapiVersion:policy/v1beta1kind:PodDisruptionBudgetmetadata:name:ela......
  • 关机一段时间后k8s无法启动
    目录重装前清除环境方法:重装后pod-coredns一直处于ContainerCreating状态:重启之后k8s集群居然起不起来了解决方法:重装重装前清除环境方法:kubeadmresetsystemctlsto......
  • k8s 使用 kubeadm init 初始化失败日志一直提示"Error getting node" err="node \"ma
    系统版本CentOSLinuxrelease7.9.2009(Core)内核版本Linux3.10.0-1160.76.1.el7.x86_64x86_64docker版本Dockerversion20.10.17,build100c701k8......
  • k8s中ingress,service,depoyment,pod如何关联
    k8s中ingress,service,depoyment,pod通过label标签名称来识别关联,它们的label name一定是一样的。1、查询发布的deploymet控制器[root@k8s-master1~]#kubectlgetdeplo......