...

Text file src/sigs.k8s.io/cli-utils/examples/alphaTestExamples/MultipleServices.md

Documentation: sigs.k8s.io/cli-utils/examples/alphaTestExamples

     1[kind]: https://github.com/kubernetes-sigs/kind
     2
     3# Demo: Multiple Services
     4
     5The following demonstrates applying and destroying multiple services to a `kind` cluster.
     6
     7Steps:
     81. Download the resources files for wordpress, mysql services.
     92. Spin-up kubernetes cluster on local using [kind].
    103. Deploy the wordpress, mysql services using kapply and verify the status.
    114. Destroy wordpress service and verify that only wordpress service is destroyed.
    12
    13First define a place to work:
    14
    15<!-- @makeWorkplace @testE2EAgainstLatestRelease -->
    16```
    17DEMO_HOME=$(mktemp -d)
    18```
    19
    20Alternatively, use
    21
    22> ```
    23> DEMO_HOME=~/hello
    24> ```
    25
    26## Establish the base
    27
    28Download the example configs for services `mysql` and `wordpress`
    29<!-- @createBase @testE2EAgainstLatestRelease -->
    30```
    31BASE=$DEMO_HOME/base
    32mkdir -p $BASE
    33OUTPUT=$DEMO_HOME/output
    34mkdir -p $OUTPUT
    35GREEN='\033[0;32m'
    36RED='\033[0;31m'
    37NC='\033[0m' # No Color
    38
    39mkdir $BASE/wordpress
    40mkdir $BASE/mysql
    41
    42curl -s -o "$BASE/wordpress/#1.yaml" "https://raw.githubusercontent.com\
    43/kubernetes-sigs/kustomize\
    44/master/examples/wordpress/wordpress\
    45/{deployment,service}.yaml"
    46
    47curl -s -o "$BASE/mysql/#1.yaml" "https://raw.githubusercontent.com\
    48/kubernetes-sigs/kustomize\
    49/master/examples/wordpress/mysql\
    50/{secret,deployment,service}.yaml"
    51
    52function expectedOutputLine() {
    53  if ! grep -q "$@" "$OUTPUT/status"; then
    54    echo -e "${RED}Error: output line not found${NC}"
    55    echo -e "${RED}Expected: $@${NC}"
    56    exit 1
    57  else
    58    echo -e "${GREEN}Success: output line found${NC}"
    59  fi
    60}
    61```
    62
    63Use the kapply init command to generate the inventory template. This contains
    64the namespace and inventory id used by apply to create inventory objects.
    65<!-- @createInventoryTemplate @testE2EAgainstLatestRelease-->
    66```
    67kapply init $BASE/mysql | tee $OUTPUT/status
    68expectedOutputLine "namespace: default is used for inventory object"
    69
    70kapply init $BASE/wordpress | tee $OUTPUT/status
    71expectedOutputLine "namespace: default is used for inventory object"
    72```
    73
    74Delete any existing kind cluster and create a new one. By default the name of the cluster is "kind"
    75<!-- @deleteAndCreateKindCluster @testE2EAgainstLatestRelease -->
    76```
    77kind delete cluster
    78kind create cluster
    79```
    80
    81Let's apply the mysql service
    82<!-- @RunMysql @testE2EAgainstLatestRelease -->
    83```
    84kapply apply $BASE/mysql --reconcile-timeout=120s --status-events | tee $OUTPUT/status
    85
    86expectedOutputLine "deployment.apps/mysql is Current: Deployment is available. Replicas: 1"
    87
    88expectedOutputLine "secret/mysql-pass is Current: Resource is always ready"
    89
    90expectedOutputLine "service/mysql is Current: Service is ready"
    91
    92# Verify that we have the mysql resources in the cluster.
    93kubectl get all --no-headers --selector=app=mysql | wc -l | xargs | tee $OUTPUT/status
    94expectedOutputLine "4"
    95
    96# Verify that we don't have any of the wordpress resources in the cluster. 
    97kubectl get all --no-headers --selector=app=wordpress | wc -l | xargs | tee $OUTPUT/status
    98expectedOutputLine "0"
    99```
   100
   101And the apply the wordpress service
   102<!-- @RunWordpress @testE2EAgainstLatestRelease -->
   103```
   104kapply apply $BASE/wordpress --reconcile-timeout=120s --status-events | tee $OUTPUT/status
   105
   106expectedOutputLine "service/wordpress is Current: Service is ready"
   107
   108expectedOutputLine "deployment.apps/wordpress is Current: Deployment is available. Replicas: 1"
   109
   110# Verify that we now have the wordpress resources in the cluster.
   111kubectl get all --no-headers --selector=app=wordpress | wc -l | xargs | tee $OUTPUT/status
   112expectedOutputLine "4"
   113```
   114
   115Destroy one service and make sure that only that service is destroyed and clean-up the cluster.
   116<!-- @destroyAppDeleteKindCluster @testE2EAgainstLatestRelease -->
   117```
   118kapply destroy $BASE/wordpress | tee $OUTPUT/status;
   119
   120expectedOutputLine "service/wordpress delete successful"
   121expectedOutputLine "deployment.apps/wordpress delete successful"
   122expectedOutputLine "delete result: 2 attempted, 2 successful, 0 skipped, 0 failed"
   123expectedOutputLine "reconcile result: 2 attempted, 2 successful, 0 skipped, 0 failed, 0 timed out"
   124
   125# Verify that we still have the mysql resources in the cluster.
   126kubectl get all --no-headers --selector=app=mysql | wc -l | xargs | tee $OUTPUT/status
   127expectedOutputLine "4"
   128
   129# TODO: When we implement wait for prune/destroy, add a check here to make
   130# sure the wordpress resources are actually deleted.
   131
   132kind delete cluster;
   133```

View as plain text