Kubernetes中部署wordpress+mysql(六)

经过前面的内容其实对k8s已经有了服务迁移的能力了,下面这篇文章主要是用来搭建一些后面要用的组件

一、创建wordpress命名空间

kubectl create namespace wordpress

可以用命令查看刚刚创建的命名空间

kubectl get ns

二、创建wordpress-db.yaml文件

创建前先创建文件夹wordpress,然后将文件上传到wordpress文件夹

mkdir wordpress
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-deploy
  namespace: wordpress
  labels:
    app: mysql
spec:
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6  
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
          name: dbport
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: rootPassW0rd
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress
        volumeMounts:
        - name: db
          mountPath: /var/lib/mysql
      volumes:
      - name: db
        hostPath:
          path: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: wordpress
spec:
  selector:
    app: mysql
  ports:
  - name: mysqlport
    protocol: TCP
    port: 3306
    targetPort: dbport

三、根据wordpress-db.yaml创建资源[mysql数据库]

用命令启动文件创建

kubectl apply -f wordpress-db.yaml

进入命名空间查看创建情况,可以发现mysql数据库的pord创建成功了

kubectl get svc mysql -n wordpress

如果想查看详情信息用如下命令

kubectl describe svc mysql -n wordpress

获取ip地址,在创建wordpress.yaml文件中会用到,或者把svc的ip给wordpress.yml文件也行

kubectl get pods -n wordpress

四、创建wordpress.yaml文件

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: wordpress-deploy
  namespace: wordpress
  labels:
    app: wordpress
spec:
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: wdport
        env:
        - name: WORDPRESS_DB_HOST
          value: 192.168.80.223:3306                     
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  namespace: wordpress
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
  - name: wordpressport
    protocol: TCP
    port: 80
    targetPort: wdport

五、根据wordpress.yaml创建资源[wordpress]

 启动yaml文件创建服务(记得修改其中mysql的ip地址,其实也可以使用service的name:mysql)

kubectl apply -f wordpress.yaml 

 查看pod创建情况

kubectl get pods -n wordpress

获取到转发后的端口,供集群之外访问;会发现wordpress创建了一个新的NodePort,如30063

kubectl get svc -n wordpress

六、访问测试

win上访问集群中任意宿主机节点的IP:30063