經過前面的內容其實對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