Kubernetes(k8s) 配置 Nginx


k8s 安裝教程請參考:https://www.cnblogs.com/spll/p/10033316.html

Nginx安裝配置參考: https://www.cnblogs.com/puroc/p/5764330.html

 

一、環境配置:

master:192.168.0.38

node1:    192.168.0.39

node2:    192.168.0.40

安裝完后用 kubectl get node 查看一下

二、創建configMap

kubectl create configmap confnginx --from-file nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

server {
       listen                    80;
       server_name               localhost;
       root                      /home/wwwroot/test;
       index                     index.html;
}
}

  

 

在兩個Node上新建我們的web訪問目錄: 

 

 

 

 

三、創建nginx-rc.yaml

Replication Controller簡稱RC,它能夠保證Pod持續運行,並且在任何時候都有指定數量的Pod副本,在此基礎上提供一些高級特性,比如滾動升級和彈性伸縮。

 

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: docker.io/nginx:alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginx-config
          subPath: nginx.conf
        - mountPath: /home/wwwroot/test
          name: nginx-data
      volumes:
        - name: nginx-config
          configMap:
            name: confnginx
        - name: nginx-data
          hostPath:
            path: /home/wwwroot/hello

 

kubectl create -f nginx-rc.yaml

 

 

 

四、創建nginx-svc.yaml

 

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  ports:
    - port: 8000
      targetPort: 80
      protocol: TCP
      nodePort: 30080    #外網訪問端口
  type: NodePort   #這個是端口類型
  selector:
    name: nginx

 

  

kubectl create -f nginx-service.yaml

 

 

五、驗證

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM