附上連接:前端
docker構建web集羣:https://my.oschina.net/daba0007/blog/1605365node
kubenetes與web集羣: https://my.oschina.net/daba0007/blog/1602425python
k8s-web集羣架構從零開始(1): https://my.oschina.net/daba0007/blog/1604526mysql
我使用的是django作爲架構來搭建web。這裏web前端的內容我就不作擴展了,我仍是使用上次隨便寫的那個網站來作測試。不過web集羣在這裏和docker當時的作法不太同樣了。以前咱們是使用了web容器和nginx容器共享數據卷容器來實現網站數據共享。nginx
nginx是一個高性能的HTTP和反向代理服務器,在web集羣中咱們使用它作爲http的代理服務器,在k8s中,咱們徹底能夠把這web容器和nginx容器寫在一個同一個pod裏面,由於他們共享着數據卷,關係十分地親密。git
使用django鏈接mysql和redis服務主要是在setting中修改。github
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'form', 'USER':'root', 'PASSWORD':'123456', #'HOST': '127.0.0.1' 'HOST':'service_mysql', 'Port':'3306', } } CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", #"LOCATION": "redis://127.0.0.1:6379", "LOCATION": "redis://service_redis:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } }
咱們須要把這裏host的mysql和location的redis換成服務的ip。首先來編寫dockerfileweb
# 基礎鏡像 FROM daocloud.io/python:3.6 # 維護者信息 MAINTAINER daba0007 ADD dabaweb.tar.gz /usr/src/ # app 所在目錄 WORKDIR /usr/src/dabaweb RUN pip install xlutils RUN pip install django-redis # 安裝 app 所需依賴 RUN pip install --no-cache-dir -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com # 啓動執行命令 COPY entrypoint.sh /usr/src/ WORKDIR /usr/src RUN chmod +x /usr/src/entrypoint.sh ENTRYPOINT ["/usr/src/entrypoint.sh"]
這裏的啓動腳本以下 entrypoint.shredis
#!/bin/bash sed -i "s/service_mysql/$(echo $MYSQL_MASTER_SERVICE_HOST)/g" /usr/src/dabaweb/dabaweb/setting.py sed -i "s/service_redis/$(echo $REDIS_MASTER_SERVICE_HOST)/g" /usr/src/dabaweb/dabaweb/setting.py #使用uwsgi來啓動django /usr/local/bin/uwsgi --http :8000 --chdir /usr/src/dabaweb -w dabaweb.wsgi
建立dockerfile,而且上傳sql
docker build -t daba0007/dabaweb . docker push daba0007/dabaweb
再構造一個nginx的dockerfile
FROM daba0007/nginx MAINTAINER daba0007 RUN rm /etc/nginx/conf.d/default.conf ADD nginx-conf/ /etc/nginx/conf.d/
nginx服務鏈接dabaweb是在nginx.conf中proxy_pass,咱們須要把web替代成localhost,由於是在同一個pod中。
server { listen 80; server_name localhost; charset utf-8; root /usr/src/dabaweb; access_log /var/log/nginx/django.log; location ^~ /static { alias /usr/src/dabaweb/static; } location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
那麼咱們須要掛載文件夾。編寫dabacluster-rc.yaml
apiVersion: v1 kind: ReplicationController metadata: name: dabacluster labels: name: dabacluster spec: replicas: 3 selector: name: dabacluster template: metadata: labels: name: dabacluster spec: containers: - name: dabaweb image: daba0007/dabaweb env: - name: GET_HOSTS_FROM value: env ports: - containerPort: 8000 volumeMounts: - name: dabaweb-dir mountPath: /usr/src/dabaweb readOnly: false - name: dabanginx image: daba0007/dabanginx env: - name: GET_HOSTS_FROM value: env ports: - containerPort: 80 volumeMounts: - name: dabaweb-dir mountPath: /usr/src/dabaweb readOnly: false volumes: - name: dabaweb-dir hostPath: path: /root/k8s/web/web/dabaweb/
而後執行
[root@k8s-master web]# kubectl create -f dabacluster-rc.yaml replicationcontroller "dabacluster" created [root@k8s-master web]# kubectl get pod NAME READY STATUS RESTARTS AGE dabacluster-5kp26 2/2 Running 0 18s dabacluster-7dhsk 2/2 Running 0 18s dabacluster-mww8t 2/2 Running 0 18s mysql-master-whtwd 1/1 Running 3 2d mysql-slave-6x8bx 1/1 Running 3 2d mysql-slave-n58vk 1/1 Running 3 2d redis-master-25bpz 1/1 Running 6 4d redis-slave-plrxq 1/1 Running 5 4d redis-slave-thb9r 1/1 Running 5 4d
再編寫web服務web-svc.yaml,鏈接服務時,使用Service的NodePort給kubernetes集羣中Service映射一個外網能夠訪問的端口,這樣一來,外部就能夠經過NodeIP+NodePort的方式訪問集羣中的服務了。
apiVersion: v1 kind: Service metadata: name: dabacluster labels: name: dabacluster spec: ports: type: NodePort - port: 80 targetPort: 80 nodePort: 32000 # 指明暴露在外端口的port,即k8s中的80映射到主機上32000端口 selector: name: dabacluster
而後執行
[root@k8s-master web]# kubectl create -f dabacluster-svc.yaml service "dabacluster" created NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE dabacluster NodePort 10.68.64.177 <none> 80:32000/TCP 10m kubernetes ClusterIP 10.68.0.1 <none> 443/TCP 4d mysql-master ClusterIP 10.68.83.79 <none> 3306/TCP 2d mysql-slave ClusterIP 10.68.208.186 <none> 3306/TCP 2d redis-master ClusterIP 10.68.69.173 <none> 6379/TCP 4d redis-slave ClusterIP 10.68.174.55 <none> 6379/TCP 4d
訪問http://ip:32000,發現成功的訪問,說明服務啓動成功
花了兩大篇來介紹k8s,細心的人就會發現咱們只說到了svc,rc和pod。咱們以前構造k8s的時候是使用了三個節點,一個master和兩個minion,在搭建web集羣的過程當中徹底沒提到啊,它們發生了什麼?
其實對於每一個節點(在個人集羣中是三個,一個master和兩個minion),他們負載的能力確定是有限的。咱們在每次寫服務的時候,都會分配一些資源給節點。當一個節點的負載超額的時候(pod數量過多或系統資源不夠分配),k8s會自動加入下一個節點,而且將這些超出的pod放到下一個節點中。也就是說,若是咱們有足夠多的節點,在master上操做的時候就感受像是在一個超級計算機中,全部的需求都能知足。
進入某個節點查看
kubectl exec -ti mysql-master-whtwd /bin/bash
獲得log
kubectl logs -f [pods] # [pods]寫入你pods的名字,也能夠是rc,svc等等
刪除
kubectl delete svc redis-master kubectl delete rc redis-master
感謝如下博主提供的思路