之前写过spring boot项目迁移到k8s,结果昨天用了之前的yaml发现ingress有点问题,现在修复一下重新的yaml如下

---
apiVersion: v1
kind: Service
metadata:
 name: boss-service
 namespace: pre
 labels:
  version: "1.0"
spec:
 # nodePort暴露服务
 type: NodePort
 ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
    # nodePort: 30411
 selector:
  name: boss
  version: "1.0"

---

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: boss-ingress
  namespace: pre
spec:
  rules:
  # 访问路径
  - host: pre.boss.com
    http:
      paths:
      - path: /
        backend:
          serviceName: boss-service
          servicePort: 8080
---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: boss
 namespace: pre
 labels:
  version: "1.0"
spec:
 replicas: 1
 # selector:
  # matchExpressions:
  #  - {
  #     key: name, operator: In, values: [boss],
  #     key: version, operator: In, values: ["1.0"]
  #    }
 # 滚动更新策略
 strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
 template:
  metadata:
   labels:
    name: boss
    version: "1.0"
  spec: 
   # serviceAccountName: pre
   # 优雅关闭时间
   terminationGracePeriodSeconds: 30
   containers: 
   - name: boss
     image: springboot-application
     imagePullPolicy: IfNotPresent
     resources:
      requests:
        memory: 1Gi
     livenessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 10
      periodSeconds: 60
     ports:
     - containerPort: 8080
       name: web
       protocol: TCP
     - containerPort: 8181
       name: Heartbeat
       protocol: TCP
     env:
     - name: MYSQL_ROOT_PASSWORD
       value: "password"
     - name: SPRING_PROFILES_ACTIVE
       value: "test"

另外加上mysql的yaml

##namespace
---
#apiVersion: v1
#kind: Namespace
#metadata:
#  name: pre
##mysql-claim.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-ex-claim
  namespace: pre
  annotations:
    volume.beta.kubernetes.io/storage-class: "nfs" 
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi

##mysql-cm.yaml 
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: pre
data:
  custom.cnf: |
        [mysqld]
        server-id=1
        log-bin
        expire_logs_days=7
        sync_binlog=0
        binlog_cache_size=1M

##mysql-secret.yaml
###  echo -n hello |base64
---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-user-pwd
  namespace: pre
data:
  #mysql-root-pwd: NXN0YXI=
  mysql-root-pwd: NXN0YXI=
  #mysql-app-user-pwd: NXN0YXI=
  mysql-app-user-pwd: NXN0YXI=
  #mysql-test-user-pwd: NXN0YXI= 
  mysql-test-user-pwd: NXN0YXI=
##mysql-deployment.yaml
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: pre
  labels:
    app: mysql-ex
  name: mysql-ex
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql-ex
  template:
     metadata:
       labels:
         app: mysql-ex
     spec:
       initContainers:
       - name: mysql-init
         image: busybox
         imagePullPolicy: IfNotPresent
         env:
         - name: MYSQL_TEST_USER_PASSWORD
           valueFrom:
             secretKeyRef:
               name: mysql-user-pwd
               key: mysql-test-user-pwd
         command:
           - sh
           - "-c"
           - |
             set -ex
             rm -rf /var/lib/mysql/lost+found
             cat > /docker-entrypoint-initdb.d/mysql-boss-initt.sql <<EOF
             create database boss default character set utf8;
             grant all on boss.* to 'test'@'%' identified by '$MYSQL_TEST_USER_PASSWORD';
             flush privileges;
             EOF
             cat > /docker-entrypoint-initdb.d/mysql-appdb-init.sql <<EOF
             create table app(id int);
             insert into app values(1);
             commit;
             EOF
         volumeMounts:
         - name: mysql-data
           mountPath: /var/lib/mysql
         - name: mysql-initdb
           mountPath: /docker-entrypoint-initdb.d
       containers:
       #这边可以更改image: mysql:5.7
         - image: mysql:5.7
           name: mysql
           imagePullPolicy: IfNotPresent
           env:
           - name: MYSQL_ROOT_PASSWORD
             valueFrom:
               secretKeyRef:
                 name: mysql-user-pwd
                 key: mysql-root-pwd
           - name: MYSQL_PASSWORD
             valueFrom:
               secretKeyRef:
                 name: mysql-user-pwd
                 key: mysql-app-user-pwd
           - name: MYSQL_USER
             value: app
           - name: MYSQL_DATABASE
             value: appdb
           volumeMounts:
           - name: mysql-data
             mountPath: /var/lib/mysql
           - name: mysql-initdb
             mountPath: /docker-entrypoint-initdb.d
           - name: mysql-config
             mountPath: /etc/mysql/conf.d/
           ports:
           - name: mysql
             containerPort: 3306
           livenessProbe:
             exec:
               command:
               - /bin/sh
               - "-c"
               - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
               - mysql -h 127.0.0.1 -u root -e "SELECT 1"
             initialDelaySeconds: 30
             timeoutSeconds: 5
             successThreshold: 1
             failureThreshold: 3
           readinessProbe:
             exec:
               command:
                 - /bin/sh
                 - "-c"
                 - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
                 - mysql -h 127.0.0.1 -u root -e "SELECT 1"
             initialDelaySeconds: 10
             timeoutSeconds: 1
             successThreshold: 1
             failureThreshold: 3
       volumes:
         - name: mysql-data
           persistentVolumeClaim:
             claimName: mysql-ex-claim
         - name: mysql-initdb
           emptyDir: {}
         - name: mysql-config
           configMap:
             name: mysql-config
##services
---
apiVersion: v1
kind: Service
metadata:
  #name: mysql-ex
  name: pre-db
  namespace: pre
spec:
  selector:
    app: mysql-ex
  ports:
  - port: 3306
    # nodePort: 33016
    protocol: TCP
    port: 3306
    targetPort: 3306
  type: NodePort

rabbitmq的yaml

---
apiVersion: v1
kind: Service
metadata:
  # Expose the management HTTP port on each node
  name: rabbitmq-management
  namespace: pre
  labels:
    app: rabbitmq
spec:
  ports:
  - port: 15672
    name: http
  selector:
    app: rabbitmq
  type: NodePort # Or LoadBalancer in production w/ proper security
---
apiVersion: v1
kind: Service
metadata:
  # The required headless service for StatefulSets
  name: rabbitmq
  namespace: pre
  labels:
    app: rabbitmq
spec:
  ports:
  - port: 5672
    name: amqp
  - port: 4369
    name: epmd
  - port: 25672
    name: rabbitmq-dist
  clusterIP: None
  selector:
    app: rabbitmq
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: rabbitmq
  namespace: pre
spec:
  serviceName: "rabbitmq"
  replicas: 3
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      containers:
      - name: rabbitmq
        image: rabbitmq:3.7-rc-management
        lifecycle:
          postStart:
            exec:
              command:
              - /bin/sh
              - -c
              - >
                if [ -z "$(grep rabbitmq /etc/resolv.conf)" ]; then
                  sed "s/^search \([^ ]\+\)/search rabbitmq.\1 \1/" /etc/resolv.conf > /etc/resolv.conf.new;
                  cat /etc/resolv.conf.new > /etc/resolv.conf;
                  rm /etc/resolv.conf.new;
                fi;
                until rabbitmqctl node_health_check; do sleep 1; done;
                if [ -z "$(rabbitmqctl cluster_status | grep rabbitmq-0)" ]; then
                  touch /gotit
                  rabbitmqctl stop_app;
                  rabbitmqctl reset;
                  rabbitmqctl join_cluster rabbit@rabbitmq-0;
                  rabbitmqctl start_app;
                else
                  touch /notget
                fi;
        env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: RABBITMQ_ERLANG_COOKIE
          value: "YZSDHWMFSMKEMBDHSGGZ"
        - name: RABBITMQ_NODENAME
          value: "rabbit@$(MY_POD_NAME)"
        ports:
        - containerPort: 5672
          name: amqp

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐