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

k8s制作pod的yaml文件

程序员文章站 2024-03-09 10:08:47
...

第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