江湖有传言:能独立完成这个小项目的,算是入了Kubernetes门!!!
拓扑图
主机规划
K8S搭建
https://blog.csdn.net/zisefeizhu/article/details/87911836
kubernetes 命令补全
https://blog.csdn.net/zisefeizhu/article/details/89930329
[root@master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master01 Ready,SchedulingDisabled master 23d v1.14.1
node01 Ready 23d v1.14.1
node02 Ready 23d v1.14.1
harbor搭建
https://blog.csdn.net/zisefeizhu/article/details/90729970
[root@harbor ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:1514 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::443 :::*
LISTEN 0 128 :::4443 :::*
Nfs搭建
https://blog.csdn.net/zisefeizhu/article/details/90729970
准备一块10G的磁盘做为lvm逻辑卷
[root@nfs ~]# fdisk -l
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
[root@nfs ~]# mkdir /k8s-wordpress
[root@nfs ~]# yum install lvm2 -y
[root@nfs ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@nfs ~]# vgcreate vg /dev/sdb
Volume group "vg" successfully created
[root@nfs ~]# lvcreate -L 9G -n lv vg
Logical volume "lv" created.
[root@nfs ~]# mkfs.xfs /dev/vg/lv
meta-data=/dev/vg/lv isize=512 agcount=4, agsize=589824 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2359296, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@nfs ~]# mount /dev/vg/lv /k8s-wordpress/
[root@nfs ~]# echo "/dev/vg/lv /k8s-wordpress xfs defaults 0 0" >> /etc/fstab
[root@nfs ~]# df | grep "/k8s"
/dev/mapper/vg-lv 9426944 32992 9393952 1% /k8s-wordpress
创建lnmpw挂载目录
[root@nfs k8s-wordpress]# mkdir -pv /k8s-wordpress/lnmpw/volumes/data/{mysql,nginx}
mkdir: 已创建目录 "/k8s-wordpress/lnmpw"
mkdir: 已创建目录 "/k8s-wordpress/lnmpw/volumes"
mkdir: 已创建目录 "/k8s-wordpress/lnmpw/volumes/data"
mkdir: 已创建目录 "/k8s-wordpress/lnmpw/volumes/data/mysql"
mkdir: 已创建目录 "/k8s-wordpress/lnmpw/volumes/data/nginx"
客户端挂载
[root@node01 ~]# systemctl start nfs
[root@node01 ~]# showmount -e 10.0.0.206
Export list for 10.0.0.206:
/k8s-wordpress/lnmpw/volumes *
/webtest *
测试挂载
[root@node01 ~]# mount -t nfs 10.0.0.206:/k8s-wordpress/lnmpw/volumes /mnt/
[root@node01 data]# umount -l /mnt/
部署lnmpw
[root@master01 Dockerfile-lnmp]# pwd
/root/k8s_wordporss/Dockerfile-lnmp
创建pv
[root@master01 Dockerfile-lnmp]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
nfs:
path: /k8s-wordpress/lnmpw/volumes/data/mysql
server: 10.0.0.206
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv01
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
nfs:
path: /k8s-wordpress/lnmpw/volumes/data/nginx
server: 10.0.0.206
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv02
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
nfs:
path: /k8s-wordpress/lnmpw/volumes/data/nginx
server: 10.0.0.206
[root@master01 Dockerfile-lnmp]# kubectl apply -f pv.yaml
persistentvolume/mysql-pv created
persistentvolume/wp-pv01 created
persistentvolume/wp-pv02 created
[root@master01 Dockerfile-lnmp]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 2Gi RWX Retain Available 6s
wp-pv01 3Gi RWX Retain Available 6s
wp-pv02 3Gi RWX Retain Available 6s
部署mysql
[root@master01 Dockerfile-lnmp]# cat mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- name: mysql
spec:
containers:
- name: mysql
image : mysql:5.6
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- mountPath: "/var/lib/mysql"
name: mysql-data
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pv-claim
[root@master01 Dockerfile-lnmp]# kubectl apply -f mysql-deployment.yaml
通过secret创建mysql密码
[root@master01 Dockerfile-lnmp]# kubectl create secret generic mysql-pass --from-literal=password=123456
secret/mysql-pass created
[root@master01 Dockerfile-lnmp]# kubectl get pods wordpress-mysql-7d5966b644-zn7sv
NAME READY STATUS RESTARTS AGE
wordpress-mysql-7d5966b644-zn7sv 1/1 Running 0 86s
制作nginx镜像
[root@master01 nginx]# cat Dockerfile
FROM centos:6
MAINTAINER zisefeizhu
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel && \
yum clean all
ADD nginx-1.15.1.tar.gz /tmp
RUN cd /tmp/nginx-1.15.1 && \
./configure --prefix=/usr/local/nginx && \
make -j 2 && make install && \
rm -rf /tmp/nginx-1.15.1
COPY nginx.conf /usr/local/nginx/conf
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
[root@master01 nginx]# cat nginx.conf
user root;
worker_processes auto;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
}
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;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.php index.html;
location ~ \.php$ {
root html;
fastcgi_pass wordpress-php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
[root@master01 nginx]# ll
总用量 1012
-rw-r--r-- 1 root root 395 6月 2 19:36 Dockerfile
-rw-r--r-- 1 root root 1024086 7月 14 2018 nginx-1.15.1.tar.gz
-rw-r--r-- 1 root root 863 6月 2 16:27 nginx.conf
[root@master01 nginx]# docker build -t 10.0.0.204/wordpress/nginx:latest .
...
Successfully built 116e5f6e3719
Successfully tagged 10.0.0.204/wordpress/nginx:latest
制作php镜像
[root@master01 php]# ll
总用量 18920
-rw-r--r-- 1 root root 930 6月 2 19:45 Dockerfile
-rw-r--r-- 1 root root 19288947 6月 2 16:27 php-5.6.31.tar.gz
-rw-r--r-- 1 root root 73698 6月 2 16:27 php.ini
[root@master01 php]# cat Dockerfile
FROM centos:6
MAINTAINER zisefeizhu
RUN yum install -y gcc gcc-c++ gd-devel libxml2-devel \
libcurl-devel libjpeg-devel libpng-devel openssl-devel && \
yum clean all
ADD php-5.6.31.tar.gz /tmp/
RUN cd /tmp/php-5.6.31 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql --with-mysqli \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-iconv \
--enable-fpm --enable-zip --enable-mbstring && \
make -j 4 && make install && \
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.conf && \
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod +x /etc/init.d/php-fpm && \
rm -rf /tmp/php-5.6.31
COPY php.ini /usr/local/php/etc
EXPOSE 9000
CMD /etc/init.d/php-fpm start && tail -F /var/log/messages
[root@master01 php]# docker build -t 10.0.0.204/wordpress/php:latest .
Successfully built d16a4d3d2d21
Successfully tagged 10.0.0.204/wordpress/php:latest
上传镜像到Harbor
[root@master01 php]# docker login 10.0.0.204
Username: admin
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@master01 php]# docker push 10.0.0.204/wordpress/nginx:latest
latest: digest: sha256:f996c4420e480e8115e57821d66196dee47c17064a06131e0219c9a7312ef738 size: 1370
[root@master01 php]# docker push 10.0.0.204/wordpress/php:latest
latest: digest: sha256:f6cedf70fdab45a37e8bbee3e27f82e7e573a1127e603edb2bc910e3a0713248 size: 1374
kubernetes配置Harbor仓库
[root@master01 php]# kubectl create secret docker-registry regcred --docker-server=10.0.0.204 --docker-username=admin --docker-password=123456 [email protected]
secret/regcred created
部署php环境
[root@master01 Dockerfile-lnmp]# cat php-deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-php
labels:
app: wordpress
spec:
ports:
- port: 9000
selector:
app: wordpress-php
tier: frontend
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pvc01
labels:
app: wordpress
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 3Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wordpress-php
labels:
app: wordpress
spec:
replicas: 3
selector:
matchLabels:
app: wordpress-php
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress-php
tier: frontend
spec:
imagePullSecrets:
- name: regcred
containers:
- name: php
image: 10.0.0.204/wordpress/php:latest
ports:
- containerPort: 9000
name: wordpress
volumeMounts:
- name: php-data
mountPath: "/usr/local/nginx/html"
volumes:
- name: php-data
persistentVolumeClaim:
claimName: wp-pvc01
[root@master01 Dockerfile-lnmp]# kubectl apply -f php-deployment.yaml
service/wordpress-php created
persistentvolumeclaim/wp-pvc01 created
deployment.extensions/wordpress-php created
部署nginx环境
[root@master01 Dockerfile-lnmp]# cat nginx-deployment.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-wp-config
data:
site.conf: |-
server {
listen 80;
server_name localhost;
root html;
index index.php index.html;
location ~ \.php$ {
root html;
fastcgi_pass wordpress-php:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-nginx
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress-nginx
tier: frontend
type: NodePort
sessionAffinity: ClientIP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pvc02
labels:
app: wordpress
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 3Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: wordpress-nginx
labels:
app: wordpress
spec:
replicas: 3
selector:
matchLabels:
app: wordpress-nginx
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress-nginx
tier: frontend
spec:
imagePullSecrets:
- name: regcred
containers:
- name: nginx
image: 10.0.0.204/wordpress/nginx:latest
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- mountPath: "/usr/local/nginx/html"
name: nginx-data
- mountPath: "/usr/local/nginx/conf/vhost/site.conf"
name: config
subPath: site.conf
volumes:
- name: nginx-data
persistentVolumeClaim:
claimName: wp-pvc02
- name: config
configMap:
name: nginx-wp-config
[root@master01 Dockerfile-lnmp]# kubectl apply -f nginx-deployment.yaml
configmap/nginx-wp-config created
service/wordpress-nginx created
persistentvolumeclaim/wp-pvc02 created
deployment.extensions/wordpress-nginx created
至此nginx mysql php pod 创建完成
查看pod,svc,pv,pvc,secret的状态
[root@master01 Dockerfile-lnmp]# kubectl get pods
NAME READY STATUS RESTARTS AGE
wordpress-mysql-7d5966b644-zn7sv 1/1 Running 1 57m
wordpress-nginx-6655fdcd9-88fcj 1/1 Running 0 2m32s
wordpress-nginx-6655fdcd9-fjxtc 1/1 Running 0 2m32s
wordpress-nginx-6655fdcd9-tkks8 1/1 Running 0 2m32s
wordpress-php-5f7d54fd99-8q8gg 1/1 Running 0 10m
wordpress-php-5f7d54fd99-dzfl2 1/1 Running 0 10m
wordpress-php-5f7d54fd99-pmvfb 1/1 Running 0 10m
[root@master01 Dockerfile-lnmp]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 23d
wordpress-mysql ClusterIP 10.110.210.251 3306/TCP 59m
wordpress-nginx NodePort 10.101.18.182 80:32516/TCP 3m48s
wordpress-php ClusterIP 10.97.243.166 9000/TCP 12m
[root@master01 Dockerfile-lnmp]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 2Gi RWX Retain Bound default/mysql-pv-claim 63m
persistentvolume/wp-pv01 3Gi RWX Retain Bound default/wp-pvc01 63m
persistentvolume/wp-pv02 3Gi RWX Retain Bound default/wp-pvc02 63m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-pv-claim Bound mysql-pv 2Gi RWX 60m
persistentvolumeclaim/wp-pvc01 Bound wp-pv01 3Gi RWX 12m
persistentvolumeclaim/wp-pvc02 Bound wp-pv02 3Gi RWX 4m31s
[root@master01 Dockerfile-lnmp]# kubectl get secrets
NAME TYPE DATA AGE
default-token-wg2cl kubernetes.io/service-account-token 3 23d
mysql-pass Opaque 1 59m
regcred kubernetes.io/dockerconfigjson 1 27m
创建wordpress数据库
[root@master01 ~]# kubectl exec -it wordpress-mysql-7d5966b644-zn7sv bash
root@wordpress-mysql-7d5966b644-zn7sv:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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.04 sec)
mysql> exit
Bye
root@wordpress-mysql-7d5966b644-zn7sv:/# exit
exit
[root@nfs ~]# cd /k8s-wordpress/lnmpw/volumes/data/mysql/
[root@nfs mysql]# ll
总用量 110604
-rw-rw---- 1 polkitd input 56 6月 2 19:34 auto.cnf
-rw-rw---- 1 polkitd input 12582912 6月 2 19:41 ibdata1
-rw-rw---- 1 polkitd input 50331648 6月 2 19:41 ib_logfile0
-rw-rw---- 1 polkitd input 50331648 6月 2 19:34 ib_logfile1
drwx------ 2 polkitd input 4096 6月 2 19:34 mysql
drwx------ 2 polkitd input 4096 6月 2 19:34 performance_schema
drwx------ 2 polkitd input 20 6月 2 20:34 wordpress
配置wordpress
[root@nfs nginx]# pwd
/k8s-wordpress/lnmpw/volumes/data/nginx
[root@nfs nginx]# ll
总用量 8876
drwxr-xr-x 5 nobody nfsnobody 4096 2月 8 2018 wordpress
-rw-r--r-- 1 root root 9082696 2月 8 2018 wordpress-4.9.4-zh_CN.tar.gz