1.准备nginx文件,构建为镜像
1、创建目录
[root@docter ~]# mkdir /blog/{php,nginx,mysql}
[root@docter ~]# cd /blog/nginx/
2、编写nginx的dockerfile文件
[root@docter nginx]# cat Dockerfile
FROM nginx
ADD nginx.conf /etc/nginx/nginx.conf
ADD default.conf /etc/nginx/conf.d/default.conf
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666 -M -r -s /sbin/nologin
ADD discuz /usr/share/nginx/html
RUN chown -R www.www /usr/share/nginx/html
WORKDIR /usr/shar/nginx/html
EXPOSE 80 443
CMD nginx -g "daemon off;"
3、编写nginx的default.conf 文件
[root@docter nginx]# cat default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3、编辑nginx配置文件
[root@docker1 nginx]# vim nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
4.上传discuz包
#上传,解压,只留upload,改名为discuz
[root@k8s-m-01 nginx]# ll
total 16
-rw-r--r-- 1 root root 449 Aug 14 14:38 default.conf
-rw-r--r-- 1 root root 325 Aug 14 14:38 Dockerfile
-rw-r--r-- 1 root root 607 Aug 14 14:39 nginx.conf
drwxr-xr-x 13 root root 4096 Mar 22 19:44 upload
5、构建镜像
#在阿里云上面,先创建一个命名空间,再拉取镜像
[root@k8s-m-01 nginx]# docker build -t registry.cn-shanghai.aliyuncs.com/cdank8s/web:discuz-nginx-v1 .
#登录阿里云的本地仓库
[root@k8s-m-01 nginx]# docker login registry.cn-hangzhou.aliyuncs.com
Username: danlleee
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
#把镜像上传到阿里云
[root@k8s-m-01 nginx]# docker push registry.cn-hangzhou.aliyuncs.com/cdan-web/discuz:nginx-v1
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/cdan-web/discuz]
c87af564398a: Pushed
34ef6b102470: Pushed
9300c38069e5: Pushed
733dccb30fd7: Pushed
314140127be1: Pushed
fea76014ff25: Pushed
e3135447ca3e: Pushed
b85734705991: Pushed
988d9a3509bb: Pushed
59b01b87c9e7: Pushed
7c0b223167b9: Pushed
814bff734324: Pushed
nginx-v1: digest: sha256:c3fbc64e02bbde6dfeed60316db15f0614b603c56637f3e2dbce6a2e8c83525c size: 2823
#测试启动
[root@k8s-m-01 nginx]# docker run -d --name nginx registry.cn-hangzhou.aliyuncs.com/cdan-web/discuz:nginx-v1
[root@k8s-m-01 nginx]# docker ps -a | grep nginx
11b38bc51294 registry.cn-shanghai.aliyuncs.com/cdank8s/web:discuz-v1 "/docker-entrypoint.…" 27 seconds ago Exited (1) 27 seconds ago nginx
[root@k8s-m-01 nginx]# docker exec -it 11b38bc51294 bash
Error response from daemon: Container 11b38bc51294484e5c1f86110e2b01c68a36db683e4cbb3c6190c5392269d9bd is not running
[root@k8s-m-01 nginx]# ls
default.conf discuz Dockerfile nginx.conf
2.构建php镜像
#上传php包
[root@k8s-m-01 php]# ll
total 19436
drwxr-xr-x 13 root root 4096 Mar 22 19:44 discuz
-rw-r--r-- 1 root root 327 Aug 14 16:47 Dockerfile
-rw-r--r-- 1 root root 19889622 Apr 28 10:39 php.tar.gz
-rw-r--r-- 1 root root 463 Aug 14 16:39 www.conf
2、上传php.tar.gz
[rook8s-m-01 php]# wget http://www.mmin.xyz:81/package/lnmp/php.tar.gz
#编辑php Dockerfile
[root@docker php]# vim Dockerfile
FROM centos:7
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666 -M -r -s /sbin/nologin
ADD php.tar.gz /tmp
RUN yum -y localinstall /tmp/*.rpm
ADD www.conf /etc/php-fpm.d/
EXPOSE 9000
WORKDIR /usr/share/nginx/html
ADD discuz /usr/share/nginx/html
RUN chown -R www.www /usr/share/nginx/html
CMD php-fpm -F
#discuze复制过来
[root@k8s-m-01 php]# cp ../nginx/discuz/ .
#创建PHP.repo
[root@k8s-m-01 php]# cat www.conf
[www]
user = www
group = www
listen = 9000
request_terminate_timeout = 0
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
#创建镜像
[root@k8s-m-01 php]# docker build -t registry.cn-hangzhou.aliyuncs.com/cdan-web/discuz:php-v1 .
#上传到本地
[root@k8s-m-01 php]# docker push registry.cn-hangzhou.aliyuncs.com/cdan-web/discuz:php-v1
3.创建yaml配置文件
# 1、编写mysql.yaml
[root@k8s-m-01 discuz]# vim mysql.yaml
kind: Namespace
apiVersion: v1
metadata:
name: mysql
---
kind: Deployment
apiVersion: apps/v1
metadata:
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: "123"
- name: MYSQL_DATABASE
value: discuz
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /etc/mysql/my.cnf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 30
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
---
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: mysql
spec:
ports:
- port: 3306
targetPort: 3306
protocol: TCP
name: mysql
selector:
app: mysql
# 2、编写web.yaml
[root@k8s-m-01 discuz]# vim web.yaml
kind: Namespace
apiVersion: v1
metadata:
name: web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: web
spec:
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: php
image: registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:php-v2
imagePullPolicy: Always
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /etc/php-fpm.d/www.conf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 9000
initialDelaySeconds: 10
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
- name: nginx
image: registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:nginx-v2
imagePullPolicy: Always
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /etc/nginx/nginx.conf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
---
kind: Service
apiVersion: v1
metadata:
name: web
namespace: web
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
selector:
app: web
type: NodePort
4.生成yaml文件
# 1、生成yaml文件
[root@k8s-m-01 blog]# kubectl apply -f web.yaml
namespace/web created
deployment.apps/web created
service/web created
[root@k8s-m-01 blog]# kubectl apply -f mysql.yaml
namespace/mysql created
deployment.apps/mysql created
service/mysql created
# 2、查看
[root@k8s-m-01 blog]# kubectl get pod -n web
NAME READY STATUS RESTARTS AGE
web-7f897d448c-v5vbg 2/2 Running 0 69s
[root@k8s-m-01 blog]# kubectl get pod -n mysql
NAME READY STATUS RESTARTS AGE
mysql-6f9b947c9f-25rrx 1/1 Running 0 5m54s
[root@k8s-m-01 blog]# kubectl get svc -n web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.96.59.89 <none> 80:31558/TCP 2m13s
# 3、IP访问
192.168.15.11:31558