1.拉取jdk镜像

docker pull openjdk:8-jdk

2.Dockerfile

cat > Dockerfile <<'EOF'
# 基础镜像
FROM openjdk:8-jdk


ADD ./*.jar /app.jar

RUN ln -sf /usr/share/zoneinfo/Asia/ShangHai /etc/localtime \
   && echo Asia/Shanghai > /etc/timezone \
   && dpkg-reconfigure -f noninteractive tzdata

ENTRYPOINT ["java", "-jar", "/app.jar"]

EOF

3.构建镜像

docker build -t java-test:0.1  .

4.deploy.yaml

cat > deploy.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: java-test
  template:
    metadata:
      labels:
        app: java-test
    spec:
      containers:
        - env:
          - name: SPRING_PROFILES_ACTIVE
            value: "prod"        
          name: java-test
          image: java-test:0.1
          imagePullPolicy: Never
          ports:
          - containerPort: 8080
            protocol: TCP
      imagePullSecrets:
       - name: myregistrykey
EOF

注 : 8080为容器内部运行端口 

5.service.yaml

cat > service.yaml <<'EOF'
kind: Service
apiVersion: v1
metadata:
  labels:
    app: java-test-service
  name: java-test-service
  namespace: test
spec:

  type: NodePort
  ports:
  - name: http
    port: 8080
    nodePort: 30611
    protocol: TCP
    targetPort: 8080
  selector:
    app: java-test
EOF

注 : 8080为容器内部运行端口,30611为外部访问端口

6.创建

kubectl create ns test
kubectl apply -f deploy.yaml -n test
kubectl apply -f service.yaml -n test

7.查看

kubectl get all -n test|grep java-test

Logo

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

更多推荐