To prepare
This article will use K8S to deploy a Springboot + Redis application, because it is an example, so the function is relatively simple, only set value and get value two APIS.
(1) Set value
(2) Get the value
Building Web Applications
(1) Create a Springboot project(2) Introducing maven dependencies for Redis and Jedis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
Copy the code
(3) Create redis tool class, connect redis, and introduce redisIp using variable
@Component
public class RedisUtil {
@Value("${redisIp}")
private String redisIp;
@Value("${redisPort:6379}")
private int redisPort;
@Bean
public RedisConnectionFactory initRedisConnFactory(a) {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisIp, redisPort);
// configuration.setPassword("123456");
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);
return connectionFactory;
}
@Bean
public RedisTemplate getRedisTemplate(a){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(initRedisConnFactory());
returnredisTemplate; }}Copy the code
(4) Create an API
@RestController
@RequestMapping(value = "/api/v1/k8s")
public class K8sDemoController {
@Autowired
private RedisTemplate redisTemplate;
/** * set the value *@param key
* @param value
* @return* /
@GetMapping(value = "/setkv")
@ResponseBody
public String setKV(@RequestParam String key,@RequestParam String value) {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set(key,value);
return "Setup successful";
}
/** * get the value *@param key
* @return* /
@GetMapping(value = "/get/{key}")
@ResponseBody
public String get(@PathVariable(value = "key") String key) {
ValueOperations valueOperations = redisTemplate.opsForValue();
String result = String.valueOf(valueOperations.get(key));
returnresult; }}Copy the code
(5) Upload the JAR package to the server for later use, and also upload the JDK installation package to the server
Start a Redis service using K8S
Note: All resources of K8S can be described using yamL files
(1) Create a file named redis-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: redis
labels:
name: redis
spec:
replicas: 1 The number of copies is 1
selector:
name: redis
template: # template
metadata:
name: redis
labels:
name: redis
spec:
containers:
- name: redis
image: redis:latest
imagePullPolicy: IfNotPresent # Mirror pull policy
ports:
- containerPort: 6379 # container port
Copy the code
Run the following command to create ReplicationController for Redis
kubectl create -f redis-controller.yaml
Copy the code
Check whether the vm is created successfully:
kubectl get rc
Copy the code
View the created Pod:
kubectl get pods
Copy the code
(2) Create a file named redis-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: redis
spec:
selector:
name: redis # Select the Pod tag
ports:
- port: 6379 Expose the port number
targetPort: 6379 # Service port number
Copy the code
Use the following command to create the redis Service:
kubectl create -f redis-svc.yaml
Copy the code
Check whether the vm is created successfully:
kubectl get svc
Copy the code
As shown in the figure above, the IP assigned to redis is
10.109.56.243
Redis is started.
Create a Web application image using Dockerfile
Dockerfile contains the following contents:
# Base image
FROM centos:7
# Tag information
LABEL author=lsy
# set the variable, then reference it directly
ENV path=/usr/soft
Create a directory
RUN mkdir ${path}
Set the working directory
WORKDIR ${path}
Put the JDK installation package into a directory in the container. This command will automatically unpack it
ADD jdk-8u191-linux-x64.tar.gz ${path}
Set up the container Java environment
ENV JAVA_HOME=${path}/jdk1.8.0_191
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH
Copy the Web application JAR package to the directory
COPYK8s_demo - 1.0. The jar${path}
Expose port 8080
EXPOSE 8080
Run the command when the container is first started
CMDJava jar - DredisIp = 10.109.56.243 k8s_demo - 1.0. The jar
Copy the code
The redisIp parameter in the command executed after the container is started is obtained from the previous step. K8s allows direct access between services in the cluster.
Java jar - DredisIp = 10.109.56.243 k8s_demo - 1.0. The jarCopy the code
Run the following command in the Dockerfile directory to build the image:
Docker build-t cnode- 1:2000 /k8sdemo:v1.2Copy the code
Run the following command to view the image:
docker images
Copy the code
After the image is created successfully, you need to push the image to the private repository:
Docker push cnode 1:50 00 / k8sdemo: v1.2Copy the code
At this point, the Web application image has been built and pushed to a private repository
Deploy Web applications using K8S
(1) Create yaml file named k8sdemo-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: k8sdemo
labels:
name: k8sdemo
spec:
replicas: 3 # If the number of copies is 3, K8S will automatically perform load balancing
selector:
name: k8sdemo
template:
metadata:
name: k8sdemo
labels:
name: k8sdemo
spec:
containers:
- name: k8sdemo
image: Cnode 1:50 00 / k8sdemo: v1.2 # Image just uploaded to private repository
imagePullPolicy: IfNotPresent # Mirror pull policy
ports:
- containerPort: 8080
Copy the code
Run the following command to create ReplicationController for the Web app:
kubectl create -f k8sdemo-controller.yaml
Copy the code
Check whether the vm is created successfully:
Check to see if 3 pods are created:
(2) Create a YAMl file named k8sdemo-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: k8sdemo
spec:
type: NodePort
selector:
name: k8sdemo
ports:
- port: 8080
targetPort: 8080
nodePort: 30080
Copy the code
The web app needs to allow external access, so you need to set the spec.type of Service to NodePort and the port exposed to external access in spec.ports. NodePort (30080)
Use the following command to create a Service for the Web app:
kubectl create -f k8sdemo-svc.yaml
Copy the code
Check whether the vm is created successfully:
As shown in the figure above, you can see that it maps port 8080 to port 30080 of the cluster node.
Next, you can access the WEB app API using port 30080.
validation
Set key=name, value=liusy
(2) Get key=name
I’m Liusy, a programmer who likes to work out.
Welcome to pay attention to wechat public number [Liusy01], exchange Java technology and fitness, get more dry goods, get Java advanced dry goods, get the latest factory interview materials, become a Java god together.
Here we go. Keep an eye on it.