Skip to content

k8s Test

创建命名空间(Namespace)

创建文件test-ns.yaml:

yaml
apiVersion: v1
kind: Namespace
metadata:
  name: test-ns
#spec:
#  finalizers:
#    - kubernetes

执行命令应用此文件以创建namespace:

bash
kubectl apply -f test-ns.yaml

查看当前的namespace:

bash
kubectl get ns

创建应用(Deployment)

创建文件test-deploy.yaml:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  namespace: test-ns
  labels: 
    app: golang-simple-server
spec:
  replicas: 2
  selector:
    matchLabels:
      app: golang-simple-server
  template:
    metadata:
      labels:
        app: golang-simple-server
    spec:
      containers:
      - image: fuming/golang-hello-server # 这里一定要写对,如果写错,k8s不会报错
        imagePullPolicy: IfNotPresent
        name: server
        ports:
        - containerPort: 1423

执行命令应用此文件以创建deployment:

bash
kubectl apply -f test-deploy.yaml

查看当前namespace的deployment:

bash
kubectl get deployment -n test-ns

创建服务(Service)

创建文件test-service.yaml:

yaml
apiVersion: v1
kind: Service
metadata:
  name: test-service
  namespace: test-ns
spec:
  type: LoadBalancer
  selector:
    app: golang-simple-server
  ports:
    - protocol: TCP
      port: 80
      targetPort: 1423

执行命令应用此文件以创建service:

bash
kubectl apply -f test-service.yaml

查看当前namespace的service:

bash
kubectl get svc -n test-ns

执行

bash
curl http://127.0.0.1/v1/info/hello



curl -X POST http://127.0.0.1/v1/info/hello

即可访问服务。

最后

删除namespace

删除命名空间test-ns:

bash
kubectl delete namespace test-ns

注意删除namespace,会删除其下的所有deployment和service。

删除deployment

删除命名空间test-ns下的名为test的deployment:

bash
kubectl delete deployment test -n test-ns

删除service

删除命名空间test-ns下的名为test-service的deployment:

bash
kubectl delete svc test-service -n test-ns