This is the fourth day of my participation in the More text Challenge. For details, see more text Challenge

The Pod controllers that manage stateless applications, especially business-type applications, are mostly deployment controllers. Here is a simple example of a practical deployment.

Java project Demo

Use SpringBoot to write a simple query hive data demo, into a JAR package.

Hive configuration

hive:
  url: jdbc:hive2://ip:10000/default
  driver-class-name: org.apache.hive.jdbc.HiveDriver
  type: com.alibaba.druid.pool.DruidDataSource
  username: hive
  password: hive
Copy the code

The configuration class

@Configuration public class HiveDataSource { @Autowired private Environment environment; public HiveDataSource() { } @Bean( name = {"hiveDruidJdbcDataSource"} ) @Qualifier("hiveDruidJdbcDataSource") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(this.environment.getProperty("hive.url")); dataSource.setDriverClassName(this.environment.getProperty("hive.driver-class-name")); dataSource.setUsername(this.environment.getProperty("hive.username")); dataSource.setPassword(this.environment.getProperty("hive.password")); return dataSource; } @Bean( name = {"hiveDruidJdbcTemplate"} ) public JdbcTemplate hiveJdbcTemplate(@Qualifier("hiveDruidJdbcDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); }}Copy the code

The control class

@RestController @RequestMapping({"/hive"}) public class HiveController { private static final Logger logger = LoggerFactory.getLogger(HiveController.class); @Autowired @Qualifier("hiveJdbcTemplate") private JdbcTemplate hiveJdbcTemplate; public HiveController() { } @GetMapping({"/create"}) public Map create() { StringBuffer sql = new StringBuffer("create table IF NOT EXISTS "); sql.append("HIVE_TEST"); sql.append("(KEY INT, VALUE STRING)"); sql.append("PARTITIONED BY (CTIME DATE)"); sql.append("ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' "); sql.append("STORED AS TEXTFILE"); logger.info(sql.toString()); this.hiveJdbcTemplate.execute(sql.toString()); return new 1(this); } @GetMapping({"/insert"}) public Map insert() { this.hiveJdbcTemplate.execute("set hive.exec.dynamic.partition.mode=nonstrict"); this.hiveJdbcTemplate.execute("insert into HIVE_TEST (key, value) values (1,'Chen')"); return new 2(this); } @GetMapping({"/select"}) public String select() { String sql = "select * from test_order_sample3"; List<Map<String, Object>> rows = this.hiveJdbcTemplate.queryForList(sql); Iterator it = rows.iterator(); while(it.hasNext()) { Map<String, Object> row = (Map)it.next(); System.out.println(String.format("%s\t%s", row.get("key"), row.get("value"))); } return "Done"; } @GetMapping({"/delete"}) public String Delete() { StringBuffer sql = new StringBuffer("DROP TABLE IF EXISTS"); sql.append("HIVE_TEST"); logger.info(sql.toString()); this.hiveJdbcTemplate.execute(sql.toString()); return "Done"; }}Copy the code

packaging

mvn clean package -Dmaven.test.skip=true 
Copy the code

Making docker image

Write Dockerfile configuration to make jar package into Docker image

FROM java:8
VOLUME /tmp
ADD springboot-hive.jar app.jar 
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Copy the code

Uploading a Private Repository

Upload the local Docker image to the harbor private image repository. See the following article for the deployment of the Harbor

docker tag springboot-hive:v1 harbor-test.libaigo.cn/dev/springboot-hive:v1
docker push harbor-test.libaigo.cn/dev/springboot-hive:v1
Copy the code

Create a namespace

kubectl create namespace devops
Copy the code

Deployment yaml configuration

apiVersion: apps/v1 kind: Deployment metadata: name: springboot-hive namespace: devops labels: app: springboot version: "1.0" spec: replicas: 3 Selector: matchLabels: app: Springboot Template: metadata: labels: app: Springboot version: "1.0" spec: terminationGracePeriodSeconds: 30 containers: - name: springboot hybrid image: harbor-test.libaigo.cn/dev/springboot-hive:v1 imagePullPolicy: IfNotPresent resources: requests: cpu: 100m memory: 1Gi livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 10 periodSeconds: 60 ports: - containerPort: 8080 name: web protocol: TCPCopy the code
  • The project uses the DevOps Namespace

  • Labels indicate the app name and version. You can also add the project name and related features to distinguish them

  • Added into the elegant closed strategy (terminationGracePeriodSeconds), 30 s closed pod to grace, grace visual extension of the time

  • Mirror pull Policy Use the local mirror and do not pull to avoid automatic pull during mirror update

  • Resource limits use CPU and memory for maximization of requests. There are no minimization requests written here, and no maximization or minimization restrictions

  • Check the container survival status (livenessProbe), using TCP protocol and port 8080 probe

  • Finally, declare the port and protocol of the service in the POD

Run the following command to create the deployment

kubectl apply -f springboot-hi-deployment.yaml
Copy the code

Check deploy and Pods status

kubectl get deploy,po  -n devops -o wide
Copy the code

The application has been successfully deployed (see Describe if the status is not Running)

kubectl describe deploy/springboot-hive  -n devops
Copy the code

Because the application is inside K8S, if you are on a k8S cluster node, you can access it through IP, but it is not externally accessible

K8s Internal cluster access

External access

To make it externally accessible, we need to use the Service object here

Service yaml configuration

apiVersion: v1 kind: Service metadata: name: springboot-hive-service namespace: devops labels: app: springboot version: Spec: type: NodePort ports: -port: 8080 Protocol: TCP NodePort: 30112 Selector: app: SpringBoot Version: "1.0"Copy the code

Here we use the NodePort type to map the application’s port 8080 to the K8S cluster’s port 30112

Run the following command to create the deployment

kubectl apply -f springboot-hive-service.yaml
kubectl get svc -o wide -n devops
Copy the code

You can view that the service has taken effect. You can access the service with any node IP address :30112 in the cluster

See the log

kubectl logs -f springboot-hive-7c844584f9-2v256 -n devops
Copy the code

Log on to the container

kubectl exec -it springboot-hive-7c844584f9-2v256 bash -n devops
Copy the code

Viewing the Deployment configuration

kubectl get  deploy/springboot-hive -n devops  -o yaml
Copy the code

The above command outputs the configuration in the yamL format on the terminal. Json is also supported

Change deployment configuration online

kubectl edit deploy/springboot-hive -n devops -o json
Copy the code

You can change the configuration online in JSON format

Dynamic expansion and shrinkage capacity

kubectl scale deploy/springboot-hive --replicas=5 -n devops
Copy the code

Upgrade and roll back an online image

kubectl set image deploy/springboot-hive springboot-hive=harbor-test.qupeiyin.cn/dev/springboot-hive:v2 -n devops
Copy the code

After the update is executed, take a look at the historical deployment record

kubectl rollout history deploy/springboot-hive
Copy the code

If no or missing historical deployment records are available, add –record when updating

kubectl set image deploy/springboot-hive springboot-hive=harbor-test.qupeiyin.cn/dev/springboot-hive:v2 -n devops --record
Copy the code

If the number of records is small, you can set how many times to record history

Add the revisionHistoryLimit attribute. RevisionHistoryLimit: 10 is the same as replicas

Roll back to the previous version

kubectl rollout undo deployment/springboot-hive -n devopskubectl rollout status deployment/springboot-hive -n devops
Copy the code

Example Roll back the specified version

kubectl rollout undo deployment/springboot-hive --to-revision=2 -n devops
Copy the code

Rancher

Rancher is a powerful K8S cluster management software, all the above operations can be completed in the interface, see the following figure