系统架构参考图:
一、环境准备:
master: 10.0.0.7
node1: 10.0.0.17
node2: 10.0.0.27
[19:45:37 root@k8s-master ~]#kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 11d v1.18.0
k8s-worker-node1 Ready <none> 10d v1.18.0
k8s-worker-node2 Ready <none> 10d v1.18.0
二、创建configMap:
#nginx.conf:
[19:55:28 root@k8s-master ~]#cat 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 /data/nginx/test;
index index.html;
}
}
[19:55:42 root@k8s-master ~]#kubectl create configmap nginx-conf --from-file nginx.conf
configmap/nginx-conf created
[19:57:37 root@k8s-master ~]#kubectl get configmap
NAME DATA AGE
nginx-conf 1 40s
#在两个Node上新建我们的web访问目录:
[19:45:08 root@k8s-worker-node1 ~]#mkdir /data/nginx/test -p
[20:08:10 root@k8s-worker-node2 ~]#mkdir /data/nginx/test -p
#配置一下页面显示,区分:
[20:14:52 root@k8s-worker-node1 ~]#echo "hello,world,This is 10.0.0.17" > /data/nginx/test/index.html
[20:14:59 root@k8s-worker-node1 ~]#cat /data/nginx/test/index.html
hello,world,This is 10.0.0.17
[20:16:19 root@k8s-worker-node2 ~]#echo "hello,world,This is 10.0.0.27" > /data/nginx/test/index.html
[20:16:20 root@k8s-worker-node2 ~]#cat /data/nginx/test/index.html
hello,world,This is 10.0.0.27
三、创建nginx-rc.yaml:
#Replication Controller简称RC,它能够保证Pod持续运行,并且在任何时候都有指定数量的Pod副本,在此基础上提供一些高级特性,比如滚动升级和弹性伸缩。
[20:23:57 root@k8s-master ~]#cat nginx-rc.yaml
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: /data/nginx/test
name: nginx-data
volumes:
- name: nginx-config
configMap:
name: nginx-conf
- name: nginx-data
hostPath:
path: /data/nginx/test
[20:24:50 root@k8s-master ~]#kubectl create -f nginx-rc.yaml
replicationcontroller/nginx-controller created
四、创建nginx-svc.yaml :
[20:29:54 root@k8s-master ~]#cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service-nodeport
spec:
ports:
- port: 8080
targetPort: 80
protocol: TCP
nodePort: 30010 #外网访问端口
type: NodePort #端口类型
selector:
name: nginx
[20:30:44 root@k8s-master ~]#kubectl create -f nginx-svc.yaml
service/nginx-service-nodeport created
[20:31:26 root@k8s-master ~]#kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-controller-nmj77 1/1 Running 0 4m39s 10.244.2.6 k8s-worker-node2 <none> <none>
nginx-controller-nrt6m 1/1 Running 0 4m39s 10.244.1.2 k8s-worker-node1 <none> <none>
[20:32:08 root@k8s-master ~]#kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d <none>
nginx-service-nodeport NodePort 10.110.21.224 <none> 8080:30010/TCP 2m name=nginx
#浏览器访问任意node节点+30010端口访问测试查看:
[20:48:23 root@k8s-master ~]#cat /data/nginx/index.html
cat: /data/nginx/index.html: 没有那个文件或目录
#这边master节点没有创建目录,以及对应的文件,但是访问master节点时仍然能访问成功!此时被调度到17节点或者27节点轮询!
#修改html内容。属刷新看看测试效果:
[20:58:56 root@k8s-worker-node1 test]#echo "hello,world,This is 10.0.0.17 v1.0" > /data/nginx/test/index.html
[20:59:20 root@k8s-worker-node2 test]#echo "hello,world,This is 10.0.0.27 v1.0" > /data/nginx/test/index.html
#附知识扩展:
service种类:
#【k8s】Service种类、类型(ClusterIP、NodePort、LoadBalancer、ExternalshName) service的几种类型
#ClusterIP:通过集群的内部 IP 暴露服务,选择该值,服务只能够在集群内部可以访问,这也是默认的ServiceType。
#NodePort:通过每个 Node 节点上的 IP 和静态端口(NodePort)暴露服务。NodePort 服务会路由到 ClusterIP 服务,这个 ClusterIP 服务会自动创建。通过请求 :,可以从集群的外部访问一个 NodePort 服务。
(#NodePort 需要借助真实存在的ip,是一个公共的ip,任何人都可以访问,而ClusterIP可以理解成不对外开放,仅限于集群内的节点之间特定的一个范围)
#LoadBalancer:使用云提供商的负载局衡器,可以向外部暴露服务。外部的负载均衡器可以路由到 NodePort 服务和 ClusterIP 服务,这个需要结合具体的云厂商进行操作。
#ExternalName:通过返回 CNAME 和它的值,可以将服务映射到 externalName 字段的内容(例如, foo.bar.example.com)。没有任何类型代理被创建,这只有 Kubernetes 1.7 或更高版本的 kube-dns 才支持。
标签:20,Kubernetes,nginx,data,Nginx,master,k8s,root From: https://www.cnblogs.com/zeng666/p/16622586.html