k8s dockerk个人学习(2)

部署nginx和应用

部署nginx时我们需要把nginx的配置文件那出来便于以后修改nginx的配置,放置nginx配置文件时我们用k8s的pv的存储方式来存放这些文件

1.安装nfs服务,在kube-node2上安装

  a.安装命令 sudo apt install nfs-kernel-server

  b.配置nfs命令  sudo vi /etc/exports 

  添加内容保存退出 /mnt/nfs1  *(rw,sync,no_subtree_check,no_root_squash) 

   创建目录 sudo mkdir /mnt/nfs1

        设置目录权限 sudo chmod +777 /mnt/nfs1

       重启nfs服务  sudo service nfs-kernel-server restart

2.kube-node3,kube-node4安装nfs客户端

  sudo apt install nfs-common 

  在其他2台从机器上挂载

  mount 10.64.10.2:/mnt/nfs1  /mnt/nfs1 

3.nginx pv等pod创建

 将下面文件复制到/mnt/nfs1 命名nginx.yaml

# persistentVolumeReclaimPolicy: Recycle
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv-log
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-nginx
  nfs:
    path: /nfsdata/nginx/log
    server: 10.64.4.2


---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv-html
spec:
  capacity:
    storage: 200M
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-nginx
  nfs:
    path: /nfsdata/nginx/html
    server: 10.64.4.2


---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv-cfg
spec:
  capacity:
    storage: 100M
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-nginx
  nfs:
    path: /nfsdata/nginx/cfg
    server: 10.64.4.2


---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pvc-log
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: nfs-nginx


---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pvc-html
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 200M
  storageClassName: nfs-nginx


---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pvc-cfg
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100M
  storageClassName: nfs-nginx


---
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
  labels:
     app: nginx
spec:
  replicas: 3   # Pod副本数
  selector:
    app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-html
        - mountPath: /etc/nginx/
          name: nginx-cfg
        - mountPath: /var/log/nginx/
          name: nginx-log
      volumes:
        - name: nginx-html
          persistentVolumeClaim:
            claimName: nginx-pvc-html
        - name: nginx-cfg
          persistentVolumeClaim:
            claimName: nginx-pvc-cfg
        - name: nginx-log
          persistentVolumeClaim:
            claimName: nginx-pvc-log


---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  sessionAffinity: ClientIP
  selector:
    app: nginx
  ports:
    - port: 80
      nodePort: 30080

  执行命令kubectl apply -f nginx.yml

访问10.64.4.3:30080可以看到nginx页面

 

posted @ 2019-03-09 14:39  sea101  阅读(237)  评论(0编辑  收藏  举报