Kubernetes - Deploy Containers Using YAML

In this scenario, you'll learn how to use Kubectl to create and launch Deployments, Replication Controllers and expose them via Services by writing yaml definitions.

YAML definitions define the Kubernetes Objects that become scheduled for deployment. The objects can be updated and redeployed to the cluster to change the configuration.

Step 1 - Create Deployment

One of the most common Kubernetes object is the deployment object. The deployment object defines the container spec required, along with the name and labels used by other parts of Kubernetes to discover and connect to the application.

Copy the following definition to the editor. The definition defines how to launch an application called webapp1 using the Docker Image katacoda/docker-http-serverthat runs on Port 80.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webapp1
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: webapp1
    spec:
      containers:
      - name: webapp1
        image: katacoda/docker-http-server:latest
        ports:
        - containerPort: 80

This is deployed to the cluster with the command 

kubectl create -f deployment.yaml

As it's a Deployment object, a list of all the deployed objects can be obtained via k

ubectl get deployment

Details of individual deployments can be outputted with 

kubectl describe deployment webapp1
Step 2 - Create Service

Kubernetes has powerful networking capabilities that control how applications communicate. These networking configurations can also be controlled via YAML.

Copy the Service definition to the editor. The Service selects all applications with the label webapp1. As multiple replicas, or instances, are deployed, they will be automatically load balanced based on this common label. The Service makes the application available via a NodePort.

apiVersion: v1
kind: Service
metadata:
  name: webapp1-svc
  labels:
    app: webapp1
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30080
  selector:
    app: webapp1

All Kubernetes objects are deployed in a consistent way using kubectl.

Deploy the Service with 

kubectl create -f service.yaml

As before, details of all the Service objects deployed with 

kubectl get svc

By describing the object it's possible to discover more details about the configuration 

kubectl describe svc webapp1-svc.
curl host01:30080
Step 3 - Scale Deployment

Details of the YAML can be changed as different configurations are required for deployment. This follows an infrastructure as code mindset. The manifests should be kept under source control and used to ensure that the configuration in production matches the configuration in source control.

Update the deployment.yaml file to increase the number of instances running. For example, the file should look like this:

replicas: 4
Updates to existing definitions are applied using kubectl apply. To scale the number of replicas, deploy the updated YAML file using 
kubectl apply -f deployment.yaml
 
    
Instantly, the desired state of our cluster has been updated, viewable with 
kubectl get deployment
 
    
Additional Pods will be scheduled to match the request. 
kubectl get pods
 
    
As all the Pods have the same label selector, they'll be load balanced behind the Service NodePort deployed.

Issuing requests to the port will result in different containers processing the request 

curl host01:30080

Additional Kubernetes Networking details and Object Definitions will will be covered in future scenarios.

 

转载于:https://www.cnblogs.com/oskb/p/10196840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值