溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在k8s中部署nginx?

發(fā)布時間:2020-08-06 17:26:26 來源:ITPUB博客 閱讀:152 作者:專注的阿熊 欄目:建站服務(wù)器

k8s啟動nginx服務(wù)
本次需要啟動的是一個通過nginx實(shí)現(xiàn)的靜態(tài)文件服務(wù)器。 創(chuàng)建命名空間腳本create_shjujubu_namespace.yaml

MT4教程專題http://www.gendan5.com/operation.html


apiVersion: v1
kind: Namespace
metadata:
  name: shujubu
  labels:
    name: shujubu

執(zhí)行文件創(chuàng)建 shujubu命名空間:
kubectl create -f  create_shjujubu_namespace.yaml
1
需要的鏡像列表:
nginx                                                                      1.17                62c261073ecf        3 months ago        109MB
1
定義pod文件 nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: sjb-nginx
  namespace: shujubu
  labels:
    name: sjb-nginx
spec:
  containers:
  - name: nginx
    image: ccr.ccs.tencentyun.com/shujubu/nginx:1.17
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: /etc/nginx/nginx.conf
      name: config
    - mountPath: /etc/nginx/conf.d/default.conf
      name: default
    - mountPath: /var/log/nginx/
      name: log
    - mountPath: /usr/share/nginx/shzr/area
      name: shzr
    - mountPath: /usr/share/nginx/zgbs/mujun
      name: mujun
    - mountPath: /usr/share/nginx/zgbs/area
      name: zgbs   restartPolicy: Never   volumes:                                                                                                                                                                                           
  - name: config
    hostPath:
      path: /home/users/qix/sjb_bs_source/nginx/nginx.conf
  - name: default
    hostPath:
      path: /home/users/qix/sjb_bs_source/nginx/conf.d/default.conf
  - name: log
    hostPath:
      path: /home/users/qix/sjb_bs_source/nginx/log/
  - name: shzr
    hostPath:
      path: /home/users/qix/sjb_bs_source/shzr/area/
  - name: mujun
    hostPath:
      path: /home/users/qix/sjb_bs_source/zgbs/mujun/
  - name: zgbs
    hostPath:
      path: /home/users/qix/sjb_bs_source/zgbs/area/

上面的pod中只定義了一個容器,并在容器中掛載了nginx的配置文件、日志文件及我們要訪問的資源文件。 定義service文件 nginx-np.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    name: sjb-nginx
  type: NodePort
  ports:
    - protocol: TCP
      port: 10008
      targetPort: 80
      nodePort: 30008

注意,service中的selector中的配置要與pod中的labels保持一致。 并附上nginx.conf中的部分配置
user  root;
worker_processes  1; events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;     gzip  on;
    gzip_min_length 1024K;   # 大于1M的壓縮
    gzip_comp_level 5;    # 壓縮級別 1-10 數(shù)字越大壓縮的越好
    gzip_types text/plain application/x-javascript text/css application/xml application/json text/javascript application/x-httpd-php image/jpeg image/gif image/png;   # 壓縮的文件類型     server {
        listen       80;
        server_name  192.168.111.36:80;
        location / {
            root /usr/share/nginx/html;
            index index.html;
        }         error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        location ^~ /shzr/area/ {
            root /usr/share/nginx/;
            autoindex on;
        }
        location ^~ /zgbs/mujun/ {
            root /usr/share/nginx/;
            autoindex on;
        }
        location ^~ /zgbs/area/ {
            root /usr/share/nginx/;
            autoindex on;
        }
    }
}
啟動pod命令:
kubectl create -f nginx-pod.yaml -n shujubu
1
啟動service命令:
kubectl create -f nginx-np.yaml -n shujubu

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI