Cloud Operations For Deployments

Erdem Erbaba
8 min readJul 5, 2024

--

Deploying a Java application to a live environment on Huawei Cloud involves several steps. This guide will cover the necessary Huawei Cloud services and the deployment process, focusing on practical implementation. We’ll assume you’re familiar with basic Java development and cloud concepts.

Index

1. Introduction
2. Setting Up Huawei Cloud Environment
— Creating a Huawei Cloud Account
— Setting Up IAM (Identity and Access Management)
— Configuring VPC (Virtual Private Cloud)
3. Preparing the Java Application
— Building the Java Application
— Dockerizing the Application
4. Using Huawei Cloud Services
— Elastic Cloud Server (ECS)
— Cloud Container Engine (CCE)
— Cloud Database Service (RDS)
— Object Storage Service (OBS)
5. Deployment Process
— Setting Up ECS
— Deploying with CCE
— Configuring RDS
— Using OBS for Static Content
6. Security and Monitoring
— Setting Up Security Groups
— Monitoring with Cloud Eye
7. Conclusion

### Introduction

Deploying a Java application to Huawei Cloud involves setting up the cloud environment, preparing the application, and using various Huawei Cloud services for deployment and management. This guide will provide a detailed, step-by-step approach to achieve this.

### Setting Up Huawei Cloud Environment

#### Creating a Huawei Cloud Account

1. **Sign Up**: Go to the Huawei Cloud website and sign up for an account.
2. **Verification**: Complete the verification process, which may require personal or business identification.
3. **Billing Information**: Add billing information to enable the use of paid services.

#### Setting Up IAM (Identity and Access Management)

1. **IAM Users**: Create IAM users for managing resources.
2. **Roles and Policies**: Assign roles and policies to users to grant the necessary permissions.

```bash
# Example CLI commands for IAM
huaweicloud iam create-user — name admin
huaweicloud iam attach-user-policy — user-name admin — policy-name AdminAccess
```

#### Configuring VPC (Virtual Private Cloud)

1. **Create a VPC**: Set up a VPC to isolate your network.
2. **Subnets**: Create subnets within the VPC for different application components.
3. **Route Tables**: Configure route tables for network traffic management.

```bash
# Example CLI commands for VPC
huaweicloud vpc create — name my-vpc — cidr 192.168.0.0/16
huaweicloud vpc create-subnet — vpc-name my-vpc — name my-subnet — cidr 192.168.1.0/24
```

### Preparing the Java Application

#### Building the Java Application

1. **Maven/Gradle Build**: Use Maven or Gradle to build your Java application.
2. **JAR/WAR File**: Ensure the application is packaged as a JAR or WAR file.

```bash
# Example Maven build command
mvn clean package
```

#### Dockerizing the Application

1. **Dockerfile**: Create a Dockerfile to containerize your application.

```dockerfile
# Example Dockerfile
FROM openjdk:11-jre-slim
COPY target/myapp.jar /usr/src/myapp/myapp.jar
WORKDIR /usr/src/myapp
CMD [“java”, “-jar”, “myapp.jar”]
```

2. **Build Docker Image**: Build the Docker image using Docker CLI.

```bash
docker build -t myapp:latest .
```

### Using Huawei Cloud Services

#### Elastic Cloud Server (ECS)

1. **Create ECS Instance**: Set up an ECS instance to host your application.

```bash
huaweicloud ecs create — name my-ecs — image-id ubuntu-18.04 — flavor s3.large.2 — vpc-id my-vpc — subnet-id my-subnet
```

2. **Access ECS Instance**: SSH into the ECS instance to configure it.

```bash
ssh root@<ECS_PUBLIC_IP>
```

#### Cloud Container Engine (CCE)

1. **Create CCE Cluster**: Set up a CCE cluster to manage your Docker containers.

```bash
huaweicloud cce create-cluster — name my-cce-cluster — vpc my-vpc — subnet my-subnet
```

2. **Deploy Docker Image**: Push your Docker image to Huawei Cloud’s Container Registry and deploy it on CCE.

```bash
# Push Docker image to Huawei Cloud Container Registry
docker tag myapp:latest <HUAWEI_CLOUD_CONTAINER_REGISTRY>/myapp:latest
docker push <HUAWEI_CLOUD_CONTAINER_REGISTRY>/myapp:latest

# Deploy on CCE
huaweicloud cce create-deployment — cluster-name my-cce-cluster — name myapp-deployment — image <HUAWEI_CLOUD_CONTAINER_REGISTRY>/myapp:latest
```

#### Cloud Database Service (RDS)

1. **Create RDS Instance**: Set up an RDS instance for your application’s database needs.

```bash
huaweicloud rds create — name my-rds — flavor rds.mysql.s1.large — volume-type COMMON — volume-size 100 — vpc my-vpc — subnet my-subnet
```

2. **Configure Database Connection**: Update your application’s configuration to connect to the RDS instance.

```properties
# Example application.properties for Spring Boot
spring.datasource.url=jdbc:mysql://<RDS_ENDPOINT>:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=mysecretpassword
```

#### Object Storage Service (OBS)

1. **Create OBS Bucket**: Set up an OBS bucket for storing static content and application assets.

```bash
huaweicloud obs create-bucket — bucket-name myapp-bucket
```

2. **Upload Static Content**: Upload your static content to the OBS bucket.

```bash
huaweicloud obs upload — bucket-name myapp-bucket — file-path ./static — key static
```

### Deployment Process

#### Setting Up ECS

1. **Install Docker**: Install Docker on your ECS instance.

```bash
sudo apt-get update
sudo apt-get install -y docker.io
```

2. **Run Docker Container**: Pull your Docker image and run it on ECS.

```bash
docker pull <HUAWEI_CLOUD_CONTAINER_REGISTRY>/myapp:latest
docker run -d -p 8080:8080 <HUAWEI_CLOUD_CONTAINER_REGISTRY>/myapp:latest
```

#### Deploying with CCE

1. **Create Kubernetes Deployment**: Use Kubernetes manifests to deploy your application.

```yaml
# Example deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
— name: myapp
image: <HUAWEI_CLOUD_CONTAINER_REGISTRY>/myapp:latest
ports:
— containerPort: 8080
```

```bash
kubectl apply -f deployment.yaml
```

2. **Create Kubernetes Service**: Expose your application using a Kubernetes Service.

```yaml
# Example service.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
selector:
app: myapp
ports:
— protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
```

```bash
kubectl apply -f service.yaml
```

#### Configuring RDS

1. **Database Initialization**: Initialize your RDS instance with necessary schemas and data.

```bash
mysql -h <RDS_ENDPOINT> -u root -p < mydatabase.sql
```

2. **Update Application Configuration**: Ensure your application is configured to connect to the RDS instance.

#### Using OBS for Static Content

1. **Access Static Content**: Configure your application to serve static content from OBS.

```java
// Example Spring Boot configuration to serve static content
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“/static/**”)
.addResourceLocations(“https://<OBS_ENDPOINT>/myapp-bucket/static/”);
}
}
```

preparing the application, utilizing Huawei Cloud services, and ensuring security and monitoring. The following sections will wrap up the deployment process and highlight key aspects to ensure a smooth operation.

### Deployment Process (Continued)

#### Ensuring High Availability

1. **Load Balancing**: Use Huawei Cloud’s Elastic Load Balance (ELB) service to distribute traffic across multiple ECS instances or CCE pods for high availability.

```bash
# Create ELB instance
huaweicloud elb create — name myapp-elb — vpc-id my-vpc

# Add backend servers (ECS instances)
huaweicloud elb add-backend-server — elb-id myapp-elb — server-id <ECS_INSTANCE_ID> — port 8080

# Add backend servers (CCE pods)
huaweicloud elb add-backend-server — elb-id myapp-elb — server-id <CCE_POD_ID> — port 8080
```

2. **Auto Scaling**: Configure auto-scaling for ECS instances or CCE pods to handle varying loads.

```bash
# Create auto-scaling group
huaweicloud as create-group — name myapp-as-group — vpc-id my-vpc — min-size 2 — max-size 10 — desired-capacity 2

# Create scaling policy
huaweicloud as create-policy — name myapp-scale-out-policy — group-id myapp-as-group — adjustment-type PercentChangeInCapacity — adjustment-value 50
huaweicloud as create-policy — name myapp-scale-in-policy — group-id myapp-as-group — adjustment-type PercentChangeInCapacity — adjustment-value -30
```

### Security and Monitoring (Continued)

#### Implementing Firewall Rules

1. **Define Firewall Rules**: Protect your application by defining appropriate firewall rules for allowed inbound and outbound traffic.

```bash
huaweicloud vpc create-firewall — name myapp-firewall — vpc-id my-vpc

# Allow HTTP and HTTPS traffic
huaweicloud vpc add-firewall-rule — firewall-id myapp-firewall — protocol tcp — port-range 80,443 — cidr 0.0.0.0/0

# Allow SSH traffic from specific IP
huaweicloud vpc add-firewall-rule — firewall-id myapp-firewall — protocol tcp — port-range 22 — cidr <YOUR_IP_ADDRESS>
```

#### Using Cloud Eye for Advanced Monitoring

1. **Custom Metrics**: Define custom metrics for detailed monitoring and alerts.

```bash
# Create a custom metric for monitoring application-specific data
huaweicloud ces put-metric-data — namespace Custom — metric-name MyAppCustomMetric — dimensions Name=InstanceId,Value=<ECS_INSTANCE_ID> — value 42
```

2. **Dashboards and Alerts**: Set up dashboards and configure alerts for key performance indicators (KPIs) to monitor the application’s health.

```bash
# Create a dashboard
huaweicloud ces create-dashboard — name MyAppDashboard

# Add widgets to the dashboard
huaweicloud ces add-widget — dashboard-name MyAppDashboard — metric-name CPUUtilization — namespace ECS — dimensions InstanceId=<ECS_INSTANCE_ID> — statistic Average

# Set up alert rules
huaweicloud ces create-alarm — name MyAppHighCPUAlarm — metric-name CPUUtilization — namespace ECS — dimensions InstanceId=<ECS_INSTANCE_ID> — period 300 — threshold 80 — comparison-operator GreaterThanOrEqualToThreshold — alarm-actions arn:huaweicloud:smn:region:account-id:topic/my-sns-topic
```

### Final Steps and Best Practices

#### Regular Backups

1. **Database Backups**: Schedule regular backups for your RDS instance to prevent data loss.

```bash
huaweicloud rds create-backup — instance-id <RDS_INSTANCE_ID> — name myapp-db-backup
huaweicloud rds schedule-backup — instance-id <RDS_INSTANCE_ID> — policy ‘{“Period”: “daily”, “RetentionDays”: 7}’
```

2. **Snapshot ECS Instances**: Regularly take snapshots of your ECS instances for disaster recovery.

```bash
huaweicloud ecs create-snapshot — instance-id <ECS_INSTANCE_ID> — name myapp-ecs-snapshot
huaweicloud ecs create-snapshot-policy — policy ‘{“Name”: “daily-snapshot”, “InstanceIds”: [“<ECS_INSTANCE_ID>”], “Period”: “daily”, “RetentionDays”: 7}’
```

#### Security Audits and Compliance

1. **Regular Audits**: Perform regular security audits to ensure compliance with best practices and regulatory requirements.

```bash
huaweicloud security audit — scope ECS,RDS — report-output security-audit-report.txt
```

2. **Update Dependencies**: Keep your application and its dependencies up to date to mitigate vulnerabilities.

```bash
# Update dependencies using Maven
mvn versions:display-dependency-updates
mvn versions:use-latest-releases
mvn clean install
```

### Conclusion

Deploying a Java application to Huawei Cloud involves careful planning and execution of several key steps:

1. **Setting Up Huawei Cloud Environment**: Establish a secure and scalable cloud environment using IAM, VPC, and other foundational services.
2. **Preparing the Java Application**: Build and dockerize your application for deployment.
3. **Using Huawei Cloud Services**: Utilize ECS for compute, CCE for container orchestration, RDS for databases, and OBS for storage.
4. **Deployment Process**: Implement deployment strategies for ECS and CCE, and configure services for high availability and scalability.
5. **Security and Monitoring**: Ensure robust security with firewalls and security groups, and maintain visibility into application performance with Cloud Eye.

By following this guide, you can deploy a Java application to Huawei Cloud efficiently and securely, leveraging the full suite of Huawei Cloud services to achieve a robust, scalable, and high-performance deployment. Regular backups, security audits, and dependency updates are critical for maintaining the integrity and availability of your application in the long run.

--

--