Kubernetes部署集群Nginx


基礎配置

主機名 IP 系統版本
k8s-master 172.21.3.20 CentOS7.8
k8s-node1 172.21.3.21 CentOS7.8
k8s-node2 172.21.3.22 CentOS7.8
 

創建namespace命名空間

kubectl create namespace nginx

創建掛載nginx.conf配置文件

cat nginx.conf 
#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server_tokens off;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }


}
#掛載nginx.conf 配置文件
kubectl create configmap nginx-conf --from-file=./nginx.conf -n nginx

創建Pod

cat nginx-rc1.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: 172.21.3.23/nginx/nginx1.19.3
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/local/nginx/conf/nginx.conf
          name: nginx-conf
          subPath: nginx.conf
      volumes:
      - configMap:
          name: nginx-conf
        name: nginx-conf
#創建Pod容器
kubectl apply -f nginx-rc1.yaml -n nginx

創建Service

  cat nginx-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
    - port: 8010
      targetPort: 80
      protocol: TCP
      nodePort: 30080    #外網訪問端口
  type: NodePort   #這個是端口類型
  selector:
    name: nginx
#創建Service
kubectl apply -f nginx-svc.yaml -n nginx

測試

訪問:http://172.21.3.20:30080


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM