美文网首页docker. k8s
kubernetes(k8s)部署wordpress示例①

kubernetes(k8s)部署wordpress示例①

作者: 祁恩达 | 来源:发表于2019-07-11 20:41 被阅读0次

部署 Wordpress 示例①

使用一个pod部署:

新建blog namespace。将应用都部署到blog这个命名空间下面,所以先创建一个命名空间:

$ kubectl create namespace blog
namespace "blog" created

然后来编写YAML文件:(wordpress-pod.yaml)

apiVersion: v1
kind: Pod
metadata:
  name: wordpress
  namespace: blog
spec:
  containers:
  - name: wordpress
    image: wordpress
    ports:
    - containerPort: 80
      name: wdport
    env:
    - name: WORDPRESS_DB_HOST
      value: localhost: 3306
    - name: WORDPRESS_DB_USER
      value: wordpress
    - name: WORDPRESS_DB_PASSWORD
      value: wordpress
  - 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

要注意这里针对mysql这个容器我们做了一个数据卷的挂载,这是为了能够将mysql的数据能够持久化到节点上,这样下次mysql容器重启过后数据不至于丢失。 然后创建上面的Pod:

$ kubectl create -f wrodpress-pod.yaml
pod "wordpress" created

接下来就是等待拉取镜像,启动容器,同样我们可以使用describe指令查看详细信息:

$ kubectl describe pod wordpress -n blog

使用两个deployment部署:

第一步,创建一个MySQL的Deployment对象:(wordpress-db.yaml)

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-deploy
  namespace: blog
  labels:
    app: mysql
spec:
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6            #使用mysql:5.7镜像会报错
        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: blog
spec:
  selector:
    app: mysql
  ports:
  - name: mysqlport
    protocol: TCP
    port: 3306
    targetPort: dbport

然后创建上面的wordpress-db.yaml文件:

$ kubectl create -f wordpress-db.yaml
service "mysql" created
deployment.apps "mysql-deploy" created

然后我们查看Service的详细情况:

$ kubectl describe svc mysql -n blog
Name:              mysql
Namespace:         blog
Labels:            <none>
Annotations:       <none>
Selector:          app=mysql
Type:              ClusterIP
IP:                10.254.175.158
Port:              mysqlport  3306/TCP
TargetPort:        dbport/TCP
Endpoints:         10.254.23.12:3306
Session Affinity:  None
Events:            <none>

可以看到Endpoints部分匹配到了一个Pod,生成了一个clusterIP:10.254.175.158,现在我们可以通过这个clusterIP加上定义的3306端口问我们mysql服务.
第二步. 创建Wordpress服务,将上面的wordpress的Pod转换成Deployment对象:(wordpress.yaml)

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: wordpress-deploy
  namespace: blog
  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: 10.254.175.158:3306                      #此处的IP,是mysql svc的clusterIP
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  namespace: blog
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
  - name: wordpressport
    protocol: TCP
    port: 80
    targetPort: wdport

注意要添加属性type: NodePort,然后创建wordpress.yaml文件:

$ kubectl apply -f wordpress.yaml
service "wordpress" created
deployment.apps "wordpress-deploy" configured

创建完成后,查看下svc:

 kubectl get svc -n blog
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
mysql       ClusterIP   10.254.175.158   <none>        3306/TCP       4m44s
wordpress   NodePort    10.254.218.67    <none>        80:39754/TCP   2m51s

可以看到wordpress服务产生了一个39754的端口,现在我们是不是就可以通过任意节点的NodeIP加上39754端口,就可以访问wordpress应用了,在浏览器中打开,如果看到wordpress跳转到了安装页面,证明安装是没有任何问题的,如果没有出现预期的效果,那么就需要去查看下Pod的日志来查看问题了:

wordpress

相关文章

网友评论

    本文标题:kubernetes(k8s)部署wordpress示例①

    本文链接:https://www.haomeiwen.com/subject/tjjjkctx.html