07-实战
使用我们学过的知识来部署 WordPress。
WordPress、MySQL 这两个应用被封装成了 Pod(由于它们都是在线业务),运行所需的环境变量保存在 ConfigMap中,统一用“声明式”来管理,比起 Shell 脚本更容易阅读和版本化管理。
WordPress 网站搭建步骤
步骤一 定义MySQL YAML
定义MySQL configmap:
MySQL 需要4个环境变量 mysql-cm.yaml
YAML apiVersion: v1 kind: ConfigMap metadata: name: mysql-cm data: MYSQL_DATABASE: 'db_wordpress' MYSQL_USER: 'wordpress' MYSQL_PASSWORD: '123456' MYSQL_ROOT_PASSWORD: '123456'
|
定义 MySQL Pod: mysql-pod.yaml
YAML apiVersion: v1 kind: Pod metadata: name: mysql-pod labels: app: wordpress role: database spec: containers: - name: wp-mysql image: mysql:5.7 imagePullPolicy: IfNotPresent ports: - containerPort: 3306 env: - name: MYSQL_DATABASE valueFrom: configMapKeyRef: name: mysql-cm key: MYSQL_DATABASE - name: MYSQL_USER valueFrom: configMapKeyRef: name: mysql-cm key: MYSQL_USER - name: MYSQL_PASSWORD valueFrom: configMapKeyRef: name: mysql-cm key: MYSQL_PASSWORD - name: MYSQL_ROOT_PASSWORD valueFrom: configMapKeyRef: name: mysql-cm key: MYSQL_ROOT_PASSWORD
|
步骤二 定义 WordPress YAML
定义 WordPress Configmap:wp-cm.yaml
YAML apiVersion: v1 kind: ConfigMap metadata: name: wp-cm data: WORDPRESS_DB_HOST: '100.100.239.19' # mysql pod 的 IP 地址 WORDPRESS_DB_USER: 'wordpress' WORDPRESS_DB_PASSWORD: '123456' WORDPRESS_DB_NAME: 'db_wordpress'
|
定义 WordPress Pod: wp-pod.yaml
YAML apiVersion: v1 kind: Pod metadata: name: wp-pod labels: app: wordpress role: website spec: containers: - name: wp-pod image: wordpress:6 imagePullPolicy: IfNotPresent ports: - containerPort: 80 env: - name: WORDPRESS_DB_HOST valueFrom: configMapKeyRef: name: wp-cm key: WORDPRESS_DB_HOST - name: WORDPRESS_DB_USER valueFrom: configMapKeyRef: name: wp-cm key: WORDPRESS_DB_USER - name: WORDPRESS_DB_PASSWORD valueFrom: configMapKeyRef: name: wp-cm key: WORDPRESS_DB_PASSWORD - name: WORDPRESS_DB_NAME valueFrom: configMapKeyRef: name: wp-cm key: WORDPRESS_DB_NAME
|
步骤三 部署 MySQL 和 WordPress
步骤四 访问 WordPress
查看 Pod IP 地址
YAML kubectl get pods -o wide
|
从外部访问 Pod
YAML kubectl port-forward --address 0.0.0.0 wp-pod 8080:80
|
优化 configmap 引用
YAML apiVersion: v1 kind: Pod metadata: name: wp-pod labels: app: wordpress role: website spec: containers: - name: wp-pod image: wordpress:6 imagePullPolicy: IfNotPresent ports: - containerPort: 80 envFrom: - prefix: 'WORDPRESS_DB_' configMapRef: name: wp-cm
|
YAML apiVersion: v1 kind: ConfigMap metadata: name: wp-cm data: HOST: '100.100.239.19' USER: 'wordpress' PASSWORD: '123456' NAME: 'db_wordpress'
|
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)