436 字
2 分钟

Deploying Applications to a Kubernetes Cluster

Run from the command line#

Terminal window
kubectl run testapp --image=ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1
kubectl get pod 查看是否成功建立

Deploy with a YAML file#

pod.yaml file#
Terminal window
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
# Define containers; can be multiple
containers:
- name: test-k8s # Container name
image: ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1 # Image

Deployment commands#

Terminal window
mkdir Deployment && cd Deployment
vim pod.yml # Fill in the content above
kubectl apply -f pod.yml # Create Pod

Using a Deployment#

app.yml file#
Terminal window
apiVersion: apps/v1
kind: Deployment
metadata:
# Deployment name
name: test-k8s
spec:
replicas: 2
# Used to select and associate Pods; all labels must match
selector:
matchLabels:
app: test-k8s
# Define Pod-related data
template:
metadata:
labels:
app: test-k8s
spec:
# Define containers; can be multiple
containers:
- name: test-k8s # Container name
image: ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1 # Image

Deployment commands#

Terminal window
kubectl apply -f app.yml
kubectl get deployment

How a Deployment associates Pods via labels#

Common commands#

Terminal window
# View Pod IPs and other details
kubectl get pod -o wide
# Deploy an application
kubectl apply -f app.yaml
# View Deployments
kubectl get deployment
# View Pods
kubectl get pod -o wide
# View Pod details
kubectl describe pod pod-name
# View logs
kubectl logs pod-name
# Open an interactive shell into a Pod container; use -c container-name to select a container
kubectl exec -it pod-name -- bash
# Scale replicas up or down
kubectl scale deployment test-k8s --replicas=5
# Forward a port from the cluster to the node
kubectl port-forward pod-name 8090:8080
# View rollout history
kubectl rollout history deployment test-k8s
# Roll back to the previous revision
kubectl rollout undo deployment test-k8s
# Roll back to a specific revision
kubectl rollout undo deployment test-k8s --to-revision=2
# Delete a Deployment
kubectl delete deployment test-k8s

Use kubectl describe ID to view Pod details#

Use kubectl logs ID to view logs#

Use kubectl logs ID -f to stream logs continuously#

Use kubectl exec -it ID -- bash to enter a Pod#

Use kubectl scale deployment ID --replicas=5 to scale replicas#

Use kubectl port-forward pod-name 1234:8080 to map a container’s internal port#

Deploying Applications to a Kubernetes Cluster
https://catcat.blog/en/kubernetes-deployment.html
作者
猫猫博客
发布于
2023-09-14
许可协议
CC BY-NC-SA 4.0