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

Kubernetes objects

Setting up a Kubernetes cluster with Windows nodes is complex and will be covered later in this book, and the principles will be demonstrated on Linux examples. From a Kubernetes API Server perspective, Windows and Linux nodes operate in almost the same way.

In the Kubernetes cluster, the cluster state is managed by the kube-apiserver component and is persisted in the etcd cluster. The state is abstracted and modeled as a set of Kubernetes objects these entities describe what containerized applications should be run, how they should be scheduled, and are the policies concerning restarting or scaling them. If there is anything you would like to achieve in your Kubernetes cluster, then you have to create or update Kubernetes objects. This type of model is called a declarative model you declare your intent and Kubernetes is responsible for changing the current state of the cluster to the desired (intended) one. The declarative model and the idea of maintaining the desired state is what makes Kubernetes so powerful and easy to use.

In this book, we will follow the convention from the official documentation, where objects are capitalized; for example, Pod or Service.

The anatomy of each Kubernetes Object is exactly the same; it has two fields:

  • Spec: This defines the desired state of the Object. This is where you define your requirements when creating or updating an Object.
  • Status: This is provided by Kubernetes and describes the current state of the Object.

Working with Kubernetes objects always requires using the Kubernetes API. Most commonly, you will manage Kubernetes objects using the command-line interface (CLI) for Kubernetes, which comes as a kubectl binary. It is also possible to interact with the Kubernetes API directly using client libraries.

The installation of  kubectl and examples of its usage will be covered in Chapter 6Interacting with Kubernetes Clusters.

Now, let's take a quick look at how an example Kubernetes Object is structured. When interacting directly with the Kubernetes API, objects must be specified in JSON format. However, kubectl allows us to use YAML manifest files, which are translated into JSON when you perform operations. Using YAML manifest files is generally recommended and you can expect most of the examples that you find in the documentation to follow this convention. As an example, we will use a definition of a Pod that consists of a single nginx web server Linux container, stored in a file called nginx.yaml:

apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-example
labels:
app: nginx-host
spec:
containers:
- name: nginx
image: nginx:1.17
ports:
- containerPort: 80

The required parts in the manifest file are as follows:

  • apiVersion: The version of the Kubernetes API being used for this Object.
  • kind: The type of Kubernetes Object. In this case, this is Pod.
  • metadata: Additional metadata for the Object.
  • spec: The Object Spec. In the example specification, the nginx container uses the nginx:1.17 Docker image and exposes port 80. The Spec is different for every Kubernetes Object and has to follow the API documentation. For example, for Pod, you can find the API reference here: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podspec-v1-core.

Creating the Pod is now as simple as running the following kubectl apply command:

kubectl apply -f nginx.yaml

If you would like to try out this command without a local Kubernetes cluster, we recommend using one for Kubernetes playground; for example, https://www.katacoda.com/courses/kubernetes/playground:

  1. In the master window, run the following kubectl command, which will apply a manifest file hosted on GitHub:
kubectl apply -f https://raw.githubusercontent.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/master/Chapter04/01_pod-example/nginx.yaml
  1. After a few seconds, the Pod will be created and its STATUS should be Running:
master $ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-example 1/1 Running 0 15s 10.40.0.1 node01 <none> <none>
  1. Use the curl command in the master window to get the Pod's IP (in this case, 10.40.0.1) to verify that the container is indeed running. You should see the raw contents of the default nginx web page:
curl http://10.40.0.1:80
kubectl currently offers two declarative approaches for managing Kubernetes objects: manifest files and kustomization files. Using the kustomize approach is much more powerful as it organizes manifest files and configuration generation in a predictable structure. You can learn more about kustomize here: https://github.com/kubernetes-sigs/kustomize/tree/master/docs.

Now, let's take a closer look at the Pod API Object.

主站蜘蛛池模板: 卫辉市| 嘉兴市| 旬阳县| 嘉兴市| 宜兰县| 年辖:市辖区| 石城县| 西藏| 普宁市| 宁国市| 福海县| 宜昌市| 河津市| 绵竹市| 阿拉善右旗| 洛川县| 福清市| 永平县| 鹤岗市| 诸暨市| 云浮市| 蚌埠市| 新昌县| 桐庐县| 介休市| 海安县| 仙居县| 建瓯市| 永丰县| 辽阳县| 宿迁市| 通渭县| 聂拉木县| 清镇市| 黄冈市| 泉州市| 都匀市| 广昌县| 满城县| 岑巩县| 建宁县|