1. 程式人生 > >k8s製作pod的yaml檔案

k8s製作pod的yaml檔案

第1章 nginx配置檔案yaml
1.1 nginx _rc檔案資料持久化(rc資源型別)

cat nginx_rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx4
spec:
   replicas: 1
   selector:
     app: nginx4
   template:
     metadata:
       labels:
         app: nginx4
     spec:
       containers:
       - name: nginx4
image: 10.0.0.10:5000/nginx:v2 ports: - containerPort: 80 volumeMounts: - mountPath: /usr/local/nginx/html #容器的站點目錄 name: web volumes: - hostPath: path: /root/test #本地(宿主機)目錄 name: web

1.2 nginx_service檔案

apiVersion: v1
kind: Service
metadata:
  name: nginx4
spec:
  type: NodePort
  ports:
   - port: 80
     nodePort: 30082
  selector:
    app: nginx4

1.3 nginx_rc檔案 (pod資源型別)不需要建立service檔案即可外部訪問

[[email protected] nginx1]# cat nginx-rc.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
containers: - name: nginx image: nginx ports: - containerPort: 80 #源埠 hostPort: 80 #宿主機對映埠

1.4 nginx資料持久化(基於nfs實現)

cat /docker/nginx_rc/nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx2
spec:
   replicas: 1
   selector:
     app: nginx2
   template:
     metadata:
       labels:
         app: nginx2
     spec:
       containers:
       - name: nginx2
         image: nginx
         ports:
         - containerPort: 80
         volumeMounts:
         - mountPath: /usr/share/nginx/html
           readOnly: false
           name: nginx-data
       volumes:
       - name: nginx-data
         nfs:
           server: 192.168.1.89
           path: "/app/www_data"

第2章 tomcat配置檔案yaml
2.1 tomcat7下載地址

http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/

2.2 構建tomcat映象(Dockerfile)

cat Dockerfile
FROM centos:6.9
MAINTAINER hmk
ENV REFRESHED_AT 2018-07-21
WORKDIR /usr/local
RUN mkdir jdk
RUN mkdir tomcat
ADD jdk1.6.0_24.tar.gz /usr/local/jdk
ADD apache-tomcat-7.0.90.tar.gz /usr/local/tomcat
ENV JAVA_HOME=/usr/local/jdk/jdk1.6.0_24
ENV JRE_HOME=$JAVA_HOME/jre
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH=/sbin:$JAVA_HOME/bin:$PATH
RUN chmod +x /usr/local/tomcat/apache-tomcat-7.0.90/bin/catalina.sh
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat/apache-tomcat-7.0.90/bin/catalina.sh","run"]

2.3 tomcat_rc檔案

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
   app: myweb
  template:
   metadata:
     labels:
       app: myweb
   spec:
     containers:
     - name: myweb
       image: docker.io/kubeguide/tomcat-app:v1
       ports:
       - containerPort: 8080

2.4 tomcat_svc檔案

apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
   - port: 8080
     nodePort: 30001
  selector:
    app: myweb

2.5 tomcat資料持久化yaml檔案(掛載多個目錄)

apiVersion: v1
kind: ReplicationController
metadata:
  name: mytomcat10
spec:
  replicas: 1
  selector:
   app: mytomcat10
  template:
   metadata:
     labels:
       app: mytomcat10
   spec:
     containers:
     - name: mytomcat10
       image: 10.0.0.10:5000/jenkins/maven
       ports:
       - containerPort: 8080
       volumeMounts:
        - mountPath: /usr/local/tomcat/apache-tomcat-8.5.23/webapps      #容器的站點地址
          name: web
        - mountPath: /usr/local/tomcat/apache-tomcat-8.5.23/conf         #容器的站點地址
          name: conf
     volumes:
      - hostPath:
         path: /data/webapps                  #宿主機的站點目錄地址
        name: web
      - hostPath:
         path: /data/conf                     #宿主機的配置檔案目錄地址
        name: conf

2.6 tomcat資料持久化(基於nfs)

cat /docker/tomcat_rc/tomcat_rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: tomcat
spec:
   replicas: 1
   selector:
     app: tomcat
   template:
     metadata:
       labels:
         app: tomcat
     spec:
       containers:
       - name: tomcat
         image: tomcat:8
         ports:
         - containerPort: 8080
         volumeMounts:
         - mountPath: /usr/local/tomcat/webapps
           readOnly: false
           name: nginx-data
       volumes:
       - name: nginx-data
         nfs:
           server: 192.168.1.89             #nfs伺服器地址
           path: "/app/www_data"            #nfs伺服器共享的目錄

第3章 redis配置檔案yaml
3.1 redis下載地址

wget http://download.redis.io/releases/redis-3.2.6.tar.gz
http://download.redis.io/releases/

3.2 構建redis映象(Dockerfile)

cat Dockerfile
# This is My first Dockerfile
# Version 1.0
# Author: ljx
# Base images
FROM centos:6.9
# MAINTAINER
MAINTAINER Jason Liu

# ADD
ADD redis-3.2.6.tar.gz /usr/local

#RUN
RUN yum -y install wget gcc gcc-c++ make vim

#WORKDIR
WORKDIR /usr/local/redis-3.2.6
RUN make
ADD redis.conf /etc/redis.conf
RUN ln -s /usr/local/redis-3.2.6/src/redis-cli /usr/bin/redis-cli
EXPOSE 6379
ENTRYPOINT [ "/usr/local/redis-3.2.6/src/redis-server","/etc/redis.conf"]
注意:redis.conf 需要redis原始檔解壓後複製到/etc目錄下,自己建立的(redis.conf)檔案不能啟動redis伺服器

3.3 redis_rc檔案 (rc資源型別) 需要配合svc.yaml檔案

apiVersion: v1
kind: ReplicationController
metadata:
  name: myredis2
spec:
  replicas: 1
  selector:
   app: myredis2
  template:
   metadata:
     labels:
       app: myredis2
   spec:
     containers:
     - name: myredis2
       image: 10.0.0.10:5000/test/redis3.2.6
       resources:
         limits:
           memory: 1024M   #分配記憶體大小
           cpu: '1'        #分配cpu數量
       ports:
         - containerPort: 6379
       env:
         - name: REDIS_AUTH_PASSWORD  #設定redis的密碼
           value: NewtouchOne123

3.4 redis_svc檔案

apiVersion: v1
kind: Service
metadata:
  name: myredis2
spec:
  type: NodePort
  ports:
   - port: 6379
     nodePort: 30013
  selector:
    app: myredis2

3.5 redis_rc檔案 (pod資源型別) 不需要svc檔案即可外部訪問

[[email protected] redis_1]# cat redis_rc.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: redis1
  labels:
    name: redis1
spec:
  containers:
  - name: redis1
    image: redis
    ports:
    - containerPort: 6379
      hostPort: 6380

3.6 redis資料持久化yaml檔案

apiVersion: v1
kind: ReplicationController
metadata:
  name: myredis1
spec:
  replicas: 1
  selector:
   app: myredis1
  template:
   metadata:
     labels:
       app: myredis1
   spec:
     containers:
     - name: myredis1
       image: 10.0.0.10:5000/redis       
       ports:
       - containerPort: 6379
       volumeMounts:
       - mountPath: /var/lib
         name: redis-data
     volumes:
      - hostPath:
         path: /data
        name: redis-data

3.7 redis配置檔案內容

[root@docker redis3.2.6]# egrep -v "^$|^#" redis.conf
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass picc
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
#slaveof 10.0.0.111 30013

第4章 mysql配置檔案yaml
4.1 mysql_rc檔案

apiVersion: v1
kind: ReplicationController    #型別為副本控制器RC
metadata:
  name: mysql                  #RC的名稱,全域性唯一
spec:
   replicas: 1                 #POD副本期待數量
   selector:
     app: mysql                #符合目標的Pod擁有此標籤
   template:                   #根據此模板建立Pod的副本
     metadata:
       labels:
         app: mysql            #Pod副本擁有的標籤,對應RC的Selector
     spec:
       containers:             #Pod內容器的定義部分
       - name: mysql           #容器的名稱
         image: mysql.io/mysql:latest    #容器對應的Docker Image
         ports:
         - containerPort: 3306           #容器應用監聽的埠號
         env:                            #注入容器內的環境變數
         - name: MYSQL_ROOT_PASSWORD
           value: "123456"
         - name: MYSQL_SERVER_ID
           value: "1"

4.2 mysql_svc檔案

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort
  ports:
   - port: 3306
     nodePort: 30014
  selector:
    app: mysql

4.3、mysql資料持久化 (基於nfs)

cat mysql_rc.yaml
apiVersion: v1                     #版本號
kind: ReplicationController        #使用rc資源型別
metadata:
  name: mymysql                    #pod的名稱
spec:
  replicas: 1                      #啟動副本的數量
  selector:
   app: mymysql
  template:
   metadata:
     labels:
       app: mymysql
   spec:
     containers:
     - name: mymysql            #自定義映象名稱
       image: mysql:5.6.20      #映象資訊
       ports:
       - containerPort: 3306    #映象的埠號
       env:                     #定義環境變數
       - name: MYSQL_ROOT_PASSWORD    #定義mysql的root密碼
         value: "123456"              #root密碼
       volumeMounts:                  
       - mountPath: /var/lib/mysql    #容器的掛載路徑
         readOnly: false              #可讀可寫
         name: mysql1-data            #自定義名稱
     volumes:
       - name: mysql1-data            #宿主機的自定義名稱
         nfs:                         #使用宿主機的nfs方式掛載
           server: 192.168.1.89       #nfs伺服器地址
           path: "/app/mysql1_data"   #nfs共享的路徑

4.4、mysql配置檔案持久化

apiVersion: v1
kind: ReplicationController
metadata:
  name: mymysql
spec:
  replicas: 1
  selector:
   app: mymysql
  template:
   metadata:
     labels:
       app: mymysql
   spec:
     containers:
     - name: mymysql
       image: mysql:5.6.20
       ports:
       - containerPort: 3309         #修改了mysql預設埠微3309
       env:
       - name: MYSQL_ROOT_PASSWORD
         value: "123456"
       volumeMounts:
        - mountPath: /etc/my.cnf
          name: conf
     volumes:
      - hostPath:
         path: /data/my.cnf
        name: conf

第5章 rabbitmq配置檔案yanl
5.1、rabbitmq_rc檔案內容如下:

cat rabbitmq-rc.yml
--------------------------------------------------------------
apiVersion: extensions/v1beta1
#kind: Deployment
kind: ReplicationController
metadata:
  name: rabbitmq
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      hostname: rabbitmq
      containers:
        - name: rabbitmq
          image: rabbitmq
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5672
            - containerPort: 15672
          env:
            - name: RABBITMQ_PASS
              value: admin
      nodeSelector:
        FUN: "SERVICE"

5.2、rabbitmq_svc檔案內容如下:

cat rabbitmq-service.yml
----------------------------------------------------------------------
apiVersion: v1
kind: Service
metadata:
  name: rabbitmq
  namespace: default
  labels:
    app: rabbitmq
spec:
  selector:
    app: rabbitmq
  type: NodePort
  ports:
    - name: amqp
      port: 5672
      nodePort: 31111
    - name: web
      port: 15672
      nodePort: 31112

第6章 zookeeper配置檔案yaml
6.1、zookeeper_rc檔案內容如下:

[[email protected] zookeeper_rc]# cat zookeeper-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: zookeeper-test
spec:
   replicas: 1
   selector:
     app: zookeeper-test
   template:
     metadata:
       labels:
         app: zookeeper-test
     spec:
       containers:
       - name: zookeeper-test
         image: 192.168.1.201:5000/zookeeper
         ports:
         - containerPort: 2181
         ports:
         - containerPort: 2888

6.2、zookeeper_svc檔案內容如下:

[[email protected] zookeeper_rc]# cat zookeeper-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: zookeeper
spec:
  type: NodePort
  ports:
   - port: 2181
     nodePort: 30005
  selector:
    app: zookeeper

第7章 gitlab配置檔案yaml
7.1、gitlab_deply檔案內容如下:

cat one-gitlab-deployment.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gitlab
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: gitlab
    spec:
      hostname: gitlab
      containers:
        - name: gitlab
          image: gitlab-ce
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
      nodeSelector:
        FUN: "SERVICE"

7.2、gitlab_svc檔案內容如下:

cat one-gitlab-service.yml 
apiVersion: v1
kind: Service
metadata:
  name: gitlab
  namespace: default
  labels:
    app: gitlab
spec:
  selector:
    app: gitlab
  type: NodePort
  ports:
    - port: 80
      nodePort: 30010