Understanding Kubernetes Configurations

Prerequisites

You will need to get yourself acquainted with the concepts and deployment of Kubernetes for you to follow through with this article. There are specific terms that may need you to refer back to the articles for this tutorial.

Section 1: Kubernetes Primitives

Users leverage Kubernetes APIs for the creation, scaling and termination of applications on the platform. Kubernetes manages various types of objects, each targeted by a different operation. Objects constitute the basic building blocks of Kubernetes, availed as primitives for managing containerized applications. In summary, below are the most important Kubernetes API objects:

  1. Clusters
  2. Nodes
  3. Namespaces
  4. Pods
  5. Labels and selectors
  6. Services
  7. Replication set
  8. Deployment

Section 2: Kubernetes Nodes and Namespaces

If you have successfully followed how to Install and Deploy Kubernetes on Ubuntu 16.04, you can get a list of all nodes and namespaces by running the command below:

kubectl get nodesOutput
NAME STATUS ROLES AGE VERSION
spc3c97hei-master-1 Ready master 10m v1.8.7
spc3c97hei-worker-1 Ready <none> 4m v1.8.7
spc3c97hei-worker-2 Ready <none> 4m v1.8.7
kubectl get namespaces
Output
NAME STATUS AGE
default Active 11m
kube-public Active 11m
kube-system Active 11m
stackpoint-system Active 4m

Section 3: How to Create and Deploy Pods

We need the kubectl CLI to declare objects in YAML format to submit to Kubernetes for processing. Let us create our first pod:

apiVersion: "v1"
kind: Pod
metadata:
name: web-pod
labels:
name: web
env: dev
spec:
containers:
- name: myweb
image: nginx
ports:
- containerPort: 80
name: http
protocol: TCP
kubectl create -f Sample-Pod.yamlOutput
pod "web-pod" created
kubectl get podsOutput
NAME READY STATUS RESTARTS AGE
web-pod 1/1 Running 0 2m

Section 4: How to use Services to Expose a Pod

You can expose Pods either internally or externally using Services. In our simple project, let us expose the Nginx web server pod publicly. Our preferred object of use is the NodePort, which uses an arbitrary port on a node. We begin by creating a Sample-Service.yaml file, which has the coded instructions to define the Nginx service.

apiVersion: v1
kind: Service
metadata:
name: web-svc
labels:
name: web
env: dev
spec:
selector:
name: web
type: NodePort
ports:
- port: 80
name: http
targetPort: 80
protocol: TCP
kubectl create -f Sample-Service.yml
Output
service "web-svc" created
kubectl get servicesOutput
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.3.0.1 <none> 443/TCP 28m
web-svc NodePort 10.3.0.143 <none> 80:32096/TCP 38s
curl http://node_ip_address:32096
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Nginx!</title>
...
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Section 5: Using Replica Sets to Scale Pods

Replica sets maintain the minimum required Pods running within the cluster. We are going to destroy the Pod we created and use the Replica Set to create three replacements.

kubectl delete pod web-podOutput
pod "web-pod" deleted
apiVersion: apps/v1beta2
kind: ReplicaSet
metadata:
name: web-rs
labels:
name: web
env: dev
spec:
replicas: 3
selector:
matchLabels:
name: web
template:
metadata:
labels:
name: web
env: dev
spec:
containers:
- name: myweb
image: nginx
ports:
- containerPort: 80
name: http
protocol: TCP
kubectl create -f Simple-RS.ymlOutput
replicaset "web-rs" created
kubectl get podsOutput
NAME READY STATUS RESTARTS AGE
web-rs-htb58 1/1 Running 0 8s
web-rs-khtld 1/1 Running 0 8s
web-rs-p5lzg 1/1 Running 0 8s
kubectl delete pod web-rs-p5lzgOutput
pod "web-rs-p5lzg" deleted
kubectl get podsOutput
NAME READY STATUS RESTARTS AGE
web-rs-htb58 1/1 Running 0 3m
web-rs-khtld 1/1 Running 0 3m
web-rs-fqh2f 0/1 ContainerCreating 0 3s
web-rs-p5lzg 1/1 Running 0 3m
web-rs-p5lzg 0/1 Terminating 0 3m

Section 6: Deployments

Deployments are easier for upgrades and patches compared to Pods and Replica Sets. It is the fundamental reason why you would want to use them to deploy containers. For instance, you can upgrade a running pod with Deployments but not with Replica Sets. The feature allows upgrades without downtime and enables PAAS capabilities. Let us first delete our replica set and then proceed to create a Deployment like so:

kubectl delete rs web-rsOutput
replicaset "web-rs" deleted
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: web-dep
labels:
name: web
env: dev
spec:
replicas: 3
selector:
matchLabels:
name: web
template:
metadata:
labels:
name: web
spec:
containers:
- name: myweb
image: nginx
ports:
- containerPort: 80
kubectl create -f Simple-Deployment.ymlOutput
deployment "web-dep" created
kubectl get deploymentsOutput
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
web-dep 3 3 3 3 2m
kubectl get podsOutput
NAME READY STATUS RESTARTS AGE
web-dep-8594f5c765-5wmrb 1/1 Running 0 3m
web-dep-8594f5c765-6cbsr 1/1 Running 0 3m
web-dep-8594f5c765-sczf8 1/1 Running 0 3m
kubectl delete deployment web-depOutput
deployment "web-dep" deleted
kubectl delete service web-svc
Output
service "web-svc" deleted

Conclusion

This tutorial has built on what we discussed in the beginner’s guide to Kubernetes. We have examined primitive configurations as well as the most important configurations that you are likely to encounter on Kubernetes. We have configured a web server using a pod, created a service, Replica set and deployment. The configurations we have studied in this tutorial are among the most fundamental when handling Kubernetes.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com