kubernetes-wordpress综合练习

kubernetes-wordpress综合练习

需求

## wordpress 要运行在k8s中

## MySQL
1.名称空间: blog
2.镜像mysql:5.7
3.环境变量
    - root密码:123
    - 数据库:wordpress
    - 用户:wordpress
    - 密码:123
    - 参数:字符集
5.数据持久化:在宿主机的/data/mysql/data

## wordpress需求
副本数为2
镜像:自己打(有坑),或者官方
数据库地址:cluster ip
数据库名称:wordpress
用户:wordpress

******** 使用NFS持久化数据,宿主机:/data/wordpress/data

资源清单

[root@master-1 tmp]# vim mysql.yml
---
apiVersion: v1
kind: Namespace
metadata:
  name: blog
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-dp
  namespace: blog
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      name: mysql
      labels:
        app: mysql
    spec:
      volumes:
        - name: mysql-data
          hostPath:
            path: /data/mysql/data
      containers:
        - name: mysql-container
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123"
            - name: MYSQL_DATABASE
              value: "wordpress"
            - name: MYSQL_USER
              value: "wordpress"
            - name: MYSQL_PASSWORD
              value: "123"
          args:
            - --character-set-server=utf8
            - --collation-server=utf8_general_ci
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  namespace: blog
spec:
  type: ClusterIP
  selector:
    app: mysql
  ports:
    - name: mysql-port
      protocol: TCP
      port: 3306
      targetPort: 3306

[root@master-1 tmp]# kubectl apply -f wordpress/mysql.yaml 

[root@master-1 tmp]# kubectl get svc -n blog 
NAME        TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
mysql-svc   ClusterIP   10.1.77.209   <none>        3306/TCP   15s

[root@master-1 tmp]# vim wordpress.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wp-dp
  namespace: blog
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      name: wp
      labels:
        app: wordpress
    spec:
      containers:
        - name: wp-container
          image: wordpress:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: WORDPRESS_DB_HOST
              value: "10.1.77.209:3306"
            - name: WORDPRESS_DB_NAME
              value: wordpress
            - name: WORDPRESS_DB_USER
              value: wordpress
            - name: WORDPRESS_DB_PASSWORD
              value: "123"
---
apiVersion: v1
kind: Service
metadata:
  name: wp-svc
  namespace: blog
spec:
  type: ClusterIP
  selector:
    app: wordpress
  ports:
    - name: wp-port
      protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: wp-ingress
  namespace: blog
spec:
  rules:
    - host: blog.ljy.com
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: wp-svc
                port:
                  number: 80

[root@master-1 tmp]# kubectl apply -f wordpress.yml 

存在问题

1)没有共享存储

  • 访问网站,图片破图

2)没有回话保持

  • 访问网站,一边登录 一边没登录

NFS方案

1.NFS是物理机(推荐,存储安全)

1)node-1 和 node-2挂载宿主机hostPath,将宿主机的hostPath远程挂载到NFS

2)node-1 和 node-2直接挂载NFS

2.NFS是POD

1)node-1 和 node-2挂载宿主机hostPath,将宿主机的hostPath远程挂载到NFS

2)node-1 和 node-2直接挂载NFS

部署NFS(node端也需要下载nfs-utils)

# 1.安装nfs
[root@nfs ~]# yum install -y nfs-utils
[root@node-1 ~]# yum install -y nfs-utils
[root@node-2 ~]# yum install -y nfs-utils

# 2.配置nfs
[root@nfs ~]# vim /etc/exports
/data/wordpress 172.16.1.0/24(rw,sync,no_root_squash)

# 3.创建共享存储目录
[root@nfs ~]# mkdir /data/wordpress -p

# 4.启动nfs
[root@nfs ~]# systemctl start nfs-server

挂载NFS

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wp-dp
  namespace: blog
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      name: wp-dp
      labels:
        app: wordpress
    spec:
----------------添加部分------------------------
      volumes:
      - name: wp-data
        nfs:
          path: /data/wordpress
          server: 172.16.1.91
-----------------------------------------------
      containers:
      - name: wp-container
        image: wordpress:latest
        imagePullPolicy: IfNotPresent
        env:
        - name: WORDPRESS_DB_HOST
          value: "10.1.77.209:3306"
        - name: WORDPRESS_DB_NAME
          value: 'wordpress'
        - name: WORDPRESS_DB_USER
          value: 'wordpress'
        - name: WORDPRESS_DB_PASSWORD
          value: '123'
----------------添加部分------------------------
        volumeMounts:
        - name: wp-data
          mountPath: /var/www/html/
-----------------------------------------------

不断刷新都不会破图

image-20230926182057542

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇