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

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.

主站蜘蛛池模板: 柘荣县| 湄潭县| 拉萨市| 宝坻区| 浦城县| 简阳市| 大理市| 高台县| 淮安市| 九寨沟县| 寿阳县| 平泉县| 固始县| 绵竹市| 绵竹市| 漳州市| 祁东县| 云霄县| 丽水市| 图木舒克市| 昌黎县| 安多县| 色达县| 闽清县| 华亭县| 永春县| 湖口县| 开鲁县| 莎车县| 桓台县| 南安市| 哈巴河县| 鄢陵县| 井陉县| 衡东县| 房产| 衢州市| 泸定县| 富蕴县| 兴安盟| 孟州市|