欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kubernetes 部署 Nginx 集群

程序员文章站 2022-03-19 16:53:07
...
一.设置标签
为了保证nginx之能分配到nginx服务器需要设置标签,设置标签可以让Pod选择该服务器部署
#设置标签 key为type 的 value为nginx
# kubectl label node k8s-node1  type=nginx
# kubectl label node k8s-node2  type=nginx
#查看标签
# kubectl get node --show-labels


二.创建nginx-deployment的yaml文件
nginx-deployment.yaml

### define deployment info ###
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
  labels:
    app: nginx
### define pod info ###
spec:
# define the pod count
  replicas: 2
  revisionHistoryLimit: 5  #保存5个历史版本
# select the pod which is controlled
  selector:
    matchLabels:
      app: nginx
# define the pod
  template:
    metadata:
# define the pod label,which must be same with selector's matchLabels
      labels:
        app: nginx
        env: dev
        tie: front
### define container info ###
    spec:
      containers:
# define the container name
      - name: nginx
        image: nginx:1.17.5
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: conf
          mountPath: /etc/nginx
        - name: opt
          mountPath: /opt
      #node label selector
      nodeSelector:
        type: nginx
      volumes:
      - name: conf
        hostPath:
          path: /etc/nginx
          type: Directory
      - name: opt
        hostPath:
          path: /opt
          type: Directory


三、应用启动
# kubectl create -f nginx-deployment.yaml

# kubectl get pod -o wide

四.创建nginx-service的yaml文件

nginx-service.yaml

apiVersion: v1
# delcare it's service
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
spec:
  externalIPs:
  - 192.168.10.80
  - 192.168.10.81
  - 192.168.10.82
  ports:
# define the service's port
  - port: 80
    name: nginx-service-80 
    protocol: TCP
# define the container's port
    targetPort: 80
# nodePort 对应 type: NodePort
#    nodePort: 30080  
# define the pod label which the selector match with
  selector:
    app: nginx
# three type : ClusterIP,NodePort,LoadBalancer
  type: ClusterIP

 
五、应用启动
# kubectl create -f nginx-service.yaml

# kubectl get service -o wide

六、访问Nginx
http://192.168.10.81


附录:
查找.yaml 的所有文件
find / -name *.yaml









相关标签: nginx