With the development of container technology, K8S is becoming more and more popular. There are many articles about K8S on the web, but most of them are clustered deployment-related,
This article will focus on how to use K8S as an application developer.
- github
To prepare
Preliminary knowledge
- Docker, there is no doubt about knowing Docker before learning K8S.
tool
brew install kubectl
Copy the code
Kubectl is a command line tool for K8S for managing K8S clusters. For other operating systems, see the official documents. Of course, you can also manage containers using Dashboard.
Cluster
K8s Cluster: The production environment has a Cluster and the test environment has a Cluster.
Namespace
There are different namespaces in a Cluster to distinguish different business teams.
Pod
Pod is the smallest deployable unit in K8S. Typically, a Pod runs one Container, but sometimes multiple containers can be run. Similar docker – compose.
Deployment
Deployment is used to control pods, such as applying several pods to a single Deployment.
The configuration file
The configuration file for k8S is located in ~/.kube/config. You can also use the kubectl config command to view and change the configuration. When you use the command to change the configuration, the configuration file will also change.
Configuration files can specify the configuration of Cluster, Namespace, User, and Context. Here is a simplified version of the configuration file.
This configuration file configures a user shanyue, a cluster dev, and sets the dev context
apiVersion: v1
clusters:
- cluster:
certificate-authority: /Users/shanyue/.minikube/ca.crt
server: https://192.168.99.100:8443
name: dev
contexts:
- context:
cluster: dev
namespace: Business
user: shanyue
name: dev
current-context: dev
kind: Config
preferences: {}
users:
- name: shanyue
user:
client-certificate: /Users/shanyue/.minikube/client.crt
client-key: /Users/shanyue/.minikube/client.key
Copy the code
Current-context indicates the current context. You can also run the following command to set the context:
#Check the configuration
kubectl config view
#Viewing the Cluster List
kubectl config get-clusters
#Looking at the Context list
kubectl config get-contexts
#Set the current Context
kubectl config use-context dev
Copy the code
Create a resource
Kubectl create stands for creating a resource from a file, which can be either Deployment or Pod.
Kubectl run stands for creating resources from the image.
Kubectl create - f app. Yaml kubectl run -- image = k8s. GCR. IO/echoserver: 1.10 - port = 8080Copy the code
The kubectl apply command is typically used when deploying in CI to update resources based on configuration files.
You can write multiple configurations, Deployment, Service, and various Kind configurations in the configuration file. Here is a sample configuration of Node as the server language.
apiVersion: v1
kind: Service
metadata:
name: app
namespace: dev
labels:
name: app
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
selector:
name: app
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: app
namespace: dev
labels:
name: app
spec:
template:
metadata:
labels:
name: app
spec:
containers:
- name: app
image: node
imagePullPolicy: Always
env:
- name: PORT
value: "8080"
ports:
- containerPort: 8080
Copy the code
Access to the resource
The following $app represents the Name of a specific Pod
#Get all the Deployments under the current Context
kubectl get deployments
#Get all the pods in the current Context
kubectl get pods
#Get all pods in the current Cluster
kubectl get pods --all-namespaces
#Gets the state of a particular Pod
kubectl describe pod $app
#Go to the command line inside a Pod container
kubectl exec -it $app bash
#View the logs for a Pod
kubectl logs $app
Copy the code