为什么要使用kubernetes

这里有一个PHP项目系统,想需要部署到kubernetes集群中,因为它能够实现负载均衡,存储管理,自动扩缩,等错误回滚。

kubernetes的环境准备

首先需要有一个k8s的环境

本次K8s操作环境如下,至于怎么去搭建K8S环境这里不去实操了

服务器主机ip地址
master192.168.200.128
node192.168.200.129

构建PHP HTTP Docker镜像环境

参考本地部署环境:PHP项目部署到云服务器(CentOS+HTTP+PHP+MYSQL)_真的不会啊的博客-CSDN博客

部署一下php+httpd环境 需要有一个存放文件夹的良好习惯,这里创建一个属于项目的空文件夹

[root@k8s-master-node1 ~]# mkdir httpPhp/
[root@k8s-master-node1 ~]# cd httpPhp/
[root@k8s-master-node1 httpPhp]# 

镜像生成离不开dockerfile 去编写php+httpd dockerfile

[root@k8s-master-node1 httpPhp]# cat Dockerfile 
FROM centos:7.9.2009
MAINTAINER langhua
RUN rm -rf /etc/yum.repos.d/*
ADD *.repo /etc/yum.repos.d/     #这里需要上传的文件
RUN yum install httpd -y
RUN yum install php php-mysql -y
ADD httpd.conf /etc/httpd/conf/httpd.conf   #这里需要上传http配置的文件
RUN echo "<?php phpinfo(); ?>" > /var/www/html/index.php
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
[root@k8s-master-node1 httpPhp]# 

以上还不能立马去构建docker镜像,还缺少一些重要文件

在这个dockerfile当前目录中需要存在以下文件

[root@k8s-master-node1 httpPhp]# ll
total 28 
-rw-r--r--. 1 root root  2523 Oct  2 14:32 CentOS-Base.repo  #这个是通过联网下载的yum阿里源
-rw-r--r--. 1 root root   301 Oct  2 14:44 Dockerfile      #这个是dockerfile 创建镜像文件
-rw-r--r--. 1 root root 11803 Oct  2 06:52 httpd.conf      #这个是httpd配置文件 配置文件如下
[root@k8s-master-node1 httpPhp]# 
[root@k8s-master-node1 httpPhp]# cat httpd.conf |grep -Ev '^$|#'
......
<IfModule dir_module>
    DirectoryIndex index.html index.php   #这里追加index.php 不然访问不了默认网站
</IfModule>
......
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php         #就添加了这一行
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
......
​
#其实以上就添加如下
AddType application/x-httpd-php .php
DirectoryIndex index.html index.php

确保以上文件存在就可以去构建镜像了

[root@k8s-master-node1 httpPhp]# docker build -t 192.168.200.128/test/http_php:latest .
Sending build context to Docker daemon  20.48kB
Step 1/9 : FROM centos:7.9.2009
 ---> eeb6ee3f44bd
Step 2/9 : MAINTAINER guziliang
 ---> Using cache
 ---> ecd63250109f
Step 3/9 : RUN rm -rf /etc/yum.repos.d/*
 ---> Using cache
 ---> ebfd8e2c1dc5
Step 4/9 : ADD *.repo /etc/yum.repos.d/
 ---> Using cache
 ---> d058dff66bd4
Step 5/9 : RUN yum install httpd -y
 ---> Using cache
 ---> 9bf491375793
Step 6/9 : RUN yum install php php-mysql -y
 ---> Using cache
 ---> c6be24a277be
Step 7/9 : ADD httpd.conf /etc/httpd/conf/httpd.conf
 ---> Using cache
 ---> 9169b36d9d54
Step 8/9 : RUN echo "<?php phpinfo(); ?>" > /var/www/html/index.php
 ---> Using cache
 ---> af0d0542227d
Step 9/9 : CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
 ---> Using cache
 ---> 331313bc658f
Successfully built 331313bc658f
Successfully tagged 192.168.200.128/test/http_php:latest
[root@k8s-master-node1 httpPhp]# 
​
#结尾出现了Successfully 这个说明docker images镜像构建成功了
#这里构建镜像的名称是192.168.200.128/test/http_php:latest 前缀是仓库的地址 需要上传到Harbor仓库中

以上镜像创建好了,把这个上传到harbor仓库里面

其实这个不用上传也可以的,用于方便还是上传了。如果想需要去实现,那必须在每个节点中有这个docker镜像。

#这个是登录你的harbor仓库
[root@k8s-master-node1 httpPhp]# docker login 192.168.200.128 -u=admin -p=Harbor12345 
#上传镜像到harbor 仓库
[root@k8s-master-node1 httpPhp]# docker push 192.168.200.128/test/http_php:latest

部署上线到构建的images环境

以上httpd+php环境images构建完啦,有了docker images镜像就可以去部署项目了,接下来以下编写yaml操作

phphttp的deployment文件内容如下

[root@k8s-master-node1 httpPhp]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-demo
  namespace: test
spec:
  replicas: 10
  selector:
    matchLabels:
      project: www
      app: php-demo
  template:
    metadata:
      labels:
        project: www
        app: php-demo
    spec:
      containers:
      - name: http 
        image: 192.168.200.128/test/http_php:latest   #这里是我们
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: web
          protocol: TCP
        volumeMounts:
        - name: data-volume
          mountPath: /var/www/html/    #记住这个 这是网站的目录
      volumes:                     
      - name: data-volume
        hostPath:
          path: /www/data             #记住这个/www/date 挂载了 /var/www/html/
[root@k8s-master-node1 httpPhp]# 

记住以上的/www/data是宿主机的目录 对应容器的/var/www/html 容器这个是网站目录。我们只需要在宿主机/www/data/目录中去存放php项目

对外暴露端口 server yaml

[root@k8s-master-node1 httpPhp]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: php-demo 
  namespace: test
spec:
  selector:
    project: www
    app: php-demo
  ports:
  - name: web
    port: 80
    targetPort: 80
    nodePort: 30008         #这是宿主机对外提供的端口
  type: NodePort
  sessionAffinity: ClientIP

创建启动HTTP PHP项目

[root@k8s-master-node1 httpPhp]# kubectl apply -f deployment.yaml 
deployment.apps/php-demo unchanged
[root@k8s-master-node1 httpPhp]# kubectl apply -f service.yaml              
service/php-demo unchanged
[root@k8s-master-node1 httpPhp]# 

查看状态

[root@k8s-master-node1 httpPhp]# kubectl get -n test deployments,pod,svc
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/php-demo   10/10   10           10          5h5m

NAME                            READY   STATUS    RESTARTS   AGE
pod/php-demo-6cd9794567-4mxrp   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-chkkb   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-db4mc   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-hbxkb   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-kdcfw   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-p5kst   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-p99xd   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-v2xnq   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-vv7gc   1/1     Running   0          5h5m
pod/php-demo-6cd9794567-wtrfp   1/1     Running   0          5h5m

NAME               TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
service/php-demo   NodePort   10.96.65.79   <none>        80:30008/TCP   5h2m
[root@k8s-master-node1 httpPhp]# 

以上看到我们的pod全都在运行 当然了是可以去访问了 访问ip如下

http://192.168.200.128:30008

部署数据库环境

php项目需要mysql的支持,以上没有做mysql 数据库,接下来配置nfs作为mysql 的后端存储

以下NFS操作可以参考Kubernetes 使用NFS共享存储_真的不会啊的博客-CSDN博客

在node节点中去搭建nfs 服务器,

[root@k8s-worker-node1 ~]# mkdir -p /opt/data/mysql/data
[root@k8s-worker-node1 ~]# chmod -R 777 /opt/data/mysql/data/
[root@k8s-worker-node1 ~]# cat /etc/exports
/opt/data/mysql/data *(rw,no_root_squash,sync)
[root@k8s-worker-node1 ~]# systemctl restart rpcbind
[root@k8s-worker-node1 ~]# systemctl restart nfs
[root@k8s-worker-node1 ~]# systemctl enable rpcbind
[root@k8s-worker-node1 ~]# systemctl enable nfs

客户端测试nfs 是否可用

[root@k8s-master-node1 httpPhp]# showmount -e 192.168.200.129
Export list for 192.168.200.129:
/opt/data/mysql/data *

没啥问题可以去写 pv.pvc了

[root@k8s-master-node1 httpPhp]# cat pv1.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
  labels:
    pv: pv1
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
  nfs:
    path: /opt/data/mysql/data
    server: 192.168.200.129

pvc1.yaml

[root@k8s-master-node1 httpPhp]# cat pvc1.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: pv1

创建pv pvc

[root@k8s-master-node1 httpPhp]# kubectl apply -f pv1.yaml 
[root@k8s-master-node1 httpPhp]# kubectl apply -f pvc1.yaml
[root@k8s-master-node1 httpPhp]# kubectl get pv,pvc
NAME                   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM          STORAGECLASS   REASON   AGE
persistentvolume/pv1   5Gi        RWO            Recycle          Bound    default/pvc1   nfs                     12h

NAME                              STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS          AGE
persistentvolumeclaim/data-db-0   Pending                                      managed-nfs-storage   12h
persistentvolumeclaim/pvc1        Bound     pv1      5Gi        RWO            nfs                   12h

部署mysql

以上没有问题就可以去写mysql.yaml了,内容如下

[root@k8s-master-node1 httpPhp]# cat mysql.yaml 
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: db
spec:
  selector:
    matchLabels:
      project: php-demo
      app: mysql
  serviceName: "mysql"
  template:
    metadata:
      labels:
        project: php-demo
        app: mysql
    spec:
      hostNetwork: true
      containers:
      - name: mysql
        image: mysql:5.6 
        ports: 
        - containerPort: 3306
        env: 
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        volumeMounts:
        - mountPath: /var/lib/mysql/
          name: pv1
        volumeMounts:
        - name: timezone
          mountPath: /etc/localtime
      volumes:
      - name: pv1
        persistentVolumeClaim:
          claimName: pvc1
      - name: timezone
        hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai

创建mysql

[root@k8s-master-node1 httpPhp]# kubectl apply -f mysql.yaml

查看状态

[root@k8s-master-node1 httpPhp]# kubectl get pod
NAME   READY   STATUS    RESTARTS   AGE
db-0   1/1     Running   0          6h29m
[root@k8s-master-node1 httpPhp]# 

数据库地址以及账号密码是 192.168.200.128 root 123456

记切,记得项目中的mysql连接更改一下以上地址。

PHP项目放在master节点上的/www/date目录即可。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐