新建k8s nginx容器(需要外网访问)
第一步,创建deploy
apiVersion: extensions/v1beta1 #k8s版本不同,api可能也不同 kind: Deployment metadata: name: mynginx namespace: default spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: name: web labels: app: nginx spec: containers: - name: nginx image: nginx ports: - name: nginx containerPort: 80 volumeMounts: - name: html-config mountPath: /etc/nginx/conf.d/nginx.conf subPath: nginx.conf readOnly: true - name: nginx-logs mountPath: /var/log/nginx/ volumes: - name: html-config configMap: name: nginx.conf - name: nginx-logs hostPath: path: /test-nginx/logs/
2.创建configmap文件挂载到容器中
kubectl create cm nginx.conf --from-file=./nginx.conf
nginx.conf内容如下
server{ listen 80; server_name test-nginx.test.net; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log error; location / { return 200; } }
3.创建ingress,使nginx可以通过域名访问
apiVersion: extensions/v1beta1 #k8s版本不同,api可能也不同 kind: Ingress metadata: name: test-nginx namespace: default spec: rules: - host: test-nginx.test.net #域名 http: paths: - backend: serviceName: service-nginx #关联后端的svc servicePort: 80 #svc的端口号 status: loadBalancer: ingress: - ip: xxx.xxx.xxx.xxx #阿里云的k8s的负载均衡地址
4.添加阿里云dns域名解析
5.现在就可以在外放访问域名了