k8s搭建一个基于ingress,service,pv,pvc,deployment等的nginx项目
2022/2/7 7:12:38
本文主要是介绍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 发布到互联网即可验证
这篇关于k8s搭建一个基于ingress,service,pv,pvc,deployment等的nginx项目的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23云原生周刊:利用 eBPF 增强 K8s
- 2024-12-20/kubernetes 1.32版本更新解读:新特性和变化一目了然
- 2024-12-19拒绝 Helm? 如何在 K8s 上部署 KRaft 模式 Kafka 集群?
- 2024-12-16云原生周刊:Kubernetes v1.32 正式发布
- 2024-12-13Kubernetes上运行Minecraft:打造开发者平台的例子
- 2024-12-12深入 Kubernetes 的健康奥秘:探针(Probe)究竟有多强?
- 2024-12-10运维实战:K8s 上的 Doris 高可用集群最佳实践
- 2024-12-022024年最好用的十大Kubernetes工具
- 2024-12-02OPA守门人:Kubernetes集群策略编写指南
- 2024-11-26云原生周刊:K8s 严重漏洞