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 #两个 pod, 这里我认为是有几个 node 就建个 pod 吧? 不然会不均衡
- selector:
- name: nginx
- template:
- metadata:
- labels:
- name: nginx
- spec:
- containers:
- - name: nginx
- image: docker.io/nginx:alpine #镜像, 建议现在 Node 先下好.
- ports:
- - containerPort: 80
- volumeMounts:
- - mountPath: /etc/nginx/nginx.conf # 容器内的配置文件
- name: nginx-config
- subPath: nginx.conf #当前 master 宿主机目录的文件, 会挂载到容器内的 / etc/nginx/nginx.conf, 文件挂载会用到 configmap
- - mountPath: /home/wwwroot/test #容器内的目录, 会自动新建.
- name: nginx-data
- volumes:
- - name: nginx-config
- configMap:
- name: confnginx #对应 configmap 创建的名字
- - name: nginx-data
- hostPath:
- path: /home/wwwroot/hello #各个 Node 中目录, 会挂载到容器内的 / home/wwwroot/test, 在各个 Node 手动新建.
- 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
五, 验证
来源: http://www.bubuko.com/infodetail-3358084.html