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-2025 CODEPRJ.COM