原文:https://www.cnblogs.com/zeng666/p/16622586.html
-----------------
(k8s)Kubernetes 部署Nginx
系統架構參考圖:
一、環境準備:
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
#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
#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
[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 才支持。