To prepare

Prepare the K8S cluster in advance, package your project and upload it to the Docker repository

Yaml files

K8S YAML file can be written by hand (not recommended), can also be generated by Kubectl create, here is not much to say, online related articles more is… Here is my YAML file. test-deployment.yaml

--- apiVersion: apps/v1 kind: Deployment metadata: name: test-k8s namespace: default labels: cloud-service-module: test app: test-k8s spec: replicas: 3 selector: matchLabels: cloud-service-module: test app: test-k8s template: metadata: labels: app: test-k8s cloud-service-module: test spec: restartPolicy: Always containers: - name: ImagePullPolicy: "ifNotPresent" envFrom: -configMapRef: name: "test-k8s" imagePullPolicy: "ifNotPresent" test-k8s-configmap ports: - name: tomcat containerPort: 8080 readinessProbe: tcpSocket: port: 8080 initialDelaySeconds: 5 periodSeconds: 10

test-configmap.yaml

API version: v1 kind: configMap metadata: name: test-configMap data: # API version: v1 kind: configMap metadata: name: test-configMap data: # 172.20.151.111 test_port: "5555" # Note that if the value is a number, it must be enclosed in double quotes, otherwise it will return an error

test-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: test-service
  namespace: default
  labels:
    cloud-service-module: test
    cloud-service-type: java
    cloud-service-category: backend
spec:
  type: ClusterIP
  ports:
  - name: tomcat
    port: 8080
    targetPort: 8080

The deployment of

Log in to the Docker warehouse

In your K8S cluster, you need to log in the repository where the project image is located. If you do not perform this operation, the image will fail to pull when creating POD!!

Docker login --username= XXX

See below is a successful login

Note that the following problems may occur when logging into the Docker repository

Error response from daemon: Get https://xxx: dial tcp xxx:443: connect: connection refused

The solution

Vi/usr/lib/systemd/system/docker. Service # add -- insecure - registry = mysql. Gift ExecStart = / usr/bin/dockerd -h fd: / / -- containerd = / run/containerd containerd. The sock -- insecure - registry = mysql. Gift # execute commands systemctl daemon - reload systemctl Restart docker # check the success of ps - ef | grep docker [root @ k8s rac-node1 ~] # ps - ef | grep docker 7730 1 0 09:26 root? 00:00:20 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry=xxx

Login to the docker warehouse again

Create a pod

Execute commands separately:

kubectl apply -f test-service.yaml
kubectl apply -f test-configmap.yaml
kubectl apply -f test-deployment.yaml

At last,kubectl get pod,svcThe command verifies that the deployment was successful

At this point, our Java project is deployed successfully!

Refer to the article: https://blog.csdn.net/java_w/…