我們將會從簡入難一步一步的搭建這個系統,掌握 k8s 搭建項目的思路,主要為理解k8s 的使用,不代表生產環境,因為有更簡潔的方法。
首先我們要編寫 Dockerfile 文件,在本機生成我們需要的基礎鏡像,生產環境下我們可以把鏡像上傳私有倉庫,或者公共倉庫中.
目的:把不經常變動的配置固定下來,可以更好的管理和配置整個項目。
生成本地nginx 鏡像
[root@k8s nginx]# docker build -t="wpic/nginx:latest" . Sending build context to Docker daemon 8.192kB Step 1/5 : FROM nginx ---> f6d0b4767a6c Step 2/5 : MAINTAINER bushaoxun ---> Running in b3eff448d1ff Removing intermediate container b3eff448d1ff ---> 3da757f54cea Step 3/5 : EXPOSE 22 80 ---> Running in f0e4256ef9e6 Removing intermediate container f0e4256ef9e6 ---> 1138dec11c30 Step 4/5 : COPY wordpress.conf /etc/nginx/conf.d/default.conf ---> 13b4b3b01008 Step 5/5 : COPY nginx.conf /etc/nginx/nginx.conf ---> ea0b031273ae Successfully built ea0b031273ae Successfully tagged wpic/nginx:latest
生成本地php-fpm 鏡像
docker build -t "wpic/php:7.4-fpm"
構建 Pod 運行 nginx 鏡像
首先我們要構建一個 基於nginx 鏡像的 Pod, yaml 文件如下
apiVersion: v1 # 為 wordpress 項目創建一個獨立的命名空間 kind: Namespace metadata: name: wordpress --- apiVersion: v1 kind: Pod metadata: name: wordpress namespace: wordpress labels: name: wordpress # 為 Pod 設置標簽,方便其他資源對象使用標簽選擇器 spec: containers: - name: wordpress image: wpic/nginx:latest imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80 hostPort: 9000 # 映射容器端口到宿主機端口 restartPolicy: OnFailure
掛載宿主機目錄到 Pod 中 (實現代碼永久性存儲)
apiVersion: v1 kind: Namespace metadata: name: wordpress --- apiVersion: v1 kind: Pod metadata: name: wordpress namespace: wordpress labels: name: wordpress spec: containers: - name: wordpress image: wpic/nginx:latest imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80 hostPort: 9000 volumeMounts: - name: wordpress # 掛在本地存儲到容器目錄 mountPath: "/usr/share/nginx/html" restartPolicy: OnFailure volumes: - name: wordpress # 定義本地儲存 hostPath: path: "/var/www/html"
添加 php-fpm 容器, 我們使用官方鏡像 php:7.4-fpm
apiVersion: v1 kind: Namespace metadata: name: wordpress --- apiVersion: v1 kind: Pod metadata: name: wordpress namespace: wordpress labels: name: wordpress spec: containers: - name: nginx image: wpic/nginx:latest imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80 hostPort: 9000 volumeMounts: - name: wordpress mountPath: "/usr/share/nginx/html" - name: php-fpm image: wpic/php:7.4-fpm lifecycle: # 授予 wordpress 讀寫權限 postStart: exec: command: - "chown" - "-R" - "www-data.www-data" - "/usr/share/nginx/html" imagePullPolicy: IfNotPresent ports: - containerPort: 9000 volumeMounts: #必須在容器中掛載,和 nginx 配置有關 - name: wordpress mountPath: "/usr/share/nginx/html" restartPolicy: OnFailure volumes: - name: wordpress hostPath: path: "/var/www/html"
以 ConfigMap 的形式把nginx 配置文件掛載到 nginx 容器中 (生產環境中,我們一定要自己生成 特有的 docker 鏡像,固定不常更改的配置文件到鏡像中)
apiVersion: v1 kind: Namespace metadata: name: wordpress --- apiVersion: v1 kind: Pod metadata: name: wordpress namespace: wordpress labels: name: wordpress spec: containers: - name: nginx image: wpic/nginx:latest imagePullPolicy: IfNotPresent ports: - name: nginx containerPort: 80 hostPort: 9000 volumeMounts: - name: wordpress mountPath: "/usr/share/nginx/html" - name: configmap-nginx # 掛載 nginx 配置文件 mountPath: /etc/nginx/conf.d - name: php-fpm image: wpic/php:7.4-fpm lifecycle: # 為 wordpress 授予讀寫的權限 postStart: exec: command: - "chown" - "-R" - "www-data.www-data" - "/usr/share/nginx/html" imagePullPolicy: IfNotPresent ports: - containerPort: 9000 volumeMounts: - name: wordpress mountPath: "/usr/share/nginx/html" restartPolicy: OnFailure volumes: - name: wordpress hostPath: path: "/var/www/html" - name: configmap-nginx configMap: #定義 ConfigMap 為數據卷 name: nginx items: - key: wordpress.conf path: wordpress.conf # 在命名空間 wordpress 中創建名字為 ConfigMap 對象, configmap-nginx 是目錄,目錄中存在配置文件 wordpress.conf. kubectl create configmap nginx --from-file=configmap-nginx -n wordpress # wordpress.conf 文件內容如下 server { listen 80; server_name 0.0.0.0; root /usr/share/nginx/html; index index.php index.html; error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } location ~.*\.php$ { fastcgi_index index.php; try_files $uri =404; fastcgi_buffers 32 32K; fastcgi_buffer_size 32K; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 600; #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }