With the development of container technology, k8s is becoming more and more hot. There are many articles on k8s on the web, but most of them are related to cluster deployment,
This article is mainly about how to use k8s as an application developer.
Get ready
brew install kubectl
kubectlis the k8s command-line tool for managing k8s clusters. The above is the installation method under Mac, other operating systems refer to the official documentation. Of course, you can also use Dashboard to manage containers.
Cluster
K8s cluster, the general production environment has a Cluster cluster, the test environment has a Cluster cluster.
Namespace
In a Cluster there will be different Namespace that can differentiate between different business teams.
Pod
The Pod is the smallest deployable unit in the k8s. Typically one Pod runs a Container, but sometimes multiple Container are run. Similar to Docker-compose.
Deployment
Deployment is used to control pods, such as controlling a few pods of an application.
Configuration file
The configuration file location about K8s is ~/.kube/config also available in the kubectl config view view configuration.
The configuration file can be specified, Cluster Namespace configured, User and set Context . The following is a simple version of the configuration file.
apiVersion: v1clusters:- cluster: certificate-authority: /Users/shanyue/.minikube/ca.crt server: https://192.168.99.100:8443 name: Devcontexts:- context: cluster: k8s-dev namespace: edu-dev user: shanyue name: devcurrent-context: devkind: Configpreferences: {}users:- name: shanyue user: client-certificate: /Users/shanyue/.minikube/client.crt client-key: /Users/shanyue/.minikube/client.key
current-contextit represents the current context, or it can be set by the following name.
kubectl config use-context dev
Create a resource
kubectl createRepresents a resource created from a file, either Deployment or Pod.
kubectl runRepresents the creation of a resource based on a mirror.
kubectl create -f app.yamlkubectl run --image=k8s.gcr.io/echoserver:1.10 --port=8080
Typically, a command is used when you deploy in CI kubectl apply , which represents a creation or update based on a configuration file.
You can write multiple configurations in the configuration file, or you can write various configurations of Deployment,service. The following is a configuration of node as a server language sample.
apiVersion: v1kind: Servicemetadata: name: app namespace: dev labels: name: appspec: ports: - port: 8080 targetPort: 8080 protocol: TCP selector: name: app---apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: app namespace: dev labels: name: appspec: template: metadata: labels: name: app spec: containers: - name: app image: node imagePullPolicy: Always env: - name: PORT value: "8080" ports: - containerPort: 8080
Access resources
# get all the Deploymentkubectl get Deploymentskubectl get pods# under current context to get all Cluster under current Deployme Ntkubectl get pods--all-namespaces# get the status of a specific pod kubectl describe $pod app# into a pod container inside the command line kubectl exec-it $app bash# Check Look at the log of a Pod kubectl logs app