使用Helm在Kubernetes 1.12中安装WordPress
大多数的周末我都会逛咖啡店读书,但偏偏今天因为台风大部分店铺在傍晚早些时候就关门了。
这一次我们尝试在Kubernetes 1.12上使用Helm安装WordPress。Helm(v2.11.0 – Pumpkin Spice Latte Edition)目前只支持Kubernetes v1.11及以下版本,但应该可以运行。可以在这个链接找到相关信息:https://github.com/helm/helm/releases/tag/v2.11.0
Helm和Tiller的安装
首先下载Helm并安装Tiller。
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.11.0-linux-amd64.tar.gz
tar xf helm-v2.11.0-linux-amd64.tar.gz
cd linux-amd64
./helm init
耕耘机开始运动。
# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-node-gxpkn 2/2 Running 2 19h
calico-node-nxnn9 2/2 Running 0 19h
calico-node-wjzzv 2/2 Running 0 19h
coredns-576cbf47c7-dqwkv 1/1 Running 1 19h
coredns-576cbf47c7-n2vtz 1/1 Running 1 19h
etcd-192.168.0.131 1/1 Running 1 19h
kube-apiserver-192.168.0.131 1/1 Running 1 19h
kube-controller-manager-192.168.0.131 1/1 Running 1 19h
kube-proxy-lhjb9 1/1 Running 1 19h
kube-proxy-vbh4p 1/1 Running 0 19h
kube-proxy-vrvkm 1/1 Running 0 19h
kube-scheduler-192.168.0.131 1/1 Running 1 19h
metrics-server-6985c94f4b-t8b2c 1/1 Running 0 19h
tiller-deploy-845cffcd48-ck948 1/1 Running 0 89s
使用Helm安装WordPress。
然后慢慢地安装了WordPress。
./helm repo update
./helm install stable/wordpress
(実行結果)
Error: no available release name found
没问题,这是一个我见过的错误。暂且不考虑任何事情,执行以下四行代码。
kubectl create serviceaccount --namespace kube-system tiller
kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
./helm init --service-account tiller
重新安装。
./helm repo update
./helm install stable/wordpress
(実行結果)
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!⎈
root@m16:~/linux-amd64# ./helm install stable/wordpress
NAME: knotted-molly
LAST DEPLOYED: Sun Sep 30 20:06:44 2018
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
knotted-molly-wordpress-f6b99df66-c2pb8 0/1 Pending 0 0s
knotted-molly-mariadb-0 0/1 Pending 0 0s
==> v1/Secret
NAME AGE
knotted-molly-mariadb 0s
knotted-molly-wordpress 0s
==> v1/ConfigMap
knotted-molly-mariadb-init-scripts 0s
knotted-molly-mariadb 0s
knotted-molly-mariadb-tests 0s
==> v1/PersistentVolumeClaim
knotted-molly-wordpress 0s
==> v1/Service
knotted-molly-mariadb 0s
knotted-molly-wordpress 0s
==> v1beta1/Deployment
knotted-molly-wordpress 0s
==> v1beta1/StatefulSet
knotted-molly-mariadb 0s
NOTES:
1. Get the WordPress URL:
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
Watch the status with: 'kubectl get svc --namespace default -w knotted-molly-wordpress'
export SERVICE_IP=$(kubectl get svc --namespace default knotted-molly-wordpress --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
echo "WordPress URL: http://$SERVICE_IP/"
echo "WordPress Admin URL: http://$SERVICE_IP/admin"
2. Login with the following credentials to see your blog
echo Username: user
echo Password: $(kubectl get secret --namespace default knotted-molly-wordpress -o jsonpath="{.data.wordpress-password}" | base64 --decode)
root@m16:~/linux-amd64#
好的。
但是,目前尚未找到持久卷,因此PVC处于待定状态。
# kubectl get pod
NAME READY STATUS RESTARTS AGE
knotted-molly-mariadb-0 0/1 Pending 0 106s
knotted-molly-wordpress-f6b99df66-c2pb8 0/1 Pending 0 106s
my-db-f55786649-pznng 1/1 Running 0 20h
my-node-7488b97dd8-56wk6 1/1 Running 2 20h
my-node-7488b97dd8-9btbs 1/1 Running 3 20h
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-knotted-molly-mariadb-0 Pending 113s
knotted-molly-wordpress Pending 113s
嗯,有点麻烦但要建立一个NFS服务器(192.168.0.135)。
建立NFS服务器(省略步骤),并导出/share *(rw,no_root_squash),在所有节点上安装nfs-common。
apt-get -y install nfs-common
每个PVC所需的容量分别为8Gi和10Gi。
# kubectl get pvc data-knotted-molly-mariadb-0 -o yaml
...
spec:
accessModes:
- ReadWriteOnce
dataSource: null
resources:
requests:
storage: 8Gi
...
# kubectl get pvc knotted-molly-wordpress -o yaml
...
spec:
accessModes:
- ReadWriteOnce
dataSource: null
resources:
requests:
storage: 10Gi
...
所以,按照8Gi、10Gi的顺序在NFS上创建PV。
apiVersion: v1
kind: PersistentVolume
metadata:
name: data-knotted-molly-mariadb-0
spec:
capacity:
storage: 8Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /share/data-knotted-molly-mariadb-0
server: 192.168.0.135
apiVersion: v1
kind: PersistentVolume
metadata:
name: knotted-molly-wordpress
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /share/knotted-molly-wordpress
server: 192.168.0.135
mkdir /share
mount 192.168.0.135:/share /share
mkdir /share/data-knotted-molly-mariadb-0
chmod 777 /share/data-knotted-molly-mariadb-0
mkdir /share/knotted-molly-wordpress
chdmod 777 /share/knotted-molly-wordpress
kubectl create -f pv_data-knotted-molly-mariadb-0.yaml
kubectl create -f pv_knotted-molly-wordpress.yaml
嗯,其实,我不太清楚有没有一种巧妙的方法可以将由Helm创建的应用程序使用的某个PVC分配给目标PV,但是,至少存储空间已经分配如下。
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-knotted-molly-mariadb-0 Bound data-knotted-molly-mariadb-0 8Gi RWO 28m
knotted-molly-wordpress Bound knotted-molly-wordpress 10Gi RWO 28m
Pod终于启动了。
# kubectl get pod
NAME READY STATUS RESTARTS AGE
knotted-molly-mariadb-0 1/1 Running 0 114s
knotted-molly-wordpress-f6b99df66-qghmg 1/1 Running 0 70s
my-db-f55786649-pznng 1/1 Running 0 20h
my-node-7488b97dd8-56wk6 1/1 Running 2 20h
my-node-7488b97dd8-9btbs 1/1 Running 3 20h
现在我想要访问已启动的WordPress,但是服务正在使用负载均衡器并且处于待定状态。
# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
knotted-molly-mariadb ClusterIP 10.110.181.27 <none> 3306/TCP 42m
knotted-molly-wordpress LoadBalancer 10.96.245.136 <pending> 80:31632/TCP,443:30585/TCP 42m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h
my-db ClusterIP 10.106.19.247 <none> 3306/TCP 20h
my-node NodePort 10.96.42.60 <none> 80:30864/TCP 20h
所以将其更改为ClusterIP。
kubectl edit service knotted-molly-wordpress
(変更前)
23 - name: http
24 nodePort: 31632
25 port: 80
(変更後)
23 - name: http
24 # nodePort: 31632
25 port: 80
---
(変更前)
28 - name: https
29 nodePort: 30585
30 port: 443
(変更後)
28 - name: https
29 # nodePort: 30585
30 port: 443
---
(変更前)
35 sessionAffinity: None
36 type: LoadBalancer
37 status:
(変更後)
35 sessionAffinity: None
36 type: NodePort
37 status:
顺便创建Ingress资源,以便能够从我的个人电脑通过Web浏览器访问。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: knotted-molly-wordpress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: knotted-molly-wordpress
servicePort: 80
host: knotted-molly-wordpress.com
kubectl create -f ingress_knotted-molly-wordpress.yaml
然后,修改本地PC的hosts文件,将knotted-molly-wordpress.com的访问地址修改为192.168.0.134(keepalived的VIP地址,用于运行Ingress),使其可以进行访问。
哇!终于有了Wordpress的管理界面。
用户名为user,密码请执行以下命令获取。
echo Password: $(kubectl get secret --namespace default knotted-molly-wordpress -o jsonpath="{.data.wordpress-password}" | base64 --decode)
然后登录。
嗯。
使用Helm来轻松搭建WordPress,却远远不能达到简单的程度。