Deployment is one of the most commonly used workload types in Kubernetes. It is also called a stateless workload. Its goal is to ensure that a fixed number of pods in the cluster always run continuously in the state you specify. This topic describes how to create a stateless application in an ACK cluster by using the ACK console and kubectl.
Before you begin
Before you create a workload, we recommend that you familiarize yourself with the basic knowledge and usage notes of workloads. For more information, see Workloads. This topic is divided into the following two parts:
Create a Deployment: This part describes how to create a Deployment by using the ACK console or kubectl.
Parameters: This part provides documentation on console configurations and YAML templates for using kubectl.
Create a Deployment
The following section describes a simplified workload creation process. You can refer to this process to quickly deploy and validate workloads. After you are familiar with the basic operations, you can configure custom workloads. For more information, see Parameters.
Configure basic information for the application
Log on to the ACK console. In the left-side navigation pane, click Clusters. On the Clusters page, click the name of the cluster that you want to manage. In the left-side navigation pane of the cluster details page, choose .On the Deployments page, click Create from Image in the upper-right corner.
On the Basic Information wizard page, configure the basic settings of the application. Click Next to go to the Container page.
Configure a container
In the Container section, configure the Image Name and Port parameters. Other parameters are optional. Keep the default settings. Click Next to go to the Advanced page. The following section describes details of the container images.
Before pulling this image, you need to enable Internet access for the cluster. If you keep the default value for the Configure SNAT for VPC parameter when you create a cluster, the cluster can access the Internet. For more information about how to enable Internet access for an existing cluster, see Enable an existing ACK cluster to access the Internet.
registry.cn-hangzhou.aliyuncs.com/acs-sample/nginx:latest
Configure advanced settings
On the Advanced wizard page, configure the following settings: access control, scaling, scheduling, labels, and annotations. In the Access Control section, configure the method to expose backend pods. In the dialog box that appears, configure the parameters, and click OK. Then, click Create.
In this step, a LoadBalancer Service is created to expose the workload. You are charged for the CLB instance. For more information about the detailed billing items, see Pay-as-you-go. If you do not plan to use the CLB in the future, promptly release the CLB.
View the application
On the Complete wizard page, you can view the created application. Click View Details below Creation Task Submitted. Click the Access Method tab. Select the nginx-test-svc Service and click the hyperlink in the External Endpoint column to start the magic cube game.
You can view, edit, and redeploy the created workload in the console.
Before you create a workload, make sure that you have connected to the cluster by using kubectl. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
Copy the following cometent to the deployment.yaml file: In the following YAML file, a Deployment is defined and the
LoadBalancer
type Service is used to expose the Deployment.apiVersion: apps/v1 kind: Deployment # The type of the workload. metadata: name: nginx-test namespace: default # Change the namespace based on your business requirements. labels: app: nginx spec: replicas: 2 # Specify the number of pods. selector: matchLabels: app: nginx template: # Pod configurations. metadata: labels: # Pod labels. app: nginx spec: containers: - name: nginx # The name of the container. image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6 # Specify the version of the NGINX image. ports: - containerPort: 80 # The port exposed by the container. protocol: TCP # Set the protocol to TCP or UDP. The default protocol is TCP. --- # service apiVersion: v1 kind: Service metadata: name: nginx-test-svc namespace: default # Change the namespace based on your business requirements. labels: app: nginx spec: selector: app: nginx # Match the tag to ensure that the service points to the correct pods. ports: - port: 80 # The port provided by the Service in the cluster. targetPort: 80 # The port on which the application in the container listens (containerPort). protocol: TCP # The protocol. Default value: TCP. type: LoadBalancer # The type of the Service. Default value: ClusterIP. This Service can only be accessed by other Services or pods within the cluster.
Run the following command to create the Deployment and Service:
kubectl apply -f deployment.yaml
Expected output:
deployment.apps/nginx-test created service/nginx-test-svc created
Run the following command to query the public IP address of the Service:
kubectl get svc
Expected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 172.16.**.*** <none> 443/TCP 4h47m nginx-test-svc LoadBalancer 172.16.**.*** 106.14.**.*** 80:31130/TCP 1h10m
Enter the public IP address (
106.14.**.***
) of NGINX in the address bar of your browser to access the NGINX container to which the workload belongs.
Parameters
Parameters on the ACK console
Basic information
Parameter | Description |
Parameter | Description |
Name | The name of the workload. The name of the pod to which the workload belongs is generated based on this parameter. |
Namespace | The namespace to which the workload belongs. |
Replicas | The number of pods that are provisioned for the workload. Default value: 2. |
Type | The type of the workload. For more information about how to select a workload, see Deploy a workload. |
Label | The label of the workload. |
Annotations | The annotations of the workload. |
Synchronize Timezone | Specify whether to synchronize the time zone between nodes and containers. |
Container
Advanced settings
Section | Parameter | Description |
Section | Parameter | Description |
Access Control | Service | A Service provides a fixed and unified Layer 4 (transport layer) entry for a group of pods. It is a resource that must be configured when a workload is exposed. Multiple Service types are supported. Valid values: Cluster IP, Node Port, and SLB. Before you configure a Service, get a basic understanding of Services. For more information, see Service management. |
Ingress | Ingresses provide Layer 7 (application layer) entry for multiple Services in a cluster and forward requests to different Services based on domain names. Before you use an Ingress, you must install an Ingress controller. ACK provides multiple options for different scenarios. For more information, see Comparison among Nginx Ingresses, ALB Ingresses, and MSE Ingresses. | |
Scaling | HPA | The auto scaling is triggered by monitoring the performance metrics of the container. Horizontal Pod Autoscaler (HPA) can help you automatically adjust the total amount of resources used by your workloads when your workloads fluctuate. You can scale out resources to relieve pressure during high workloads and scale down resources during low workloads. For more information, see Implement HPA. |
CronHPA | You can configure this parameter to trigger workload scaling at a scheduled time. This method is suitable for scenarios where workloads change periodically. For example, social media has periodic traffic peaks after lunch and after dinner. For more information, see Implement CronHPA. | |
Scheduling | Update Method | The mechanism by which workloads replace old pods with new pods when the pod configuration changes.
|
| Affinity, anti-affinity, and tolerations are used for scheduling, even if the pod is running on a specific node. Scheduling operations are complex and need to be planned in advance. For more information, see Scheduling. | |
Labels, Annotations | Pod Labels | Add a label to each pod to which the workload belongs. All resources in the cluster, including workloads and services, are matched with pods by using labels. By default, ACK adds labels in the format of |
Pod Annotations | Add an annotation to each pod to which the workload belongs. Some features in ACK use annotations. You can configure the annotations when you use these features. |
Workload YAML example
apiVersion: apps/v1
kind: Deployment # The type of the workload.
metadata:
name: nginx-test
namespace: default # Change the namespace based on your business requirements.
labels:
app: nginx
spec:
replicas: 2 # Specify the number of pods.
selector:
matchLabels:
app: nginx
template: # Pod configurations.
metadata:
labels: # Pod labels.
app: nginx
annotations: # Pod annotations.
description: "This is an application deployment"
spec:
containers:
- name: nginx # The name of the image.
image: nginx:1.7.9 #Specify the version of the NGINX image.
ports:
- name: nginx # name
containerPort: 80 # The port exposed by the container.
protocol: TCP # Set the protocol to TCP or UDP. The default protocol is TCP.
command: ["/bin/sh"] # Container startup item.
args: [ "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY) && exec nginx -g 'daemon off;'"] #Add the output variable and the command to start NGINX.
stdin: true # Enable standard input.
tty: true # Assign a virtual terminal.
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config # The name of the ConfigMap.
key: SPECIAL_LEVEL # The key name of the ConfigMap.
securityContext:
privileged: true # Set the parameter to true to enable privileged mode. Set the parameter to false to disable privileged mode. Default value: false.
resources:
limits:
cpu: "500m" # The maximum CPU usage, which is set to 500 millicores.
memory: "256Mi" # The maximum memory usage, which is set to 256 MiB.
ephemeral-storage: "1Gi" # The maximum temporary storage usage, which is set to 1 GiB.
requests:
cpu: "200m" # The minimum requested CPU usage, which is set to 200 millicores.
memory: "128Mi" # The minimum requested memory usage, which is set to 128 MiB.
ephemeral-storage: "500Mi" # The minimum temporary storage usage requested, which is set to 500 MiB.
livenessProbe: # Configure container liveness probes.
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe: # Configure container readiness probes.
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 10
volumeMounts:
- name: tz-config
mountPath: /etc/localtime
readOnly: true
volumes:
- name: tz-config
hostPath:
path: /etc/localtime # Mount the /etc/localtime files of the host to the same path in the container by using the volumeMounts and volume fields.
---
# service
apiVersion: v1
kind: Service
metadata:
name: nginx-test-svc
namespace: default # Change the namespace based on your business requirements.
labels:
app: nginx
spec:
selector:
app: nginx # Match the tag to ensure that the service points to the correct pods.
ports:
- port: 80 # The port provided by the Service in the cluster.
targetPort: 80 # The port on which the application in the container listens (containerPort).
protocol: TCP # The protocol. Default value: TCP.
type: ClusterIP # The type of the Service. Default value: ClusterIP. This Service can only be accessed by other Services or pods within the cluster.
---
# ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: default # Change the namespace based on your business requirements.
annotations:
kubernetes.io/ingress.class: "nginx" # Specify the Ingress controller type.
# If you use the SLB Ingress controller, you can specify the following parameters:
# service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: "lb-xxxxxxxxxx"
# service.beta.kubernetes.io/alibaba-cloud-loadbalancer-spec: "slb.spec.s1.small"
spec:
rules:
- host: foo.bar.com # Replace this parameter with your domain name
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service # The name of the backend service.
port:
number: 80 # The backend service port.
tls: # Optional. This parameter is used to enable HTTPS.
- hosts:
- foo.bar.com # Replace this parameter with your domain name
secretName: tls-secret # The secret name of the TLS certificate.
References
Use StatefulSets for applications that need to use stable persistent storage, such as databases. For more information, see Use a StatefulSet to create a stateful application.
For more information about how to resolve the issues that occur when you create a workload, see FAQ about workloads.
If your pods encounter exceptions, see Pod troubleshooting.