Docker+K8S cluster environment construction and distributed application deployment

Docker+K8S cluster environment construction and distributed application deployment

1. Install Docker

yum install docker
#Start the service systemctl start docker.service
systemctl enable docker.service
#Test docker version

2. Install etcd

yum install etcd -y
#Start etcd
systemctl start etcd
systemctl enable etcd
#Enter the following command to check the health status of etcd etcdctl -C http://localhost:2379 cluster-health
#Install Kubernetes
yum install kubernetes -y

After installation, edit the file /etc/kubernetes/apiserver and remove the ServiceAccount after KUBE_ADMISSION_CONTROL, such as:

KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

Then start the following programs (Master):

systemctl start kube-apiserver
systemctl enable kube-apiserver
systemctl start kube-controller-manager
systemctl enable kube-controller-manager
systemctl start kube-scheduler
systemctl enable kube-scheduler

Next, start the Node program:

systemctl start kubelet
systemctl enable kubelet
systemctl start kube-proxy
systemctl enable kube-proxy

In this way, a simple K8S cluster environment has been built. We can run the following command to view the cluster status.


However, the cluster environment does not work well at present, because the network of pods in the cluster needs to be managed uniformly, so an overlay network flannel needs to be created.

1. Install flannel:

yum install flannel -y

2. Edit the file /etc/sysconfig/flanneld and add the following code:

--logtostderr=false --log_dir=/var/log/k8s/flannel/ --etcd-prefix=/atomic.io/network --etcd-endpoints=http://localhost:2379 --iface=enp0s3

The -iface corresponds to the name of the network card.

3. Configure the key for flanneld in etcd

Flannel uses etcd for configuration to ensure configuration consistency among multiple flannel instances, so the following configuration needs to be performed on etcd:

etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'

/atomic.io/network/config This key corresponds to the configuration item FLANNEL_ETCD_PREFIX in /etc/sysconfig/flannel above. If it is wrong, the startup will fail.)

Network is used to configure the network segment. It cannot conflict with the physical machine IP. It can be defined arbitrarily, and try to avoid the physical machine IP segment.

4. Start the modified flannel, and restart docker and kubernete in turn:

systemctl enable flanneld 
systemctl start flanneld
service docker restart
systemctl restart kube-apiserver
systemctl restart kube-controller-manager
systemctl restart kube-scheduler
systemctl enable flanneld
systemctl start flanneld
service docker restart
systemctl restart kubelet
systemctl restart kube-proxy

In this way, when we deploy the application into a Docker container, we can access the container through the physical IP.

Distributed application deployment

1. Build a framework based on SpringBoot, which will not be described here. By default it is already built.
2. Write a Dockerfile. The content example is as follows:

#Download the java8 image FROM java:8
#Mount local files to the /tmp directory VOLUME /tmp
#Copy the file to the container ADD demo-0.0.1-SNAPSHOT.jar /demo.jar
#Expose port 8080 EXPOSE 8080
#Configure the command to be executed after starting the container ENTRYPOINT ["java","-jar","/demo.jar"]

Create an image using the docker build command:

docker build -t demo .

At this point, we execute docker images and we will see the image we just built, such as:

Deploy SpringBoot applications using K8S

1. Create the rc file demo-rc.yaml:

apiVersion: v1
kind: ReplicationController
metadata:
 name: demo
spec:
 # Number of nodes. Setting it to multiple can achieve load balancing. replicas: 1
 selector:
  app: demo
 template:
  metadata:
   labels:
    app: demo
  spec:
   containers:
   - name: demo
    #Image nameimage: demo
    #If there is a local image, the image will not be pulled from the warehousePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080

Run the following command to create a pod:

kubectl create -f demo-rc.yaml

After successful creation, we can view the pod:


ContainerCreating prompts that it is being created. You can view the creation log at this time:


It can be found that he prompts: redhat-cat.crt does not exist. Let's first check the file through the ll command:


It can be found that the file is a link file, which points to /etc/rhsm/ca/redhat-uep.pem, but this file does not exist. So where did this file come from? The answer is in this path. We need to install the rhsm software. Run the command to install it:

yum install *rhsm* -y

After waiting for a while, the installation is complete.

After the installation is complete, execute the ll command to check whether the file exists:

[root@MiWiFi-R3-srv ~]# ll /etc/rhsm/ca/redhat-uep.pem
ls: cannot access /etc/rhsm/ca/redhat-uep.pem: No such file or directory

We found that there is still no file, but we can create it manually:

touch /etc/rhsm/ca/redhat-uep.pem

After completing the above operations, we first delete rc and then create it:

[root@MiWiFi-R3-srv ~]# kubectl delete rc demo
replicationcontroller "demo" deleted
[root@MiWiFi-R3-srv ~]# kubectl create -f demo-rc.yaml 
replicationcontroller "demo" created

After waiting for a while, we checked po again and found that it had started successfully:

[root@MiWiFi-R3-srv ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
demo-hdmxs 1/1 Running 0 1m

At this point, we cannot access the application through the LAN, and we need to create a Service:

1. Create a service file demo-svc.yaml:

apiVersion: v1
kind: Service
metadata:
 name: demo
spec:
 type: NodePort
 ports:
 - port: 8080
  targetPort: 8080
  # The port that the node exposes to the outside world (must be in the range of 30000-32767)
  nodePort: 30001
 selector:
  app: demo

2. Execute the command:

[root@MiWiFi-R3-srv ~]# kubectl create -f demo-svc.yaml 
service "demo" created

3. We can view the service we just created:


At this point, we can access the application through ip:30001, as shown in the figure:


If you cannot access it, you need to turn off the firewall:

systemctl stop firewalld
iptables -P FORWARD ACCEPT

This is the end of this article about Docker+K8S cluster environment construction and distributed application deployment. For more relevant Docker K8S cluster environment construction content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Introduction to Kubernetes (k8s)
  • How to deploy a single-node redis database in kubernetes environment
  • Steps to deploy Django project using k8s
  • Implementation of k8s deployment of java project
  • Docker learning notes k8s deployment method
  • Production-level K8S basic environment deployment and configuration process

<<:  Example of how to retrieve the latest data using MySQL multi-table association one-to-many query

>>:  Example code for implementing dynamic skinning with vue+element

Recommend

Modify the maximum number of mysql connections and configuration files in docker

1. Find the mysql image docker ps 2. Enter the mi...

Common usage of regular expressions in Mysql

Common usage of Regexp in Mysql Fuzzy matching, c...

MySQL uses SQL statements to modify table names

In MySQL, you can use the SQL statement rename ta...

Database index knowledge points summary

Table of contents First Look Index The concept of...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

Will the deprecated Docker be replaced by Podman?

The Kubernetes team recently announced that it wi...

WeChat applet uniapp realizes the left swipe to delete effect (complete code)

WeChat applet uniapp realizes the left swipe to d...

Vue uniapp realizes the segmenter effect

This article shares the specific code of vue unia...

Solve the problem of garbled Chinese characters in Mysql5.7

When using MySQL 5.7, you will find that garbled ...

Implementation of waterfall layout in uni-app project

GitHub address, you can star it if you like it Pl...

Steps to encapsulate the carousel component in vue3.0

Table of contents 1: Encapsulation idea 2. Packag...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

Detailed explanation of nginx reverse proxy webSocket configuration

Recently, I used the webSocket protocol when work...