微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

k8s搭建一个基于ingress,service,pv,pvc,deployment等的nginx项目

架构图:

 示例:

[root@master ~]# cat webcluster.yaml 
---
kind: PersistentVolume
apiVersion: v1
Metadata:
  name: pv-nfs
spec:
  volumeMode: Filesystem
  capacity:
    storage: 30Gi
  accessModes:
  - ReadWriteOnce
  - ReadOnlyMany
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.1.100
    path: /var/webroot

---
kind: PersistentVolumeClaim
apiVersion: v1
Metadata:
  name: pvc-nfs
spec:
  volumeMode: Filesystem
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 25Gi

---
kind: ConfigMap
apiVersion: v1
Metadata:
  name: Nginx-conf
data:
  Nginx.conf: |2

    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/Nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       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  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        server {
            listen       80;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html;
                index  index.html index.htm;
            }

            #error_page  404              /404.html;

            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }

            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.PHP$ {
            #    proxy_pass   http://127.0.0.1;
            #}

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            location ~ \.PHP$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.PHP;
                include        fastcgi.conf;
            }

            # deny access to .htaccess files, if Apache's document root
            # concurs with Nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }


        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}


        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

    }

---
kind: Deployment
apiVersion: apps/v1
Metadata:
  name: webNginx
spec:
  selector:
    matchLabels:
      myapp: Nginx
  replicas: 3
  template:
    Metadata:
      labels:
        myapp: Nginx
    spec:
      volumes:
      - name: Nginx-PHP
        configMap: 
          name: Nginx-conf
      - name: log-data
        hostPath:
          path: /var/log/weblog
          type: DirectoryOrCreate
      - name: website
        persistentVolumeClaim:
          claimName: pvc-nfs
      containers:
      - name: Nginx
        image: 192.168.1.100:5000/myos:Nginx
        volumeMounts:
        - name: Nginx-PHP
          subPath: Nginx.conf
          mountPath: /usr/local/Nginx/conf/Nginx.conf
        - name: log-data
          mountPath: /usr/local/Nginx/logs
        - name: website
          mountPath: /usr/local/Nginx/html
        ports:
        - protocol: TCP
          containerPort: 80
      - name: PHP-backend
        image: 192.168.1.100:5000/myos:PHP-fpm
        volumeMounts:
        - name: website
          mountPath: /usr/local/Nginx/html
      restartPolicy: Always

---
kind: Service
apiVersion: v1
Metadata:
  name: webcluster
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    myapp: Nginx
  type: ClusterIP

---
apiVersion: extensions/v1beta1
kind: Ingress
Metadata:
  name: myweb
  annotations:
    kubernetes.io/ingress.class: "Nginx"
spec:
  backend:
    serviceName: webcluster
    servicePort: 80

[root@master ~]# kubectl apply -f webcluster.yaml 
persistentvolume/pv-nfs created
persistentvolumeclaim/pvc-nfs created
configmap/Nginx-conf created
deployment.apps/webNginx created
service/webcluster created
ingress.extensions/myweb created
[root@master ~]# kubectl get pod
NAME                       READY   STATUS    RESTARTS   AGE
webNginx-647877b59-hdb64   2/2     Running   0          11s
webNginx-647877b59-ljb6g   2/2     Running   0          11s
webNginx-647877b59-rqmdr   2/2     Running   0          11s
[root@master ~]# kubectl get ingresses
NAME    HOSTS   ADDRESS        PORTS   AGE
myweb   *       192.168.1.31   80      17s
[root@master ~]# # 给 node-0001 绑定弹性公网IP,或使用 ELB 发布到互联网即可验证

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐