一、環境介紹
k8s版本: 1.15.2
存儲: 阿里雲NAS
測試代碼: wordpress
二、下載wordpress和創建好數據庫等
1、下載wordpress
wget https://cn.wordpress.org/latest-zh_CN.zip
2、創建數據庫(數據庫我使用yum下載的,數據庫盡量不要部署在k8s集群中)
create database wordpress DEFAULT CHARACTER SET utf8; grant all on wordpress.* to 'wordpress'@'%' identified by '123456';
3、把wordpress代碼放入到NAS存儲中
mkdir /data -p mount -t nfs -o vers=4,minorversion=0,noresvport 12XXXXXXXxx.cn-hongkong.nas.aliyuncs.com:/ /data mv wordpress /data/
三、寫dockerfile和構建鏡像(我這自己寫的nginx鏡像,掛載配置或者使用secret的方法也能更改配置)
mkdir -p Dockerfile [root@k8s-m Dockerfile]# cat default.conf server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { root /var/www/html; fastcgi_pass php-svc.default.svc.cluster.local:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } [root@k8s-m Dockerfile]# cat Dockerfile FROM nginx:1.15.4-alpine LABEL maintainer="zhang 1232@qq.com" COPY default.conf /etc/nginx/conf.d/ EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] #構建鏡像(沒有鏡像倉庫的話,要把鏡像導入到其它服務器上) [root@k8s-m Dockerfile]# docker build -t mynginx:2.0 ./ #查看鏡像 [root@k8s-m Dockerfile]# docker images|grep mynginx mynginx 2.0 2fd9a2724422 2 hours ago 17.7MB
四、配置nginx和php
1、創建與導入php的svc和deploy
[root@k8s-m ~]# cat php.yaml apiVersion: v1 kind: Service metadata: name: php-svc spec: selector: name: php ports: - port: 9000 name: http-php targetPort: 9000 protocol: TCP --- apiVersion: apps/v1 kind: Deployment metadata: name: my-php-deploy spec: replicas: 3 selector: matchLabels: name: php template: metadata: labels: name: php spec: containers: - name: php image: php:7.2-fpm ports: - name: http-php containerPort: 9000 volumeMounts: - name: php-code mountPath: /var/www/html/ volumes: - name: php-code nfs: path: /wordpress/ server: 12xxxxxxxxx.cn-hongkong.nas.aliyuncs.com
2、創建與導入nginx的svc和deploy
[root@k8s-m ~]# cat nginx-deploy.yaml apiVersion: v1 kind: Service metadata: name: nginx-svc spec: selector: name: nginx ports: - port: 80 name: http targetPort: 80 protocol: TCP --- apiVersion: apps/v1 kind: Deployment metadata: name: my-nginx-deploy spec: replicas: 3 selector: matchLabels: name: nginx template: metadata: labels: name: nginx spec: containers: - name: nginx image: mynginx:2.0 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html nfs: path: /wordpress/ server: 124xxxxxxxxxxxxx-hongkong.nas.aliyuncs.com
3、查看
[root@k8s-m ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h15m nginx-svc ClusterIP 10.101.81.167 <none> 80/TCP 99m php-svc ClusterIP 10.111.89.228 <none> 9000/TCP 99m [root@k8s-m ~]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE my-nginx-deploy 3/3 3 3 99m my-php-deploy 3/3 3 3 100m
4、創建Ingress訪問
[root@k8s-m ~]# cat wordpress-ingress.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-nginx spec: rules: - host: haha.zhang.com http: paths: - backend: serviceName: nginx-svc servicePort: 80 [root@k8s-m ~]# kubectl apply -f wordpress-ingress.yaml ingress.extensions/ingress-nginx created
5、訪問測試
五、PHP擴展安裝
php容器中的ini擴展文件路徑:/usr/local/etc/php/conf.d/
1、進入php容器中
[root@node1 ~]# docker run -it --name php-gd php:7.2-fpm bash
2、下載依賴
apt-get update && apt-get install libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev -y
3、安裝擴展(例如GD)
docker-php-ext-configure gd #安裝 docker-php-ext-install gd #啟用 docker-php-ext-enable gd
4、將容器保存為新的鏡像
[root@node1 ~]# docker commit -p php-gd php-gd:1.0 sha256:c562ad539630b3c5eb6888f0b7bac937d9d3af1d39de118106c5e6ca30a02ebd [root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE php-gd 1.0 c562ad539630 4 seconds ago 426MB
5、運行新php容器測試
[root@node1 ~]# docker run -it --rm php-gd:1.0 bash root@9e7fa8e57db7:/var/www/html# ls /usr/local/etc/php/conf.d/docker-php-ext-gd.ini /usr/local/etc/php/conf.d/docker-php-ext-gd.ini root@9e7fa8e57db7:/var/www/html# cat /usr/local/etc/php/conf.d/docker-php-ext-gd.ini extension=gd.so root@9e7fa8e57db7:/var/www/html# php -m|grep gd gd
6、redis之類的擴展安裝
curl -L -o redis-4.1.1.tar.gz https://github.com/phpredis/phpredis/archive/4.1.1.tar.gz tar xf redis-4.1.1.tar.gz rm redis-4.1.1.tar.gz mv phpredis-4.1.1 /usr/src/php/ext/redis docker-php-ext-configure redis docker-php-ext-install redis docker-php-ext-enable redis
##查看
root@9d5d4e093dbd:/var/www/html# php -m|grep redis redis