官术网_书友最值得收藏!

A container platform deployment example with Kubernetes

In the following Kubernetes YAML configuration, we will define various Kubernetes keywords related to deployment. One important variable is container: image. Here, we are referring to an existing Docker container image that Kubernetes will use to create a container under pod deployment. This container image should already be customized to suit your requirements. The kubectl command will read this configuration and start your container as appropriate. It will start three replica pods with the same container image and it will use the matchLables value to replace the specific container inside the three newly created pods. The keyword replica indicates that a specific value has been used to create pods with the same container image.

There are three different kinds of replicas:

  • kind: Kind defines the type of work kubectl will be doing on the given configuration. Currently, we are using Deployment.
  • metadata: This will assign a name to the deployment and label your POD with the given name under app.
  • spec: This stands for specification, where you configure the number of replicas, the selector name for pod replacement, and so on.

Create a deployment controller file with the following content. This can then be provided to the kubectl command as a parameter during the create or apply steps.

The file name is as follows: /my-hello-packt-application-deployment.yaml. Refer to the following code:

#vim my-packt-application-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-hello-packt-application-deployment
labels:
app: my-hello—packt-application
spec:
replicas: 3
selector:
matchLabels:
app: my-hello-packt-application
template:
metadata:
labels:
app: my-hello-packt-application
spec:
containers:
- name: my-hello-packt-application
image: repository-hub.packthub.example.com/my-hello-packt-application:1.1

ports:
- containerPort: 8080

In this example code, we have carried out the following steps:

  1. We have created a deployment named my-hello-packt-application-deployment, indicated by the .metadata.name field
  2. This deployment creates three replicated pods, which is shown in the replicas field
  3. The selector field is used by the deployment module to identify which pod to manage
  4. The template field contains the following sub fields:
    • The pods are labeled app: my-hello-packt-application-deployment using the labels field
    • The pod template's specification, or the .template.spec field, indicates that the pods run on one container, my-hello-packt-application, which runs the NGINX Docker hub image Version 1.7.9
    • We have created one container and named it my-hello-packt-application-deployment using the name field
    • We have run the my-hello-packt-deployment image Version 1.1
    • We have opened port 8080 so that the container can send and accept traffic

Let's take a look at the following steps that show how to create or deploy services using the previously mentioned configuration file:

  1. To create this deployment, run the following command:
kubectl create -f my-hello-packt-application-deployment.yaml                   
OR
kubectl apply -f my-hello-packt-application-deployment.yaml
It would be useful to read more about imperative or declarative object configuration on the Kubernetes website, as both commands are accepted by the  kubectl command to deploy new contents on the cluster. Refer to this link for more details: https://kubernetes.io/docs/concepts/overview/object-management-kubectl/declarative-config/.
  1. We can append the -- record to this command to record the current command in the annotations of the created or updated resource. This is useful for future reviews, such as investigating which commands were executed in each deployment revision.
  2. Next, run kubectl to get the deployments. The results will look as follows:

  1. When you check your deployments in your Kubernetes cluster, the following fields are shown:
    • NAME: The deployment name
    • DESIRED: The desired number of replicas of our application
    • CURRENT: The number of replicas currently running
    • UP-TO-DATE: The number of replicas that have been updated to get the desired state
    • AVAILABLE: The number of replicas of the application are available to our users
    • AGE: The amount of time that the application has been running
  1. To see the rollout status deployment, run the following kubectl command:
 kubectl rollout status deployment/my-hello-packt-application- deployment

This command will show the rollout status of the kubectl apply command. It will also show you the number of desired pods. In the following screenshot, you will notice that it has created three replica sets, which are all up to date, with the corresponding provided YAML file:

Then, run the following command a few seconds later:

kubectl get deployments

In the following output, we can see that the deployment created three replicas:

  1. To see the ReplicaSet (rs) created by the deployment, run the following command:
 kubectl get rs

The ReplicaSet is always shown or formatted as follows:

[DEPLOYMENT-NAME]-[POD-TEMPLATE-HASH-VALUE].
  1. Hash values are auto-generated during deployment. The following screenshot shows the dynamically generated value. Run the following command to get the auto-generated label for the pods:
 kubectl get pods --show-labels

The following output is returned:

主站蜘蛛池模板: 江阴市| 伊川县| 甘孜县| 繁峙县| 阳东县| 大洼县| 甘孜县| 抚宁县| 修水县| 九龙城区| 衡东县| 甘孜县| 永仁县| 来安县| 梅州市| 荔波县| 卓尼县| 苏州市| 株洲县| 张掖市| 扎赉特旗| 博罗县| 陈巴尔虎旗| 龙岩市| 临朐县| 揭西县| 孟津县| 陵川县| 金沙县| 定远县| 中宁县| 天等县| 依安县| 会理县| 仙游县| 新营市| 龙口市| 阿城市| 乌苏市| 临沧市| 武义县|