kubernetes 搭建lnmp集成环境

作者: wxfeng 分类: linux 发布时间: 2021-05-10 17:58    阅读 2,608 次

1. 安装nfs(每个节点都要安装)

yum install -y nfs-common nfs-utils rpcbind
mkdir /nfs
chmod 666 /nfs
chown nfsnobody /nfs
cat /etc/exports
/nfs *(rw,no_root_squash,no_all_squash,sync)
systemctl start rpcbind
systemctl start nfs

并且在每个节点都要创建pv对应的挂载文件夹(因为随机分散的节点容器都会挂载)其他三个k8s节点,安装下nfs客户端即可。

mkidr -p /nfs/pv{1..2}

创建nfs挂载目录

mkdir -p /nfs/pv1
mkdir -p /nfs/pv2

创建PV

 kubectl apply -f pv.yaml

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-mysql
  nfs:
    path: /nfs/pv1
    server: 192.168.2.127
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0002
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-nginx
  nfs:
    path: /nfs/pv2
    server: 192.168.2.127
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 3Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-php
  nfs:
    path: /nfs/pv2
    server: 192.168.2.127

部署nginx服务

 kubectl apply -f nginx.yaml

nginx.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-wp-config
data:
  default.conf: |-
    server {
        listen 80;
        server_name localhost;
        root /usr/local/nginx/html;
        index index.html index.php;

        location ~ \.php$ {
            root /usr/local/nginx/html;
            fastcgi_pass blog-php:9000;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_connect_timeout 60s;
            fastcgi_read_timeout 300s;
            fastcgi_send_timeout 300s;
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: blog-nginx
  labels:
    app: nginx
spec:
  ports:
    - port: 80
  selector:
    app: blog-nginx
    tier: frontend
  type: NodePort
  sessionAffinity: ClientIP
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pv-claim
spec:
  storageClassName: nfs-nginx
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog-nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: blog-nginx
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: blog-nginx
        tier: frontend
    spec:
      containers:
        - name: nginx
          image: nginx:1.16.1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: nginx
          volumeMounts:
            - name: nginx-persistent-storage
              mountPath: /usr/local/nginx/html
            - name: config
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: default.conf
      volumes:
        - name: nginx-persistent-storage
          persistentVolumeClaim:
            claimName: nginx-pv-claim
        - name: config
          configMap:
            name: nginx-wp-config

部署mysql

kubectl apply -f mysql.yaml

mysql.yaml

apiVersion: v1
kind: Service
metadata:
  name: blog-mysql
  labels:
    app: blog-mysql
spec:
  ports:
    - port: 3306
  selector:
    app: blog-mysql
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: nfs-mysql
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog-mysql
spec:
  selector:
    matchLabels:
      app: blog-mysql
  template:
    metadata:
      labels:
        app: blog-mysql
    spec:
      containers:
        - name: mysql
          image: mysql:8.0
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim

创建mysql密码的认证 (secret)

kubectl create secret generic mysql-pass --from-literal=password=123456

部署php

kubectl apply -f php.yaml

php.yaml

apiVersion: v1
kind: Service
metadata:
  name: blog-php
  labels:
    app: php
spec:
  ports:
    - name: php
      port: 9000
      targetPort: 9000
  selector:
    app: blog-php
    tier: frontend
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: php-pv-claim
spec:
  storageClassName: nfs-php
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog-php
  labels:
    app: php
spec:
  replicas: 3
  selector:
    matchLabels:
      app: blog-php
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: blog-php
        tier: frontend
    spec:
      containers:
        - name: php
          image: php:7.4-fpm
          command: ["php-fpm"]
          ports:
            - containerPort: 9000
              name: php
          volumeMounts:
            - name: php-persistent-storage
              mountPath: /usr/local/nginx/html
      volumes:
        - name: php-persistent-storage
          persistentVolumeClaim:
            claimName: php-pv-claim

参考:http://www.yoyoask.com/?p=3014 作者:shooter

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。